3 <title>Fragment Mutation Tests
</title>
7 testRunner
.dumpAsText();
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
;
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"));
28 function produceNodeNameString(nodes
)
30 var node
= nodes
.firstChild
;
33 result
+= node
.nodeName
;
34 node
= node
.nextSibling
;
39 function expectException(error
)
41 return function(stash
, exception
) {
43 return "FAIL, expected exception with code " + code
+ ". The resulting fragment was: \"" + produceNodeNameString(stash
) + "\".";
45 if (exception
instanceof error
)
47 return "FAIL, expected exception code: " + code
+ ", was: " + exception
+ ".";
51 function expectNodes(nodes
)
53 return function(stash
, exception
) {
55 return "FAIL, unexpected exception thrown: " + exception
;
56 var result
= produceNodeNameString(stash
);
59 return "FAIL, expected \"" + nodes
+ "\", was \"" + result
+ "\".";
63 function testFragment(method
, description
, mutationHandler
, expectation
, nonStop
)
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
++)
78 mutationHandler(evt
, frag
, stash
);
81 logResult(description
, expectation(stash
, e
));
90 logResult(description
, expectation(stash
, e
));
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
);
130 runTest("appendChild", appendChildMethod
);
131 runTest("insertBefore", insertBeforeMethod
);
136 <body onload=
"runTests()">