michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // ---------------------- michael@0: // DiffDOM(node1,node2) michael@0: // ---------------------- michael@0: michael@0: var isHTML = false; michael@0: michael@0: function DiffDOM(node1, node2, aIsHTML) michael@0: { michael@0: isHTML = aIsHTML; michael@0: return DiffNodeAndChildren(node1, node2); michael@0: } michael@0: michael@0: michael@0: // namespace attributes in the second node are ignored michael@0: const nsreg = /^xmlns[|:\w]/; michael@0: michael@0: // This function does the work of DiffDOM by recursively calling michael@0: // itself to explore the tree michael@0: function DiffNodeAndChildren(node1, node2) michael@0: { michael@0: if (!node1 && !node2) michael@0: return true; michael@0: if (!node1 || !node2) michael@0: return ErrorUp("One of the nodes is null", node1, node2); michael@0: if (node1.type!=node2.type) michael@0: return ErrorUp("Different node types", node1, node2); michael@0: michael@0: var attributes = node2.attributes; michael@0: if (attributes && attributes.length) { michael@0: var item, name, ns, value, otherValue; michael@0: for (var index = 0; index < attributes.length; index++) { michael@0: item = attributes.item(index); michael@0: ns = item.namespaceURI; michael@0: if (ns) { michael@0: name = item.localName; michael@0: otherValue = node2.getAttributeNS(ns, name); michael@0: } michael@0: else { michael@0: name = item.nodeName; michael@0: otherValue = node2.getAttribute(name); michael@0: } michael@0: value = item.nodeValue; michael@0: if (!nsreg.test(name) && otherValue!=value) { michael@0: return ErrorUp("Different values for attribute", node1, node2); michael@0: } michael@0: } michael@0: } michael@0: else if (node1.attributes && node1.attributes.length) { michael@0: return ErrorUp("Different number of attributes", node1, node2); michael@0: } michael@0: michael@0: if (isHTML) { michael@0: if (node1.nodeName.toLowerCase()!=node2.nodeName.toLowerCase()) michael@0: return ErrorUp("Different node names", node1, node2); michael@0: } michael@0: else { michael@0: if (node1.nodeName!=node2.nodeName) michael@0: return ErrorUp("Different node names", node1, node2); michael@0: } michael@0: if (node1.nodeValue!=node2.nodeValue) michael@0: return ErrorUp("Different node values", node1, node2); michael@0: if (!isHTML) michael@0: if (node1.namespaceURI!=node2.namespaceURI) michael@0: return ErrorUp("Different namespace", node1, node2); michael@0: if (node1.hasChildNodes() != node2.hasChildNodes()) michael@0: return ErrorUp("Different children", node1, node2); michael@0: if (node1.childNodes) { michael@0: if (node1.childNodes.length != node2.childNodes.length) michael@0: return ErrorUp("Different number of children", node1, node2); michael@0: for (var child = 0; child < node1.childNodes.length; child++) { michael@0: if (!DiffNodeAndChildren(node1.childNodes[child], michael@0: node2.childNodes[child])) { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function ErrorUp(errMsg, node1, node2) michael@0: { michael@0: dump("Error: "+errMsg+"\n"); michael@0: if (node1) { michael@0: dump("Node 1: "+node1+", "); michael@0: if (node1.nodeType == Node.TEXT_NODE) michael@0: dump("nodeValue: "+node1.nodeValue+"\n"); michael@0: else michael@0: dump("nodeName: "+node1.namespaceURI+":"+node1.nodeName+"\n"); michael@0: } michael@0: if (node2) { michael@0: dump("Node 2: "+node2+", "); michael@0: if (node2.nodeType == Node.TEXT_NODE) michael@0: dump("nodeValue: "+node2.nodeValue+"\n"); michael@0: else michael@0: dump("nodeName: "+node2.namespaceURI+":"+node2.nodeName+"\n"); michael@0: } michael@0: return false; michael@0: }