/** * File: Test.AnotherWay.xml_eq.js * Adds a xml_eq method to AnotherWay test objects. * */ (function() { /** * Function: createNode * Given a string, try to create an XML DOM node. Throws string messages * on failure. * * Parameters: * text - {String} An XML string. * * Returns: * {DOMElement} An element node. */ function createNode(text) { var index = text.indexOf('<'); if(index > 0) { text = text.substring(index); } var doc; if(window.ActiveXObject && !this.xmldom) { doc = new ActiveXObject("Microsoft.XMLDOM"); try { doc.loadXML(text); } catch(err) { throw "ActiveXObject loadXML failed: " + err; } } else if(window.DOMParser) { try { doc = new DOMParser().parseFromString(text, 'text/xml'); } catch(err) { throw "DOMParser.parseFromString failed"; } if(doc.documentElement && doc.documentElement.nodeName == "parsererror") { throw "DOMParser.parseFromString returned parsererror"; } } else { var req = new XMLHttpRequest(); req.open("GET", "data:text/xml;charset=utf-8," + encodeURIComponent(text), false); if(req.overrideMimeType) { req.overrideMimeType("text/xml"); } req.send(null); doc = req.responseXML; } var root = doc.documentElement; if(!root) { throw "no documentElement"; } return root; } /** * Function assertEqual * Test two objects for equivalence (based on ==). Throw an exception * if not equivalent. * * Parameters: * got - {Object} * expected - {Object} * msg - {String} The message to be thrown. This message will be appended * with ": got {got} but expected {expected}" where got and expected are * replaced with string representations of the above arguments. */ function assertEqual(got, expected, msg) { if(got === undefined) { got = "undefined"; } else if (got === null) { got = "null"; } if(expected === undefined) { expected = "undefined"; } else if (expected === null) { expected = "null"; } if(got != expected) { throw msg + ": got '" + got + "' but expected '" + expected + "'"; } } /** * Function assertElementNodesEqual * Test two element nodes for equivalence. Nodes are considered equivalent * if they are of the same type, have the same name, have the same * namespace prefix and uri, and if all child nodes are equivalent. * Throws a message as exception if not equivalent. * * Parameters: * got - {DOMElement} * expected - {DOMElement} * options - {Object} Optional object for configuring test options. * * Valid options: * prefix - {Boolean} Compare element and attribute * prefixes (namespace uri always tested). Default is false. * includeWhiteSpace - {Boolean} Include whitespace only nodes when * comparing child nodes. Default is false. */ function assertElementNodesEqual(got, expected, options) { var testPrefix = (options && options.prefix === true); // compare types assertEqual(got.nodeType, expected.nodeType, "Node type mismatch"); // compare names var gotName = testPrefix ? got.nodeName : got.nodeName.split(":").pop(); var expName = testPrefix ? expected.nodeName : expected.nodeName.split(":").pop(); assertEqual(gotName, expName, "Node name mismatch"); // for text nodes compare value if(got.nodeType == 3) { assertEqual( got.nodeValue, expected.nodeValue, "Node value mismatch" ); } // for element type nodes compare namespace, attributes, and children else if(got.nodeType == 1) { // test namespace alias and uri if(got.prefix || expected.prefix) { if(testPrefix) { assertEqual( got.prefix, expected.prefix, "Bad prefix for " + got.nodeName ); } } if(got.namespaceURI || expected.namespaceURI) { assertEqual( got.namespaceURI, expected.namespaceURI, "Bad namespaceURI for " + got.nodeName ); } // compare attributes - disregard xmlns given namespace handling above var gotAttrLen = 0; var gotAttr = {}; var expAttrLen = 0; var expAttr = {}; var ga, ea, gn, en; for(var i=0; i