OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>innerText setter test</title> | 2 <title>innerText setter test</title> |
3 <script src="/resources/testharness.js"></script> | 3 <script src="/resources/testharness.js"></script> |
4 <script src="/resources/testharnessreport.js"></script> | 4 <script src="/resources/testharnessreport.js"></script> |
5 <div id="container"></div> | 5 <div id="container"></div> |
6 <script> | 6 <script> |
| 7 // As of March 2017, WebKit and Blink have inconsistent results depending on |
| 8 // rendered or not. setupTest() tests a rendered case, and setupTestDetached() |
| 9 // tests a not-rendered case. |
| 10 |
7 function setupTest(context, plain) { | 11 function setupTest(context, plain) { |
8 // context is either a string or an element node | 12 // context is either a string or an element node |
9 if (typeof context === "string") { | 13 if (typeof context === "string") { |
10 container.innerHTML = context; | 14 container.innerHTML = context; |
11 } else { | 15 } else { |
12 container.innerHTML = ""; | 16 container.innerHTML = ""; |
13 container.appendChild(context); | 17 container.appendChild(context); |
14 } | 18 } |
15 var e = container.firstChild; | 19 var e = container.firstChild; |
16 while (e && e.nodeType != Node.ELEMENT_NODE) { | 20 while (e && e.nodeType != Node.ELEMENT_NODE) { |
17 e = e.nextSibling; | 21 e = e.nextSibling; |
18 } | 22 } |
| 23 e.offsetWidth; |
| 24 var oldChild = e.firstChild; |
| 25 e.innerText = plain; |
| 26 return [e, oldChild]; |
| 27 } |
| 28 |
| 29 function setupTestDetached(context, plain) { |
| 30 var detachedContainer = document.createElement("div"); |
| 31 // context is either a string or an element node |
| 32 if (typeof context === "string") { |
| 33 detachedContainer.innerHTML = context; |
| 34 } else { |
| 35 detachedContainer.innerHTML = ""; |
| 36 detachedContainer.appendChild(context); |
| 37 } |
| 38 var e = detachedContainer.firstChild; |
| 39 while (e && e.nodeType != Node.ELEMENT_NODE) { |
| 40 e = e.nextSibling; |
| 41 } |
19 var oldChild = e.firstChild; | 42 var oldChild = e.firstChild; |
20 e.innerText = plain; | 43 e.innerText = plain; |
21 return [e, oldChild]; | 44 return [e, oldChild]; |
22 } | 45 } |
| 46 |
| 47 function assertNewSingleTextNode(newChild, expectedText, oldChild) { |
| 48 assert_not_equals(newChild, null, "Should have a child"); |
| 49 assert_equals(newChild.nodeType, Node.TEXT_NODE, "Child should be a text node"
); |
| 50 assert_equals(newChild.nextSibling, null, "Should have only one child"); |
| 51 assert_equals(newChild.data, expectedText); |
| 52 assert_not_equals(newChild, oldChild, "Child should be a *new* text node"); |
| 53 } |
| 54 |
| 55 function assertNoEmptyTextChild(parent) { |
| 56 for (var child = parent.firstChild; child; child = child.nextSibling) { |
| 57 if (child.nodeType === Node.TEXT_NODE) { |
| 58 assert_not_equals(child.data, "", "Should not have empty text nodes"); |
| 59 } |
| 60 } |
| 61 } |
| 62 |
23 function testText(context, plain, expectedText, msg) { | 63 function testText(context, plain, expectedText, msg) { |
24 test(function(){ | 64 test(function(){ |
25 var arr = setupTest(context, plain); | 65 var arr = setupTest(context, plain); |
26 var e = arr[0]; | 66 assertNewSingleTextNode(arr[0].firstChild, expectedText, arr[1]); |
27 var oldChild = arr[1]; | |
28 assert_not_equals(e.firstChild, null, "Should have a child"); | |
29 assert_equals(e.firstChild.nodeType, Node.TEXT_NODE, "Child should be a text
node"); | |
30 assert_equals(e.firstChild.nextSibling, null, "Should have only one child"); | |
31 assert_equals(e.firstChild.data, expectedText); | |
32 assert_not_equals(e.firstChild, oldChild, "Child should be a *new* text node
"); | |
33 }, msg); | 67 }, msg); |
| 68 test(function() { |
| 69 var arr = setupTestDetached(context, plain); |
| 70 assertNewSingleTextNode(arr[0].firstChild, expectedText, arr[1]); |
| 71 }, msg + ", detached"); |
34 } | 72 } |
| 73 |
35 function testHTML(context, plain, expectedHTML, msg) { | 74 function testHTML(context, plain, expectedHTML, msg) { |
36 test(function(){ | 75 test(function(){ |
37 var e = setupTest(context, plain)[0]; | 76 var e = setupTest(context, plain)[0]; |
38 assert_equals(e.innerHTML, expectedHTML); | 77 assert_equals(e.innerHTML, expectedHTML); |
39 var child = e.firstChild; | 78 assertNoEmptyTextChild(e); |
40 while (child) { | |
41 if (child.nodeType === Node.TEXT_NODE) { | |
42 assert_not_equals(child.data, "", "Should not have empty text nodes"); | |
43 } | |
44 child = child.nextSibling; | |
45 } | |
46 }, msg); | 79 }, msg); |
| 80 test(function() { |
| 81 var e = setupTestDetached(context, plain)[0]; |
| 82 assert_equals(e.innerHTML, expectedHTML); |
| 83 assertNoEmptyTextChild(e); |
| 84 }, msg + ", detached"); |
47 } | 85 } |
48 </script> | 86 </script> |
49 <script src="setter-tests.js"></script> | 87 <script src="setter-tests.js"></script> |
OLD | NEW |