1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/tests/buster/DiffDOM.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,103 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// ---------------------- 1.10 +// DiffDOM(node1,node2) 1.11 +// ---------------------- 1.12 + 1.13 +var isHTML = false; 1.14 + 1.15 +function DiffDOM(node1, node2, aIsHTML) 1.16 +{ 1.17 + isHTML = aIsHTML; 1.18 + return DiffNodeAndChildren(node1, node2); 1.19 +} 1.20 + 1.21 + 1.22 +// namespace attributes in the second node are ignored 1.23 +const nsreg = /^xmlns[|:\w]/; 1.24 + 1.25 +// This function does the work of DiffDOM by recursively calling 1.26 +// itself to explore the tree 1.27 +function DiffNodeAndChildren(node1, node2) 1.28 +{ 1.29 + if (!node1 && !node2) 1.30 + return true; 1.31 + if (!node1 || !node2) 1.32 + return ErrorUp("One of the nodes is null", node1, node2); 1.33 + if (node1.type!=node2.type) 1.34 + return ErrorUp("Different node types", node1, node2); 1.35 + 1.36 + var attributes = node2.attributes; 1.37 + if (attributes && attributes.length) { 1.38 + var item, name, ns, value, otherValue; 1.39 + for (var index = 0; index < attributes.length; index++) { 1.40 + item = attributes.item(index); 1.41 + ns = item.namespaceURI; 1.42 + if (ns) { 1.43 + name = item.localName; 1.44 + otherValue = node2.getAttributeNS(ns, name); 1.45 + } 1.46 + else { 1.47 + name = item.nodeName; 1.48 + otherValue = node2.getAttribute(name); 1.49 + } 1.50 + value = item.nodeValue; 1.51 + if (!nsreg.test(name) && otherValue!=value) { 1.52 + return ErrorUp("Different values for attribute", node1, node2); 1.53 + } 1.54 + } 1.55 + } 1.56 + else if (node1.attributes && node1.attributes.length) { 1.57 + return ErrorUp("Different number of attributes", node1, node2); 1.58 + } 1.59 + 1.60 + if (isHTML) { 1.61 + if (node1.nodeName.toLowerCase()!=node2.nodeName.toLowerCase()) 1.62 + return ErrorUp("Different node names", node1, node2); 1.63 + } 1.64 + else { 1.65 + if (node1.nodeName!=node2.nodeName) 1.66 + return ErrorUp("Different node names", node1, node2); 1.67 + } 1.68 + if (node1.nodeValue!=node2.nodeValue) 1.69 + return ErrorUp("Different node values", node1, node2); 1.70 + if (!isHTML) 1.71 + if (node1.namespaceURI!=node2.namespaceURI) 1.72 + return ErrorUp("Different namespace", node1, node2); 1.73 + if (node1.hasChildNodes() != node2.hasChildNodes()) 1.74 + return ErrorUp("Different children", node1, node2); 1.75 + if (node1.childNodes) { 1.76 + if (node1.childNodes.length != node2.childNodes.length) 1.77 + return ErrorUp("Different number of children", node1, node2); 1.78 + for (var child = 0; child < node1.childNodes.length; child++) { 1.79 + if (!DiffNodeAndChildren(node1.childNodes[child], 1.80 + node2.childNodes[child])) { 1.81 + return false; 1.82 + } 1.83 + } 1.84 + } 1.85 + return true; 1.86 +} 1.87 + 1.88 +function ErrorUp(errMsg, node1, node2) 1.89 +{ 1.90 + dump("Error: "+errMsg+"\n"); 1.91 + if (node1) { 1.92 + dump("Node 1: "+node1+", "); 1.93 + if (node1.nodeType == Node.TEXT_NODE) 1.94 + dump("nodeValue: "+node1.nodeValue+"\n"); 1.95 + else 1.96 + dump("nodeName: "+node1.namespaceURI+":"+node1.nodeName+"\n"); 1.97 + } 1.98 + if (node2) { 1.99 + dump("Node 2: "+node2+", "); 1.100 + if (node2.nodeType == Node.TEXT_NODE) 1.101 + dump("nodeValue: "+node2.nodeValue+"\n"); 1.102 + else 1.103 + dump("nodeName: "+node2.namespaceURI+":"+node2.nodeName+"\n"); 1.104 + } 1.105 + return false; 1.106 +}