diff -r 000000000000 -r 6474c204b198 browser/devtools/markupview/test/browser_markupview_html_edit_03.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser/devtools/markupview/test/browser_markupview_html_edit_03.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,180 @@ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test that outerHTML editing keybindings work as expected and that *special* +// elements like , and can be edited correctly. + +const TEST_URL = "data:text/html," + + "" + + "" + + "" + + "
" + + "" + + ""; +const SELECTOR = "#keyboard"; +const OLD_HTML = '
'; +const NEW_HTML = '
Edited
'; + +let test = asyncTest(function*() { + let {inspector} = yield addTab(TEST_URL).then(openInspector); + + inspector.markup._frame.focus(); + + info("Checking that pressing escape cancels edits"); + yield testEscapeCancels(inspector); + + info("Checking that pressing F2 commits edits"); + yield testF2Commits(inspector); + + info("Checking that editing the element works like other nodes"); + yield testBody(inspector); + + info("Checking that editing the element works like other nodes"); + yield testHead(inspector); + + info("Checking that editing the element works like other nodes"); + yield testDocumentElement(inspector); + + info("Checking (again) that editing the element works like other nodes"); + yield testDocumentElement2(inspector); +}); + +function testEscapeCancels(inspector) { + let def = promise.defer(); + let node = getNode(SELECTOR); + + selectNode(node, inspector).then(() => { + inspector.markup.htmlEditor.on("popupshown", function onPopupShown() { + inspector.markup.htmlEditor.off("popupshown", onPopupShown); + + ok(inspector.markup.htmlEditor._visible, "HTML Editor is visible"); + is(node.outerHTML, OLD_HTML, "The node is starting with old HTML."); + + inspector.markup.htmlEditor.on("popuphidden", function onPopupHidden() { + inspector.markup.htmlEditor.off("popuphidden", onPopupHidden); + ok(!inspector.markup.htmlEditor._visible, "HTML Editor is not visible"); + + let node = getNode(SELECTOR); + is(node.outerHTML, OLD_HTML, "Escape cancels edits"); + def.resolve(); + }); + + inspector.markup.htmlEditor.editor.setText(NEW_HTML); + + EventUtils.sendKey("ESCAPE", inspector.markup.htmlEditor.doc.defaultView); + }); + + EventUtils.sendKey("F2", inspector.markup._frame.contentWindow); + }); + + return def.promise; +} + +function testF2Commits(inspector) { + let def = promise.defer(); + let node = getNode(SELECTOR); + + inspector.markup.htmlEditor.on("popupshown", function onPopupShown() { + inspector.markup.htmlEditor.off("popupshown", onPopupShown); + + ok(inspector.markup.htmlEditor._visible, "HTML Editor is visible"); + is(node.outerHTML, OLD_HTML, "The node is starting with old HTML."); + + inspector.once("markupmutation", (e, aMutations) => { + ok(!inspector.markup.htmlEditor._visible, "HTML Editor is not visible"); + + let node = getNode(SELECTOR); + is(node.outerHTML, NEW_HTML, "F2 commits edits - the node has new HTML."); + def.resolve(); + }); + + inspector.markup.htmlEditor.editor.setText(NEW_HTML); + EventUtils.sendKey("F2", inspector.markup._frame.contentWindow); + }); + + inspector.markup._frame.contentDocument.documentElement.focus(); + EventUtils.sendKey("F2", inspector.markup._frame.contentWindow); + + return def.promise; +} + +function testBody(inspector) { + let body = getNode("body"); + let bodyHTML = '

'; + let bodyFront = inspector.markup.walker.frontForRawNode(body); + let doc = content.document; + + let mutated = inspector.once("markupmutation"); + inspector.markup.updateNodeOuterHTML(bodyFront, bodyHTML, body.outerHTML); + + return mutated.then(mutations => { + is(getNode("body").outerHTML, bodyHTML, " HTML has been updated"); + is(doc.querySelectorAll("head").length, 1, "no extra s have been added"); + return inspector.once("inspector-updated"); + }); +} + +function testHead(inspector) { + let head = getNode("head"); + let headHTML = 'New Title'; + let headFront = inspector.markup.walker.frontForRawNode(head); + let doc = content.document; + + let mutated = inspector.once("markupmutation"); + inspector.markup.updateNodeOuterHTML(headFront, headHTML, head.outerHTML); + + return mutated.then(mutations => { + is(doc.title, "New Title", "New title has been added"); + is(doc.defaultView.foo, undefined, "Script has not been executed"); + is(doc.querySelector("head").outerHTML, headHTML, " HTML has been updated"); + is(doc.querySelectorAll("body").length, 1, "no extra s have been added"); + return inspector.once("inspector-updated"); + }); +} + +function testDocumentElement(inspector) { + let doc = content.document; + let docElement = doc.documentElement; + let docElementHTML = 'Updated from document element

Hello

'; + let docElementFront = inspector.markup.walker.frontForRawNode(docElement); + + let mutated = inspector.once("markupmutation"); + inspector.markup.updateNodeOuterHTML(docElementFront, docElementHTML, docElement.outerHTML); + + return mutated.then(mutations => { + is(doc.title, "Updated from document element", "New title has been added"); + is(doc.defaultView.foo, undefined, "Script has not been executed"); + is(doc.documentElement.id, "updated", " ID has been updated"); + is(doc.documentElement.className, "", " class has been updated"); + is(doc.documentElement.getAttribute("foo"), "bar", " attribute has been updated"); + is(doc.documentElement.outerHTML, docElementHTML, " HTML has been updated"); + is(doc.querySelectorAll("head").length, 1, "no extra s have been added"); + is(doc.querySelectorAll("body").length, 1, "no extra s have been added"); + is(doc.body.textContent, "Hello", "document.body.textContent has been updated"); + }); +} + +function testDocumentElement2(inspector) { + let doc = content.document; + let docElement = doc.documentElement; + let docElementHTML = 'Updated again from document element

Hello again

'; + let docElementFront = inspector.markup.walker.frontForRawNode(docElement); + + let mutated = inspector.once("markupmutation"); + inspector.markup.updateNodeOuterHTML(docElementFront, docElementHTML, docElement.outerHTML); + + return mutated.then(mutations => { + is(doc.title, "Updated again from document element", "New title has been added"); + is(doc.defaultView.foo, undefined, "Script has not been executed"); + is(doc.documentElement.id, "somethingelse", " ID has been updated"); + is(doc.documentElement.className, "updated", " class has been updated"); + is(doc.documentElement.getAttribute("foo"), null, " attribute has been removed"); + is(doc.documentElement.outerHTML, docElementHTML, " HTML has been updated"); + is(doc.querySelectorAll("head").length, 1, "no extra s have been added"); + is(doc.querySelectorAll("body").length, 1, "no extra s have been added"); + is(doc.body.textContent, "Hello again", "document.body.textContent has been updated"); + }); +}