!import
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is TransforMiiX XSLT processor code.
16 *
17 * The Initial Developer of the Original Code is
18 * Axel Hecht.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Axel Hecht <axel@pike.org> (Original Author)
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 // ----------------------
40 // DiffDOM(node1,node2)
41 // ----------------------
42
43 var isHTML = false;
44
DiffDOM
45 function DiffDOM(node1, node2, aIsHTML)
46 {
47 isHTML = aIsHTML;
48 return DiffNodeAndChildren(node1, node2);
49 }
50
51
52 // namespace attributes in the second node are ignored
53 const nsreg = /^xmlns[|:\w]/;
54
55 // This function does the work of DiffDOM by recursively calling
56 // itself to explore the tree
DiffNodeAndChildren
57 function DiffNodeAndChildren(node1, node2)
58 {
59 if (!node1 && !node2)
60 return true;
61 if (!node1 || !node2)
62 return ErrorUp("One of the nodes is null", node1, node2);
63 if (node1.type!=node2.type)
64 return ErrorUp("Different node types", node1, node2);
65
66 var attributes = node2.attributes;
67 if (attributes && attributes.length) {
68 var item, name, ns, value, otherValue;
69 for (var index = 0; index < attributes.length; index++) {
70 item = attributes.item(index);
71 ns = item.namespaceURI;
72 if (ns) {
73 name = item.localName;
74 otherValue = node2.getAttributeNS(ns, name);
75 }
76 else {
77 name = item.nodeName;
78 otherValue = node2.getAttribute(name);
79 }
80 value = item.nodeValue;
81 if (!nsreg.test(name) && otherValue!=value) {
82 return ErrorUp("Different values for attribute", node1, node2);
83 }
84 }
85 }
86 else if (node1.attributes && node1.attributes.length) {
87 return ErrorUp("Different number of attributes", node1, node2);
88 }
89
90 if (isHTML) {
91 if (node1.nodeName.toLowerCase()!=node2.nodeName.toLowerCase())
92 return ErrorUp("Different node names", node1, node2);
93 }
94 else {
95 if (node1.nodeName!=node2.nodeName)
96 return ErrorUp("Different node names", node1, node2);
97 }
98 if (node1.nodeValue!=node2.nodeValue)
99 return ErrorUp("Different node values", node1, node2);
100 if (!isHTML)
101 if (node1.namespaceURI!=node2.namespaceURI)
102 return ErrorUp("Different namespace", node1, node2);
103 if (node1.hasChildNodes() != node2.hasChildNodes())
104 return ErrorUp("Different children", node1, node2);
105 if (node1.childNodes) {
106 if (node1.childNodes.length != node2.childNodes.length)
107 return ErrorUp("Different number of children", node1, node2);
108 for (var child = 0; child < node1.childNodes.length; child++) {
109 if (!DiffNodeAndChildren(node1.childNodes[child],
110 node2.childNodes[child])) {
111 return false;
112 }
113 }
114 }
115 return true;
116 }
117
ErrorUp
118 function ErrorUp(errMsg, node1, node2)
119 {
120 dump("Error: "+errMsg+"\n");
121 if (node1) {
122 dump("Node 1: "+node1+", ");
123 if (node1.nodeType == Node.TEXT_NODE)
124 dump("nodeValue: "+node1.nodeValue+"\n");
125 else
126 dump("nodeName: "+node1.namespaceURI+":"+node1.nodeName+"\n");
127 }
128 if (node2) {
129 dump("Node 2: "+node2+", ");
130 if (node2.nodeType == Node.TEXT_NODE)
131 dump("nodeValue: "+node2.nodeValue+"\n");
132 else
133 dump("nodeName: "+node2.namespaceURI+":"+node2.nodeName+"\n");
134 }
135 return false;
136 }