OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="../../../resources/testharness.js"></script> |
| 3 <script src="../../../resources/testharnessreport.js"></script> |
| 4 <script> |
| 5 |
| 6 function test_before(nodeName) { |
| 7 var child; |
| 8 var innerHTML; |
| 9 if (nodeName == 'Comment') { |
| 10 child = document.createComment('test'); |
| 11 innerHTML = '<!--test-->'; |
| 12 } else if (nodeName == 'Element') { |
| 13 child = document.createElement('test'); |
| 14 innerHTML = '<test></test>'; |
| 15 } else { |
| 16 child = document.createTextNode('test'); |
| 17 innerHTML = 'test'; |
| 18 } |
| 19 |
| 20 test(function() { |
| 21 var parent = document.createElement('div'); |
| 22 parent.appendChild(child); |
| 23 child.before(); |
| 24 assert_equals(parent.innerHTML, innerHTML); |
| 25 }, nodeName + '.before() without any argument.'); |
| 26 |
| 27 test(function() { |
| 28 var parent = document.createElement('div'); |
| 29 parent.appendChild(child); |
| 30 child.before(null); |
| 31 var expected = 'null' + innerHTML; |
| 32 assert_equals(parent.innerHTML, expected); |
| 33 }, nodeName + '.before() with null as an argument.'); |
| 34 |
| 35 test(function() { |
| 36 var parent = document.createElement('div'); |
| 37 parent.appendChild(child); |
| 38 child.before(undefined); |
| 39 var expected = 'undefined' + innerHTML; |
| 40 assert_equals(parent.innerHTML, expected); |
| 41 }, nodeName + '.before() with undefined as an argument.'); |
| 42 |
| 43 test(function() { |
| 44 var parent = document.createElement('div'); |
| 45 parent.appendChild(child); |
| 46 child.before(''); |
| 47 assert_equals(parent.firstChild.data, ''); |
| 48 }, nodeName + '.before() with the empty string as an argument.'); |
| 49 |
| 50 test(function() { |
| 51 var parent = document.createElement('div'); |
| 52 parent.appendChild(child); |
| 53 child.before('text'); |
| 54 var expected = 'text' + innerHTML; |
| 55 assert_equals(parent.innerHTML, expected); |
| 56 }, nodeName + '.before() with only text as an argument.'); |
| 57 |
| 58 test(function() { |
| 59 var parent = document.createElement('div'); |
| 60 var x = document.createElement('x'); |
| 61 parent.appendChild(child); |
| 62 child.before(x); |
| 63 var expected = '<x></x>' + innerHTML; |
| 64 assert_equals(parent.innerHTML, expected); |
| 65 }, nodeName + '.before() with only one element as an argument.'); |
| 66 |
| 67 test(function() { |
| 68 var parent = document.createElement('div'); |
| 69 var x = document.createElement('x'); |
| 70 parent.appendChild(child); |
| 71 child.before(x, 'text'); |
| 72 var expected = '<x></x>text' + innerHTML; |
| 73 assert_equals(parent.innerHTML, expected); |
| 74 }, nodeName + '.before() with one element and text as arguments.'); |
| 75 |
| 76 test(function() { |
| 77 var parent = document.createElement('div'); |
| 78 parent.appendChild(child); |
| 79 child.before('text', child); |
| 80 var expected = 'text' + innerHTML; |
| 81 assert_equals(parent.innerHTML, expected); |
| 82 }, nodeName + '.before() with context object itself as the argument.'); |
| 83 |
| 84 test(function() { |
| 85 var parent = document.createElement('div'); |
| 86 var x = document.createElement('x'); |
| 87 var y = document.createElement('y'); |
| 88 var z = document.createElement('z'); |
| 89 parent.appendChild(y); |
| 90 parent.appendChild(child); |
| 91 parent.appendChild(x); |
| 92 child.before(x, y, z); |
| 93 var expected = '<x></x><y></y><z></z>' + innerHTML; |
| 94 assert_equals(parent.innerHTML, expected); |
| 95 }, nodeName + '.before() with all siblings of child as arguments.'); |
| 96 |
| 97 test(function() { |
| 98 var parent = document.createElement('div'); |
| 99 var x = document.createElement('x'); |
| 100 var y = document.createElement('y'); |
| 101 parent.appendChild(x); |
| 102 parent.appendChild(y); |
| 103 parent.appendChild(child); |
| 104 child.before(y, x); |
| 105 var expected = '<y></y><x></x>' + innerHTML; |
| 106 assert_equals(parent.innerHTML, expected); |
| 107 }, nodeName + '.before() when pre-insert behaves like prepend.'); |
| 108 |
| 109 test(function() { |
| 110 var parent = document.createElement('div'); |
| 111 var x = document.createElement('x'); |
| 112 parent.appendChild(x); |
| 113 parent.appendChild(document.createTextNode('1')); |
| 114 var y = document.createElement('y'); |
| 115 parent.appendChild(y); |
| 116 parent.appendChild(child); |
| 117 child.before(x, '2'); |
| 118 var expected = '1<y></y><x></x>2' + innerHTML; |
| 119 assert_equals(parent.innerHTML, expected); |
| 120 }, nodeName + '.before() with one sibling of child and text as arguments.'); |
| 121 |
| 122 test(function() { |
| 123 var x = document.createElement('x'); |
| 124 var y = document.createElement('y'); |
| 125 x.before(y); |
| 126 assert_equals(x.previousSibling, null); |
| 127 }, nodeName + '.before() on a child without any parent.'); |
| 128 } |
| 129 |
| 130 test_before('Comment'); |
| 131 test_before('Element'); |
| 132 test_before('Text'); |
| 133 |
| 134 </script> |
| 135 </html> |
OLD | NEW |