browser/devtools/markupview/test/browser_markupview_mutation_01.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 // Tests that various mutations to the dom update the markup view correctly.
michael@0 8 // The test for comparing the markup view to the real dom is a bit weird:
michael@0 9 // - Select the text in the markup view
michael@0 10 // - Parse that as innerHTML in a document we've created for the purpose.
michael@0 11 // - Remove extraneous whitespace in that tree
michael@0 12 // - Compare it to the real dom with isEqualNode.
michael@0 13
michael@0 14 const TEST_URL = TEST_URL_ROOT + "doc_markup_mutation.html";
michael@0 15 // All the mutation types we want to test.
michael@0 16 const TEST_DATA = [
michael@0 17 {
michael@0 18 desc: "Adding an attribute",
michael@0 19 test: () => {
michael@0 20 let node1 = getNode("#node1");
michael@0 21 node1.setAttribute("newattr", "newattrval");
michael@0 22 }
michael@0 23 },
michael@0 24 {
michael@0 25 desc: "Removing an attribute",
michael@0 26 test: () => {
michael@0 27 let node1 = getNode("#node1");
michael@0 28 node1.removeAttribute("newattr");
michael@0 29 }
michael@0 30 },
michael@0 31 {
michael@0 32 desc: "Updating the text-content",
michael@0 33 test: () => {
michael@0 34 let node1 = getNode("#node1");
michael@0 35 node1.textContent = "newtext";
michael@0 36 }
michael@0 37 },
michael@0 38 {
michael@0 39 desc: "Updating the innerHTML",
michael@0 40 test: () => {
michael@0 41 let node2 = getNode("#node2");
michael@0 42 node2.innerHTML = "<div><span>foo</span></div>";
michael@0 43 }
michael@0 44 },
michael@0 45 {
michael@0 46 desc: "Removing child nodes",
michael@0 47 test: () => {
michael@0 48 let node4 = getNode("#node4");
michael@0 49 while (node4.firstChild) {
michael@0 50 node4.removeChild(node4.firstChild);
michael@0 51 }
michael@0 52 }
michael@0 53 },
michael@0 54 {
michael@0 55 desc: "Appending a child to a different parent",
michael@0 56 test: () => {
michael@0 57 let node17 = getNode("#node17");
michael@0 58 let node1 = getNode("#node2");
michael@0 59 node1.appendChild(node17);
michael@0 60 }
michael@0 61 },
michael@0 62 {
michael@0 63 desc: "Swapping a parent and child element, putting them in the same tree",
michael@0 64 // body
michael@0 65 // node1
michael@0 66 // node18
michael@0 67 // node19
michael@0 68 // node20
michael@0 69 // node21
michael@0 70 // will become:
michael@0 71 // body
michael@0 72 // node1
michael@0 73 // node20
michael@0 74 // node21
michael@0 75 // node18
michael@0 76 // node19
michael@0 77 test: () => {
michael@0 78 let node18 = getNode("#node18");
michael@0 79 let node20 = getNode("#node20");
michael@0 80
michael@0 81 let node1 = getNode("#node1");
michael@0 82
michael@0 83 node1.appendChild(node20);
michael@0 84 node20.appendChild(node18);
michael@0 85 }
michael@0 86 }
michael@0 87 ];
michael@0 88
michael@0 89 let test = asyncTest(function*() {
michael@0 90 info("Creating the helper tab for parsing");
michael@0 91 let parseTab = yield addTab("data:text/html,<html></html>");
michael@0 92 let parseDoc = content.document;
michael@0 93
michael@0 94 info("Creating the test tab");
michael@0 95 let contentTab = yield addTab(TEST_URL);
michael@0 96 let doc = content.document;
michael@0 97 // Strip whitespace in the document for easier comparison
michael@0 98 stripWhitespace(doc.documentElement);
michael@0 99
michael@0 100 let {inspector} = yield openInspector();
michael@0 101 let markup = inspector.markup;
michael@0 102
michael@0 103 info("Expanding all markup-view nodes");
michael@0 104 yield markup.expandAll();
michael@0 105
michael@0 106 for (let step of TEST_DATA) {
michael@0 107 info("Starting test: " + step.desc);
michael@0 108
michael@0 109 info("Executing the test markup mutation, listening for inspector-updated before moving on");
michael@0 110 let updated = inspector.once("inspector-updated");
michael@0 111 step.test();
michael@0 112 yield updated;
michael@0 113
michael@0 114 info("Expanding all markup-view nodes to make sure new nodes are imported");
michael@0 115 yield markup.expandAll();
michael@0 116
michael@0 117 info("Comparing the markup-view markup with the content document");
michael@0 118 compareMarkup(parseDoc, inspector);
michael@0 119 }
michael@0 120 });
michael@0 121
michael@0 122 function stripWhitespace(node) {
michael@0 123 node.normalize();
michael@0 124 let iter = node.ownerDocument.createNodeIterator(node,
michael@0 125 NodeFilter.SHOW_TEXT + NodeFilter.SHOW_COMMENT, null);
michael@0 126
michael@0 127 while ((node = iter.nextNode())) {
michael@0 128 node.nodeValue = node.nodeValue.replace(/\s+/g, '');
michael@0 129 if (node.nodeType == Node.TEXT_NODE &&
michael@0 130 !/[^\s]/.exec(node.nodeValue)) {
michael@0 131 node.parentNode.removeChild(node);
michael@0 132 }
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 function compareMarkup(parseDoc, inspector) {
michael@0 137 // Grab the text from the markup panel...
michael@0 138 let markupContainerEl = getContainerForRawNode("body", inspector).elt;
michael@0 139 let sel = markupContainerEl.ownerDocument.defaultView.getSelection();
michael@0 140 sel.selectAllChildren(markupContainerEl);
michael@0 141
michael@0 142 // Parse it
michael@0 143 let parseNode = parseDoc.querySelector("body");
michael@0 144 parseNode.outerHTML = sel;
michael@0 145 parseNode = parseDoc.querySelector("body");
michael@0 146
michael@0 147 // Pull whitespace out of text and comment nodes, there will
michael@0 148 // be minor unimportant differences.
michael@0 149 stripWhitespace(parseNode);
michael@0 150
michael@0 151 // console.log(contentNode.innerHTML, parseNode.innerHTML);
michael@0 152 ok(getNode("body").isEqualNode(parseNode),
michael@0 153 "Markup panel matches what's in the content document.");
michael@0 154 }

mercurial