content/test/unit/test_isequalnode.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 function run_test()
michael@0 7 {
michael@0 8 /*
michael@0 9 * NOTE: [i] is not allowed in this test, since it's done via classinfo and
michael@0 10 * we don't have that in xpcshell; the workaround is item(i). Suck.
michael@0 11 */
michael@0 12 init();
michael@0 13
michael@0 14 test_isEqualNode_setAttribute();
michael@0 15 test_isEqualNode_clones();
michael@0 16 test_isEqualNode_variety();
michael@0 17 test_isEqualNode_normalization();
michael@0 18 test_isEqualNode_whitespace();
michael@0 19 test_isEqualNode_namespaces();
michael@0 20 test_isEqualNode_wholeDoc();
michael@0 21
michael@0 22 // XXX should Node.isEqualNode(null) throw or return false?
michael@0 23 //test_isEqualNode_null();
michael@0 24
michael@0 25 }
michael@0 26
michael@0 27 // TEST CODE
michael@0 28
michael@0 29 var doc, root; // cache for use in all tests
michael@0 30
michael@0 31 function init()
michael@0 32 {
michael@0 33 doc = ParseFile("isequalnode_data.xml");
michael@0 34 root = doc.documentElement;
michael@0 35 }
michael@0 36
michael@0 37 function test_isEqualNode_setAttribute()
michael@0 38 {
michael@0 39 // NOTE: 0, 2 are whitespace
michael@0 40 var test1 = doc.getElementById("test_setAttribute");
michael@0 41 var node1 = test1.childNodes.item(1);
michael@0 42 var node2 = test1.childNodes.item(3);
michael@0 43
michael@0 44 check_eq_nodes(node1, node2);
michael@0 45
michael@0 46
michael@0 47 el(node1).setAttribute("bar", "baz");
michael@0 48 check_neq_nodes(node1, node2);
michael@0 49
michael@0 50 el(node2).setAttribute("bar", "baz");
michael@0 51 check_eq_nodes(node1, node2);
michael@0 52
michael@0 53
michael@0 54 // the null namespace is equivalent to no namespace -- section 1.3.3
michael@0 55 // (XML Namespaces) of DOM 3 Core
michael@0 56 node1.setAttributeNS(null, "quux", "17");
michael@0 57 check_neq_nodes(node1, node2);
michael@0 58
michael@0 59 node2.setAttribute("quux", "17");
michael@0 60 check_eq_nodes(node1, node2);
michael@0 61
michael@0 62
michael@0 63 node2.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
michael@0 64 check_neq_nodes(node1, node2);
michael@0 65
michael@0 66 node1.setAttribute("seamonkey", "rheet");
michael@0 67 check_neq_nodes(node1, node2);
michael@0 68
michael@0 69 node1.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
michael@0 70 check_neq_nodes(node1, node2);
michael@0 71
michael@0 72 // this overwrites the namespaced "seamonkey" attribute added to node2
michael@0 73 // earlier, because this simply sets whatever attribute has the fully
michael@0 74 // qualified name "seamonkey" (the setAttributeNS attribute string wasn't
michael@0 75 // prefixed) -- consequently, node1 and node2 are still unequal
michael@0 76 node2.setAttribute("seamonkey", "rheet");
michael@0 77 check_neq_nodes(node1, node2);
michael@0 78 }
michael@0 79
michael@0 80 function test_isEqualNode_clones()
michael@0 81 {
michael@0 82 // tests all elements and attributes in the document
michael@0 83 var all_elts = doc.getElementsByTagName("*");
michael@0 84 for (var i = 0; i < all_elts.length; i++)
michael@0 85 {
michael@0 86 var elt = el(all_elts.item(i));
michael@0 87 check_eq_nodes(elt, elt.cloneNode(true));
michael@0 88
michael@0 89 var attrs = elt.attributes;
michael@0 90 for (var j = 0; j < attrs.length; j++)
michael@0 91 {
michael@0 92 var attr = attrs.item(j);
michael@0 93 check_eq_nodes(attr, attr.cloneNode(true));
michael@0 94 }
michael@0 95 }
michael@0 96
michael@0 97 var elm = doc.createElement("foo");
michael@0 98 check_eq_nodes(elm, elm.cloneNode(true));
michael@0 99 check_eq_nodes(elm, elm.cloneNode(false));
michael@0 100
michael@0 101 elm.setAttribute("fiz", "eit");
michael@0 102 check_eq_nodes(elm, elm.cloneNode(true));
michael@0 103 check_eq_nodes(elm, elm.cloneNode(false));
michael@0 104
michael@0 105 elm.setAttributeNS("http://example.com/", "trendoid", "arthroscope");
michael@0 106 check_eq_nodes(elm, elm.cloneNode(true));
michael@0 107 check_eq_nodes(elm, elm.cloneNode(false));
michael@0 108
michael@0 109 var elm2 = elm.cloneNode(true);
michael@0 110 check_eq_nodes(elm, elm2);
michael@0 111
michael@0 112 const TEXT = "fetishist";
michael@0 113
michael@0 114 elm.textContent = TEXT;
michael@0 115 check_neq_nodes(elm, elm2);
michael@0 116
michael@0 117 check_neq_nodes(elm, elm.cloneNode(false));
michael@0 118 check_eq_nodes(elm, elm.cloneNode(true));
michael@0 119
michael@0 120 elm2.appendChild(doc.createTextNode(TEXT));
michael@0 121 check_eq_nodes(elm, elm2);
michael@0 122
michael@0 123 var att = doc.createAttribute("bar");
michael@0 124 check_eq_nodes(att, att.cloneNode(true));
michael@0 125 check_eq_nodes(att, att.cloneNode(false));
michael@0 126 }
michael@0 127
michael@0 128 function test_isEqualNode_variety()
michael@0 129 {
michael@0 130 const nodes =
michael@0 131 [
michael@0 132 doc.createElement("foo"),
michael@0 133 doc.createElementNS("http://example.com/", "foo"),
michael@0 134 doc.createElementNS("http://example.org/", "foo"),
michael@0 135 doc.createElementNS("http://example.com/", "FOO"),
michael@0 136 doc.createAttribute("foo", "href='biz'"),
michael@0 137 doc.createAttributeNS("http://example.com/", "foo", "href='biz'"),
michael@0 138 doc.createTextNode("foo"),
michael@0 139 doc.createTextNode(" "),
michael@0 140 doc.createTextNode(" "),
michael@0 141 doc.createComment("foo"),
michael@0 142 doc.createProcessingInstruction("foo", "href='biz'"),
michael@0 143 doc.implementation.createDocumentType("foo", "href='biz'", ""),
michael@0 144 doc.implementation.createDocument("http://example.com/", "foo", null),
michael@0 145 doc.createDocumentFragment()
michael@0 146 ];
michael@0 147
michael@0 148 for (var i = 0; i < nodes.length; i++)
michael@0 149 {
michael@0 150 for (var j = i; j < nodes.length; j++)
michael@0 151 {
michael@0 152 if (i == j)
michael@0 153 check_eq_nodes(nodes[i], nodes[j]);
michael@0 154 else
michael@0 155 check_neq_nodes(nodes[i], nodes[j]);
michael@0 156 }
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 function test_isEqualNode_normalization()
michael@0 161 {
michael@0 162 var norm = doc.getElementById("test_normalization");
michael@0 163 var node1 = norm.childNodes.item(1);
michael@0 164 var node2 = norm.childNodes.item(3);
michael@0 165
michael@0 166 check_eq_nodes(node1, node2);
michael@0 167
michael@0 168 node1.appendChild(doc.createTextNode(""));
michael@0 169 check_neq_nodes(node1, node2);
michael@0 170
michael@0 171 node1.normalize();
michael@0 172 check_eq_nodes(node1, node2);
michael@0 173
michael@0 174 node2.appendChild(doc.createTextNode("fun"));
michael@0 175 node2.appendChild(doc.createTextNode("ctor"));
michael@0 176 node1.appendChild(doc.createTextNode("functor"));
michael@0 177 check_neq_nodes(node1, node2);
michael@0 178
michael@0 179 node1.normalize();
michael@0 180 check_neq_nodes(node1, node2);
michael@0 181
michael@0 182 node2.normalize();
michael@0 183 check_eq_nodes(node1, node2);
michael@0 184
michael@0 185 // reset
michael@0 186 while (node1.hasChildNodes())
michael@0 187 node1.removeChild(node1.childNodes.item(0));
michael@0 188 while (node2.hasChildNodes())
michael@0 189 node2.removeChild(node2.childNodes.item(0));
michael@0 190
michael@0 191 // attribute normalization testing
michael@0 192
michael@0 193 var at1 = doc.createAttribute("foo");
michael@0 194 var at2 = doc.createAttribute("foo");
michael@0 195 check_eq_nodes(at1, at2);
michael@0 196
michael@0 197 // Attr.appendChild isn't implemented yet (bug 56758), so don't run this yet
michael@0 198 if (false)
michael@0 199 {
michael@0 200 at1.appendChild(doc.createTextNode("rasp"));
michael@0 201 at2.appendChild(doc.createTextNode("rasp"));
michael@0 202 check_eq_nodes(at1, at2);
michael@0 203
michael@0 204 at1.appendChild(doc.createTextNode(""));
michael@0 205 check_neq_nodes(at1, at2);
michael@0 206
michael@0 207 at1.normalize();
michael@0 208 check_eq_nodes(at1, at2);
michael@0 209
michael@0 210 at1.appendChild(doc.createTextNode("berry"));
michael@0 211 check_neq_nodes(at1, at2);
michael@0 212
michael@0 213 at2.appendChild(doc.createTextNode("ber"));
michael@0 214 check_neq_nodes(at1, at2);
michael@0 215
michael@0 216 at2.appendChild(doc.createTextNode("ry"));
michael@0 217 check_neq_nodes(at1, at2);
michael@0 218
michael@0 219 at1.normalize();
michael@0 220 check_neq_nodes(at1, at2);
michael@0 221
michael@0 222 at2.normalize();
michael@0 223 check_eq_nodes(at1, at2);
michael@0 224 }
michael@0 225
michael@0 226 node1.setAttributeNode(at1);
michael@0 227 check_neq_nodes(node1, node2);
michael@0 228
michael@0 229 node2.setAttributeNode(at2);
michael@0 230 check_eq_nodes(node1, node2);
michael@0 231
michael@0 232 var n1text1 = doc.createTextNode("ratfink");
michael@0 233 var n1elt = doc.createElement("fruitcake");
michael@0 234 var n1text2 = doc.createTextNode("hydrospanner");
michael@0 235
michael@0 236 node1.appendChild(n1text1);
michael@0 237 node1.appendChild(n1elt);
michael@0 238 node1.appendChild(n1text2);
michael@0 239
michael@0 240 check_neq_nodes(node1, node2);
michael@0 241
michael@0 242 var n2text1a = doc.createTextNode("rat");
michael@0 243 var n2text1b = doc.createTextNode("fink");
michael@0 244 var n2elt = doc.createElement("fruitcake");
michael@0 245 var n2text2 = doc.createTextNode("hydrospanner");
michael@0 246
michael@0 247 node2.appendChild(n2text1b);
michael@0 248 node2.appendChild(n2elt);
michael@0 249 node2.appendChild(n2text2);
michael@0 250 check_neq_nodes(node1, node2);
michael@0 251
michael@0 252 node2.insertBefore(n2text1a, n2text1b);
michael@0 253 check_neq_nodes(node1, node2);
michael@0 254
michael@0 255 var tmp_node1 = node1.cloneNode(true);
michael@0 256 tmp_node1.normalize();
michael@0 257 var tmp_node2 = node2.cloneNode(true);
michael@0 258 tmp_node2.normalize();
michael@0 259 check_eq_nodes(tmp_node1, tmp_node2);
michael@0 260
michael@0 261 n2elt.appendChild(doc.createTextNode(""));
michael@0 262 check_neq_nodes(node1, node2);
michael@0 263
michael@0 264 tmp_node1 = node1.cloneNode(true);
michael@0 265 tmp_node1.normalize();
michael@0 266 tmp_node2 = node2.cloneNode(true);
michael@0 267 tmp_node2.normalize();
michael@0 268 check_eq_nodes(tmp_node1, tmp_node2);
michael@0 269
michael@0 270 var typeText1 = doc.createTextNode("type");
michael@0 271 n2elt.appendChild(typeText1);
michael@0 272 tmp_node1 = node1.cloneNode(true);
michael@0 273 tmp_node1.normalize();
michael@0 274 tmp_node2 = node2.cloneNode(true);
michael@0 275 tmp_node2.normalize();
michael@0 276 check_neq_nodes(tmp_node1, tmp_node2);
michael@0 277
michael@0 278 n1elt.appendChild(doc.createTextNode("typedef"));
michael@0 279 tmp_node1 = node1.cloneNode(true);
michael@0 280 tmp_node1.normalize();
michael@0 281 tmp_node2 = node2.cloneNode(true);
michael@0 282 tmp_node2.normalize();
michael@0 283 check_neq_nodes(tmp_node1, tmp_node2);
michael@0 284 check_neq_nodes(n1elt, n2elt);
michael@0 285
michael@0 286 var typeText2 = doc.createTextNode("def");
michael@0 287 n2elt.appendChild(typeText2);
michael@0 288 tmp_node1 = node1.cloneNode(true);
michael@0 289 tmp_node1.normalize();
michael@0 290 tmp_node2 = node2.cloneNode(true);
michael@0 291 tmp_node2.normalize();
michael@0 292 check_eq_nodes(tmp_node1, tmp_node2);
michael@0 293 check_neq_nodes(node1, node2);
michael@0 294
michael@0 295 n2elt.insertBefore(doc.createTextNode(""), typeText2);
michael@0 296 check_neq_nodes(node1, node2);
michael@0 297
michael@0 298 n2elt.insertBefore(doc.createTextNode(""), typeText2);
michael@0 299 check_neq_nodes(node1, node2);
michael@0 300
michael@0 301 n2elt.insertBefore(doc.createTextNode(""), typeText1);
michael@0 302 check_neq_nodes(node1, node2);
michael@0 303
michael@0 304 node1.normalize();
michael@0 305 node2.normalize();
michael@0 306 check_eq_nodes(node1, node2);
michael@0 307 }
michael@0 308
michael@0 309 function test_isEqualNode_whitespace()
michael@0 310 {
michael@0 311 equality_check_kids("test_pi1", true);
michael@0 312 equality_check_kids("test_pi2", true);
michael@0 313 equality_check_kids("test_pi3", false);
michael@0 314 equality_check_kids("test_pi4", true);
michael@0 315 equality_check_kids("test_pi5", true);
michael@0 316
michael@0 317 equality_check_kids("test_elt1", false);
michael@0 318 equality_check_kids("test_elt2", false);
michael@0 319 equality_check_kids("test_elt3", true);
michael@0 320 equality_check_kids("test_elt4", false);
michael@0 321 equality_check_kids("test_elt5", false);
michael@0 322
michael@0 323 equality_check_kids("test_comment1", true);
michael@0 324 equality_check_kids("test_comment2", false);
michael@0 325 equality_check_kids("test_comment3", false);
michael@0 326 equality_check_kids("test_comment4", true);
michael@0 327
michael@0 328 equality_check_kids("test_text1", true);
michael@0 329 equality_check_kids("test_text2", false);
michael@0 330 equality_check_kids("test_text3", false);
michael@0 331
michael@0 332 equality_check_kids("test_cdata1", false);
michael@0 333 equality_check_kids("test_cdata2", true);
michael@0 334 equality_check_kids("test_cdata3", false);
michael@0 335 equality_check_kids("test_cdata4", false);
michael@0 336 equality_check_kids("test_cdata5", false);
michael@0 337 }
michael@0 338
michael@0 339 function test_isEqualNode_namespaces()
michael@0 340 {
michael@0 341 equality_check_kids("test_ns1", false);
michael@0 342 equality_check_kids("test_ns2", false);
michael@0 343
michael@0 344 // XXX want more tests here!
michael@0 345 }
michael@0 346
michael@0 347 function test_isEqualNode_null()
michael@0 348 {
michael@0 349 check_neq_nodes(doc, null);
michael@0 350
michael@0 351 var elts = doc.getElementsByTagName("*");
michael@0 352 for (var i = 0; i < elts.length; i++)
michael@0 353 {
michael@0 354 var elt = elts.item(i);
michael@0 355 check_neq_nodes(elt, null);
michael@0 356
michael@0 357 var attrs = elt.attributes;
michael@0 358 for (var j = 0; j < attrs.length; j++)
michael@0 359 {
michael@0 360 var att = attrs.item(j);
michael@0 361 check_neq_nodes(att, null);
michael@0 362
michael@0 363 for (var k = 0; k < att.childNodes.length; k++)
michael@0 364 {
michael@0 365 check_neq_nodes(att.childNodes.item(k), null);
michael@0 366 }
michael@0 367 }
michael@0 368 }
michael@0 369 }
michael@0 370
michael@0 371 function test_isEqualNode_wholeDoc()
michael@0 372 {
michael@0 373 doc = ParseFile("isequalnode_data.xml");
michael@0 374 var doc2 = ParseFile("isequalnode_data.xml");
michael@0 375 var tw1 =
michael@0 376 doc.createTreeWalker(doc, Components.interfaces.nsIDOMNodeFilter.SHOW_ALL,
michael@0 377 null);
michael@0 378 var tw2 =
michael@0 379 doc2.createTreeWalker(doc2, Components.interfaces.nsIDOMNodeFilter.SHOW_ALL,
michael@0 380 null);
michael@0 381 do {
michael@0 382 check_eq_nodes(tw1.currentNode, tw2.currentNode);
michael@0 383 tw1.nextNode();
michael@0 384 } while(tw2.nextNode());
michael@0 385 }
michael@0 386
michael@0 387 // UTILITY FUNCTIONS
michael@0 388
michael@0 389 function n(node) { return node ? node.QueryInterface(nsIDOMNode) : null; }
michael@0 390 function el(node) { return node ? node.QueryInterface(nsIDOMElement) : null; }
michael@0 391 function at(node) { return node ? node.QueryInterface(nsIDOMAttr) : null; }
michael@0 392
michael@0 393
michael@0 394 // TESTING FUNCTIONS
michael@0 395
michael@0 396 /**
michael@0 397 * Compares the first and third (zero-indexed) child nodes of the element
michael@0 398 * (typically to allow whitespace) referenced by parentId for isEqualNode
michael@0 399 * equality or inequality based on the value of areEqual.
michael@0 400 *
michael@0 401 * Note that this means that the contents of the element referenced by parentId
michael@0 402 * are whitespace-sensitive, and a stray space introduced during an edit to the
michael@0 403 * file could result in a correct but unexpected (in)equality failure.
michael@0 404 */
michael@0 405 function equality_check_kids(parentId, areEqual)
michael@0 406 {
michael@0 407 var parent = doc.getElementById(parentId);
michael@0 408 var kid1 = parent.childNodes.item(1);
michael@0 409 var kid2 = parent.childNodes.item(3);
michael@0 410
michael@0 411 if (areEqual)
michael@0 412 check_eq_nodes(kid1, kid2);
michael@0 413 else
michael@0 414 check_neq_nodes(kid1, kid2);
michael@0 415 }
michael@0 416
michael@0 417 function check_eq_nodes(n1, n2)
michael@0 418 {
michael@0 419 if (n1 && !n1.isEqualNode(n2))
michael@0 420 do_throw(n1 + " should be equal to " + n2);
michael@0 421 if (n2 && !n2.isEqualNode(n1))
michael@0 422 do_throw(n2 + " should be equal to " + n1);
michael@0 423 if (!n1 && !n2)
michael@0 424 do_throw("nodes both null!");
michael@0 425 }
michael@0 426
michael@0 427 function check_neq_nodes(n1, n2)
michael@0 428 {
michael@0 429 if (n1 && n1.isEqualNode(n2))
michael@0 430 do_throw(n1 + " should not be equal to " + n2);
michael@0 431 if (n2 && n2.isEqualNode(n1))
michael@0 432 do_throw(n2 + " should not be equal to " + n1);
michael@0 433 if (!n1 && !n2)
michael@0 434 do_throw("n1 and n2 both null!");
michael@0 435 }

mercurial