Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / Node / fragment-mutation.html
blob9b772748f326c14fdf48a14e734ddc9418b6269f
1 <html>
2 <head>
3 <title>Fragment Mutation Tests</title>
4 <script>
6 if (window.testRunner)
7 testRunner.dumpAsText();
9 var log = [];
11 function logResult(description, outcome)
13 log.push({ description: description, outcome: outcome});
16 function printLog(methodName)
18 var entries = log.map(function(entry) {
19 return "<li>" + entry.description + ": " + entry.outcome;
20 });
21 document.body.appendChild(document.createElement("p")).innerHTML = "This test creates a fragment containing three elements: \"B\", \"U\", and \"P\", " +
22 " attempts to " + methodName + " this fragment and studies effects of mutation events on the fragment.";
23 document.body.appendChild(document.createElement("ul")).innerHTML = entries.join("\n");
24 document.body.appendChild(document.createElement("br"));
25 log = [];
28 function produceNodeNameString(nodes)
30 var node = nodes.firstChild;
31 var result = "";
32 while(node) {
33 result += node.nodeName;
34 node = node.nextSibling;
36 return result;
39 function expectException(error)
41 return function(stash, exception) {
42 if (!exception)
43 return "FAIL, expected exception with code " + code + ". The resulting fragment was: \"" + produceNodeNameString(stash) + "\".";
45 if (exception instanceof error)
46 return "PASS";
47 return "FAIL, expected exception code: " + code + ", was: " + exception + ".";
51 function expectNodes(nodes)
53 return function(stash, exception) {
54 if (exception)
55 return "FAIL, unexpected exception thrown: " + exception;
56 var result = produceNodeNameString(stash);
57 if (nodes == result)
58 return "PASS";
59 return "FAIL, expected \"" + nodes + "\", was \"" + result + "\".";
63 function testFragment(method, description, mutationHandler, expectation, nonStop)
65 var once = 0;
66 var logged = 0;
67 var frag = document.createDocumentFragment();
68 var stash = document.body.appendChild(document.createElement("div"));
69 frag.appendChild(document.createElement("b"));
70 frag.appendChild(document.createElement("u"));
71 frag.appendChild(document.createElement("p"));
72 frag.addEventListener("DOMSubtreeModified", function(evt)
74 if (!nonStop && once++)
75 return;
77 try {
78 mutationHandler(evt, frag, stash);
80 catch(e) {
81 logResult(description, expectation(stash, e));
82 logged++;
84 }, false);
86 try {
87 method(stash, frag);
89 catch(e) {
90 logResult(description, expectation(stash, e));
91 logged++;
93 if (!logged)
94 logResult(description, expectation(stash));
95 document.body.removeChild(stash);
98 function appendChildMethod(object, subject)
100 object.appendChild(subject);
103 function insertBeforeMethod(object, subject)
105 object.insertBefore(subject, object.firstChild);
108 function runTest(methodName, method)
110 var missing = document.body.appendChild(document.createElement("em"));
111 testFragment(method, "Inserting an element in front of the next item in fragment should not affect the result", function(evt, frag)
113 frag.insertBefore(missing, frag.firstChild);
114 }, expectNodes("BUP"));
116 var extra = document.body.appendChild(document.createElement("em"));
117 testFragment(method, "Appending an element at the end of the fragment should not affect the result", function(evt, frag)
119 frag.appendChild(extra);
120 }, expectNodes("BUP"));
122 testFragment(method, "Continually re-appending removed element to the fragment should eventually throw NOT_FOUND_ERR", function(evt, frag, stash)
124 stash.insertBefore(frag.lastChild, stash.firstChild);
125 }, expectException(TypeError), true);
126 printLog(methodName);
128 function runTests()
130 runTest("appendChild", appendChildMethod);
131 runTest("insertBefore", insertBeforeMethod);
134 </script>
135 </head>
136 <body onload="runTests()">
137 </body>
138 </html>