dom/xslt/tests/buster/DiffDOM.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: Java; tab-width: 2; 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 // ----------------------
michael@0 7 // DiffDOM(node1,node2)
michael@0 8 // ----------------------
michael@0 9
michael@0 10 var isHTML = false;
michael@0 11
michael@0 12 function DiffDOM(node1, node2, aIsHTML)
michael@0 13 {
michael@0 14 isHTML = aIsHTML;
michael@0 15 return DiffNodeAndChildren(node1, node2);
michael@0 16 }
michael@0 17
michael@0 18
michael@0 19 // namespace attributes in the second node are ignored
michael@0 20 const nsreg = /^xmlns[|:\w]/;
michael@0 21
michael@0 22 // This function does the work of DiffDOM by recursively calling
michael@0 23 // itself to explore the tree
michael@0 24 function DiffNodeAndChildren(node1, node2)
michael@0 25 {
michael@0 26 if (!node1 && !node2)
michael@0 27 return true;
michael@0 28 if (!node1 || !node2)
michael@0 29 return ErrorUp("One of the nodes is null", node1, node2);
michael@0 30 if (node1.type!=node2.type)
michael@0 31 return ErrorUp("Different node types", node1, node2);
michael@0 32
michael@0 33 var attributes = node2.attributes;
michael@0 34 if (attributes && attributes.length) {
michael@0 35 var item, name, ns, value, otherValue;
michael@0 36 for (var index = 0; index < attributes.length; index++) {
michael@0 37 item = attributes.item(index);
michael@0 38 ns = item.namespaceURI;
michael@0 39 if (ns) {
michael@0 40 name = item.localName;
michael@0 41 otherValue = node2.getAttributeNS(ns, name);
michael@0 42 }
michael@0 43 else {
michael@0 44 name = item.nodeName;
michael@0 45 otherValue = node2.getAttribute(name);
michael@0 46 }
michael@0 47 value = item.nodeValue;
michael@0 48 if (!nsreg.test(name) && otherValue!=value) {
michael@0 49 return ErrorUp("Different values for attribute", node1, node2);
michael@0 50 }
michael@0 51 }
michael@0 52 }
michael@0 53 else if (node1.attributes && node1.attributes.length) {
michael@0 54 return ErrorUp("Different number of attributes", node1, node2);
michael@0 55 }
michael@0 56
michael@0 57 if (isHTML) {
michael@0 58 if (node1.nodeName.toLowerCase()!=node2.nodeName.toLowerCase())
michael@0 59 return ErrorUp("Different node names", node1, node2);
michael@0 60 }
michael@0 61 else {
michael@0 62 if (node1.nodeName!=node2.nodeName)
michael@0 63 return ErrorUp("Different node names", node1, node2);
michael@0 64 }
michael@0 65 if (node1.nodeValue!=node2.nodeValue)
michael@0 66 return ErrorUp("Different node values", node1, node2);
michael@0 67 if (!isHTML)
michael@0 68 if (node1.namespaceURI!=node2.namespaceURI)
michael@0 69 return ErrorUp("Different namespace", node1, node2);
michael@0 70 if (node1.hasChildNodes() != node2.hasChildNodes())
michael@0 71 return ErrorUp("Different children", node1, node2);
michael@0 72 if (node1.childNodes) {
michael@0 73 if (node1.childNodes.length != node2.childNodes.length)
michael@0 74 return ErrorUp("Different number of children", node1, node2);
michael@0 75 for (var child = 0; child < node1.childNodes.length; child++) {
michael@0 76 if (!DiffNodeAndChildren(node1.childNodes[child],
michael@0 77 node2.childNodes[child])) {
michael@0 78 return false;
michael@0 79 }
michael@0 80 }
michael@0 81 }
michael@0 82 return true;
michael@0 83 }
michael@0 84
michael@0 85 function ErrorUp(errMsg, node1, node2)
michael@0 86 {
michael@0 87 dump("Error: "+errMsg+"\n");
michael@0 88 if (node1) {
michael@0 89 dump("Node 1: "+node1+", ");
michael@0 90 if (node1.nodeType == Node.TEXT_NODE)
michael@0 91 dump("nodeValue: "+node1.nodeValue+"\n");
michael@0 92 else
michael@0 93 dump("nodeName: "+node1.namespaceURI+":"+node1.nodeName+"\n");
michael@0 94 }
michael@0 95 if (node2) {
michael@0 96 dump("Node 2: "+node2+", ");
michael@0 97 if (node2.nodeType == Node.TEXT_NODE)
michael@0 98 dump("nodeValue: "+node2.nodeValue+"\n");
michael@0 99 else
michael@0 100 dump("nodeName: "+node2.namespaceURI+":"+node2.nodeName+"\n");
michael@0 101 }
michael@0 102 return false;
michael@0 103 }

mercurial