dom/xslt/tests/buster/DiffDOM.js

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:542424664205
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 // ----------------------
7 // DiffDOM(node1,node2)
8 // ----------------------
9
10 var isHTML = false;
11
12 function DiffDOM(node1, node2, aIsHTML)
13 {
14 isHTML = aIsHTML;
15 return DiffNodeAndChildren(node1, node2);
16 }
17
18
19 // namespace attributes in the second node are ignored
20 const nsreg = /^xmlns[|:\w]/;
21
22 // This function does the work of DiffDOM by recursively calling
23 // itself to explore the tree
24 function DiffNodeAndChildren(node1, node2)
25 {
26 if (!node1 && !node2)
27 return true;
28 if (!node1 || !node2)
29 return ErrorUp("One of the nodes is null", node1, node2);
30 if (node1.type!=node2.type)
31 return ErrorUp("Different node types", node1, node2);
32
33 var attributes = node2.attributes;
34 if (attributes && attributes.length) {
35 var item, name, ns, value, otherValue;
36 for (var index = 0; index < attributes.length; index++) {
37 item = attributes.item(index);
38 ns = item.namespaceURI;
39 if (ns) {
40 name = item.localName;
41 otherValue = node2.getAttributeNS(ns, name);
42 }
43 else {
44 name = item.nodeName;
45 otherValue = node2.getAttribute(name);
46 }
47 value = item.nodeValue;
48 if (!nsreg.test(name) && otherValue!=value) {
49 return ErrorUp("Different values for attribute", node1, node2);
50 }
51 }
52 }
53 else if (node1.attributes && node1.attributes.length) {
54 return ErrorUp("Different number of attributes", node1, node2);
55 }
56
57 if (isHTML) {
58 if (node1.nodeName.toLowerCase()!=node2.nodeName.toLowerCase())
59 return ErrorUp("Different node names", node1, node2);
60 }
61 else {
62 if (node1.nodeName!=node2.nodeName)
63 return ErrorUp("Different node names", node1, node2);
64 }
65 if (node1.nodeValue!=node2.nodeValue)
66 return ErrorUp("Different node values", node1, node2);
67 if (!isHTML)
68 if (node1.namespaceURI!=node2.namespaceURI)
69 return ErrorUp("Different namespace", node1, node2);
70 if (node1.hasChildNodes() != node2.hasChildNodes())
71 return ErrorUp("Different children", node1, node2);
72 if (node1.childNodes) {
73 if (node1.childNodes.length != node2.childNodes.length)
74 return ErrorUp("Different number of children", node1, node2);
75 for (var child = 0; child < node1.childNodes.length; child++) {
76 if (!DiffNodeAndChildren(node1.childNodes[child],
77 node2.childNodes[child])) {
78 return false;
79 }
80 }
81 }
82 return true;
83 }
84
85 function ErrorUp(errMsg, node1, node2)
86 {
87 dump("Error: "+errMsg+"\n");
88 if (node1) {
89 dump("Node 1: "+node1+", ");
90 if (node1.nodeType == Node.TEXT_NODE)
91 dump("nodeValue: "+node1.nodeValue+"\n");
92 else
93 dump("nodeName: "+node1.namespaceURI+":"+node1.nodeName+"\n");
94 }
95 if (node2) {
96 dump("Node 2: "+node2+", ");
97 if (node2.nodeType == Node.TEXT_NODE)
98 dump("nodeValue: "+node2.nodeValue+"\n");
99 else
100 dump("nodeName: "+node2.namespaceURI+":"+node2.nodeName+"\n");
101 }
102 return false;
103 }

mercurial