Node-textContent.html (9526B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Node.textContent</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id="log"></div> 7 <script> 8 // XXX mutation observers? 9 // XXX Range gravitation? 10 11 var documents, doctypes; 12 setup(function() { 13 documents = [ 14 [document, "parser"], 15 [document.implementation.createDocument("", "test", null), "createDocument"], 16 [document.implementation.createHTMLDocument("title"), "createHTMLDocument"], 17 ] 18 doctypes = [ 19 [document.doctype, "parser"], 20 [document.implementation.createDocumentType("x", "", ""), "script"], 21 ] 22 }) 23 24 // Getting 25 // DocumentFragment, Element: 26 test(function() { 27 var element = document.createElement("div") 28 assert_equals(element.textContent, "") 29 }, "For an empty Element, textContent should be the empty string") 30 31 test(function() { 32 assert_equals(document.createDocumentFragment().textContent, "") 33 }, "For an empty DocumentFragment, textContent should be the empty string") 34 35 test(function() { 36 var el = document.createElement("div") 37 el.appendChild(document.createComment(" abc ")) 38 el.appendChild(document.createTextNode("\tDEF\t")) 39 el.appendChild(document.createProcessingInstruction("x", " ghi ")) 40 assert_equals(el.textContent, "\tDEF\t") 41 }, "Element with children") 42 43 test(function() { 44 var el = document.createElement("div") 45 var child = document.createElement("div") 46 el.appendChild(child) 47 child.appendChild(document.createComment(" abc ")) 48 child.appendChild(document.createTextNode("\tDEF\t")) 49 child.appendChild(document.createProcessingInstruction("x", " ghi ")) 50 assert_equals(el.textContent, "\tDEF\t") 51 }, "Element with descendants") 52 53 test(function() { 54 var df = document.createDocumentFragment() 55 df.appendChild(document.createComment(" abc ")) 56 df.appendChild(document.createTextNode("\tDEF\t")) 57 df.appendChild(document.createProcessingInstruction("x", " ghi ")) 58 assert_equals(df.textContent, "\tDEF\t") 59 }, "DocumentFragment with children") 60 61 test(function() { 62 var df = document.createDocumentFragment() 63 var child = document.createElement("div") 64 df.appendChild(child) 65 child.appendChild(document.createComment(" abc ")) 66 child.appendChild(document.createTextNode("\tDEF\t")) 67 child.appendChild(document.createProcessingInstruction("x", " ghi ")) 68 assert_equals(df.textContent, "\tDEF\t") 69 }, "DocumentFragment with descendants") 70 71 // Text, ProcessingInstruction, Comment: 72 test(function() { 73 assert_equals(document.createTextNode("").textContent, "") 74 }, "For an empty Text, textContent should be the empty string") 75 76 test(function() { 77 assert_equals(document.createProcessingInstruction("x", "").textContent, "") 78 }, "For an empty ProcessingInstruction, textContent should be the empty string") 79 80 test(function() { 81 assert_equals(document.createComment("").textContent, "") 82 }, "For an empty Comment, textContent should be the empty string") 83 84 test(function() { 85 assert_equals(document.createTextNode("abc").textContent, "abc") 86 }, "For a Text with data, textContent should be that data") 87 88 test(function() { 89 assert_equals(document.createProcessingInstruction("x", "abc").textContent, 90 "abc") 91 }, "For a ProcessingInstruction with data, textContent should be that data") 92 93 test(function() { 94 assert_equals(document.createComment("abc").textContent, "abc") 95 }, "For a Comment with data, textContent should be that data") 96 97 // Any other node: 98 documents.forEach(function(argument) { 99 var doc = argument[0], creator = argument[1] 100 test(function() { 101 assert_equals(doc.textContent, null) 102 }, "For Documents created by " + creator + ", textContent should be null") 103 }) 104 105 doctypes.forEach(function(argument) { 106 var doctype = argument[0], creator = argument[1] 107 test(function() { 108 assert_equals(doctype.textContent, null) 109 }, "For DocumentType created by " + creator + ", textContent should be null") 110 }) 111 112 // Setting 113 // DocumentFragment, Element: 114 var testArgs = [ 115 [null, null], 116 [undefined, null], 117 ["", null], 118 [42, "42"], 119 ["abc", "abc"], 120 ["<b>xyz<\/b>", "<b>xyz<\/b>"], 121 ["d\0e", "d\0e"] 122 // XXX unpaired surrogate? 123 ] 124 testArgs.forEach(function(aValue) { 125 var argument = aValue[0], expectation = aValue[1] 126 var check = function(aElementOrDocumentFragment) { 127 if (expectation === null) { 128 assert_equals(aElementOrDocumentFragment.textContent, "") 129 assert_equals(aElementOrDocumentFragment.firstChild, null) 130 } else { 131 assert_equals(aElementOrDocumentFragment.textContent, expectation) 132 assert_equals(aElementOrDocumentFragment.childNodes.length, 1, 133 "Should have one child") 134 var firstChild = aElementOrDocumentFragment.firstChild 135 assert_true(firstChild instanceof Text, "child should be a Text") 136 assert_equals(firstChild.data, expectation) 137 } 138 } 139 140 test(function() { 141 var el = document.createElement("div") 142 el.textContent = argument 143 check(el) 144 }, "Element without children set to " + format_value(argument)) 145 146 test(function() { 147 var el = document.createElement("div") 148 var text = el.appendChild(document.createTextNode("")) 149 el.textContent = argument 150 check(el) 151 assert_equals(text.parentNode, null, 152 "Preexisting Text should have been removed") 153 }, "Element with empty text node as child set to " + format_value(argument)) 154 155 test(function() { 156 var el = document.createElement("div") 157 el.appendChild(document.createComment(" abc ")) 158 el.appendChild(document.createTextNode("\tDEF\t")) 159 el.appendChild(document.createProcessingInstruction("x", " ghi ")) 160 el.textContent = argument 161 check(el) 162 }, "Element with children set to " + format_value(argument)) 163 164 test(function() { 165 var el = document.createElement("div") 166 var child = document.createElement("div") 167 el.appendChild(child) 168 child.appendChild(document.createComment(" abc ")) 169 child.appendChild(document.createTextNode("\tDEF\t")) 170 child.appendChild(document.createProcessingInstruction("x", " ghi ")) 171 el.textContent = argument 172 check(el) 173 assert_equals(child.childNodes.length, 3, 174 "Should not have changed the internal structure of the removed nodes.") 175 }, "Element with descendants set to " + format_value(argument)) 176 177 test(function() { 178 var df = document.createDocumentFragment() 179 df.textContent = argument 180 check(df) 181 }, "DocumentFragment without children set to " + format_value(argument)) 182 183 test(function() { 184 var df = document.createDocumentFragment() 185 var text = df.appendChild(document.createTextNode("")) 186 df.textContent = argument 187 check(df) 188 assert_equals(text.parentNode, null, 189 "Preexisting Text should have been removed") 190 }, "DocumentFragment with empty text node as child set to " + format_value(argument)) 191 192 test(function() { 193 var df = document.createDocumentFragment() 194 df.appendChild(document.createComment(" abc ")) 195 df.appendChild(document.createTextNode("\tDEF\t")) 196 df.appendChild(document.createProcessingInstruction("x", " ghi ")) 197 df.textContent = argument 198 check(df) 199 }, "DocumentFragment with children set to " + format_value(argument)) 200 201 test(function() { 202 var df = document.createDocumentFragment() 203 var child = document.createElement("div") 204 df.appendChild(child) 205 child.appendChild(document.createComment(" abc ")) 206 child.appendChild(document.createTextNode("\tDEF\t")) 207 child.appendChild(document.createProcessingInstruction("x", " ghi ")) 208 df.textContent = argument 209 check(df) 210 assert_equals(child.childNodes.length, 3, 211 "Should not have changed the internal structure of the removed nodes.") 212 }, "DocumentFragment with descendants set to " + format_value(argument)) 213 }) 214 215 // Text, ProcessingInstruction, Comment: 216 test(function() { 217 var text = document.createTextNode("abc") 218 text.textContent = "def" 219 assert_equals(text.textContent, "def") 220 assert_equals(text.data, "def") 221 }, "For a Text, textContent should set the data") 222 223 test(function() { 224 var pi = document.createProcessingInstruction("x", "abc") 225 pi.textContent = "def" 226 assert_equals(pi.textContent, "def") 227 assert_equals(pi.data, "def") 228 assert_equals(pi.target, "x") 229 }, "For a ProcessingInstruction, textContent should set the data") 230 231 test(function() { 232 var comment = document.createComment("abc") 233 comment.textContent = "def" 234 assert_equals(comment.textContent, "def") 235 assert_equals(comment.data, "def") 236 }, "For a Comment, textContent should set the data") 237 238 // Any other node: 239 documents.forEach(function(argument) { 240 var doc = argument[0], creator = argument[1] 241 test(function() { 242 var root = doc.documentElement 243 doc.textContent = "a" 244 assert_equals(doc.textContent, null) 245 assert_equals(doc.documentElement, root) 246 }, "For Documents created by " + creator + ", setting textContent should do nothing") 247 }) 248 249 doctypes.forEach(function(argument) { 250 var doctype = argument[0], creator = argument[1] 251 test(function() { 252 var props = { 253 name: doctype.name, 254 publicId: doctype.publicId, 255 systemId: doctype.systemId, 256 } 257 doctype.textContent = "b" 258 assert_equals(doctype.textContent, null) 259 assert_equals(doctype.name, props.name, "name should not change") 260 assert_equals(doctype.publicId, props.publicId, "publicId should not change") 261 assert_equals(doctype.systemId, props.systemId, "systemId should not change") 262 }, "For DocumentType created by " + creator + ", setting textContent should do nothing") 263 }) 264 265 </script>