CharacterData-replaceData.html (4824B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>CharacterData.replaceData</title> 4 <link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-replacedata"> 5 <link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-data"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <div id="log"></div> 9 <script> 10 function testNode(create, type) { 11 // Step 2. 12 test(function() { 13 var node = create() 14 assert_equals(node.data, "test") 15 16 assert_throws_dom("IndexSizeError", function() { node.replaceData(5, 1, "x") }) 17 assert_throws_dom("IndexSizeError", function() { node.replaceData(5, 0, "") }) 18 assert_throws_dom("IndexSizeError", function() { node.replaceData(-1, 1, "x") }) 19 assert_throws_dom("IndexSizeError", function() { node.replaceData(-1, 0, "") }) 20 assert_equals(node.data, "test") 21 }, type + ".replaceData() with invalid offset") 22 23 // Step 3. 24 test(function() { 25 var node = create() 26 assert_equals(node.data, "test") 27 28 node.replaceData(2, 10, "yo") 29 assert_equals(node.data, "teyo") 30 }, type + ".replaceData() with clamped count") 31 32 test(function() { 33 var node = create() 34 assert_equals(node.data, "test") 35 36 node.replaceData(2, -1, "yo") 37 assert_equals(node.data, "teyo") 38 }, type + ".replaceData() with negative clamped count") 39 40 test(function() { 41 var node = create() 42 assert_equals(node.data, "test") 43 44 node.replaceData(0, 0, "yo") 45 assert_equals(node.data, "yotest") 46 }, type + ".replaceData() before the start") 47 48 test(function() { 49 var node = create() 50 assert_equals(node.data, "test") 51 52 node.replaceData(0, 2, "y") 53 assert_equals(node.data, "yst") 54 }, type + ".replaceData() at the start (shorter)") 55 56 test(function() { 57 var node = create() 58 assert_equals(node.data, "test") 59 60 node.replaceData(0, 2, "yo") 61 assert_equals(node.data, "yost") 62 }, type + ".replaceData() at the start (equal length)") 63 64 test(function() { 65 var node = create() 66 assert_equals(node.data, "test") 67 68 node.replaceData(0, 2, "yoa") 69 assert_equals(node.data, "yoast") 70 }, type + ".replaceData() at the start (longer)") 71 72 test(function() { 73 var node = create() 74 assert_equals(node.data, "test") 75 76 node.replaceData(1, 2, "o") 77 assert_equals(node.data, "tot") 78 }, type + ".replaceData() in the middle (shorter)") 79 80 test(function() { 81 var node = create() 82 assert_equals(node.data, "test") 83 84 node.replaceData(1, 2, "yo") 85 assert_equals(node.data, "tyot") 86 }, type + ".replaceData() in the middle (equal length)") 87 88 test(function() { 89 var node = create() 90 assert_equals(node.data, "test") 91 92 node.replaceData(1, 1, "waddup") 93 assert_equals(node.data, "twaddupst") 94 node.replaceData(1, 1, "yup") 95 assert_equals(node.data, "tyupaddupst") 96 }, type + ".replaceData() in the middle (longer)") 97 98 test(function() { 99 var node = create() 100 assert_equals(node.data, "test") 101 102 node.replaceData(1, 20, "yo") 103 assert_equals(node.data, "tyo") 104 }, type + ".replaceData() at the end (shorter)") 105 106 test(function() { 107 var node = create() 108 assert_equals(node.data, "test") 109 110 node.replaceData(2, 20, "yo") 111 assert_equals(node.data, "teyo") 112 }, type + ".replaceData() at the end (same length)") 113 114 test(function() { 115 var node = create() 116 assert_equals(node.data, "test") 117 118 node.replaceData(4, 20, "yo") 119 assert_equals(node.data, "testyo") 120 }, type + ".replaceData() at the end (longer)") 121 122 test(function() { 123 var node = create() 124 assert_equals(node.data, "test") 125 126 node.replaceData(0, 4, "quux") 127 assert_equals(node.data, "quux") 128 }, type + ".replaceData() the whole string") 129 130 test(function() { 131 var node = create() 132 assert_equals(node.data, "test") 133 134 node.replaceData(0, 4, "") 135 assert_equals(node.data, "") 136 }, type + ".replaceData() with the empty string") 137 138 test(function() { 139 var node = create() 140 assert_equals(node.data, "test") 141 142 node.data = "This is the character data test, append 資料,更多資料"; 143 144 node.replaceData(33, 6, "other"); 145 assert_equals(node.data, "This is the character data test, other 資料,更多資料"); 146 node.replaceData(44, 2, "文字"); 147 assert_equals(node.data, "This is the character data test, other 資料,更多文字"); 148 }, type + ".replaceData() with non-ASCII data") 149 150 test(function() { 151 var node = create() 152 assert_equals(node.data, "test") 153 154 node.data = "🌠 test 🌠 TEST" 155 156 node.replaceData(5, 8, "--"); // Counting UTF-16 code units 157 assert_equals(node.data, "🌠 te--ST"); 158 }, type + ".replaceData() with non-BMP data") 159 } 160 161 testNode(function() { return document.createTextNode("test") }, "Text") 162 testNode(function() { return document.createComment("test") }, "Comment") 163 </script>