var wows = require('vows'); var DOMParser = require('xmldom').DOMParser; var XMLSerializer = require('xmldom').XMLSerializer; // Create a Test Suite wows.describe('XML Namespace Parse').addBatch({ // See: http://jsfiddle.net/bigeasy/ShcXP/1/ "Document_getElementsByTagName":function () { var doc = new DOMParser().parseFromString(''); console.assert(doc.getElementsByTagName('*').length == 2); console.assert(doc.documentElement.getElementsByTagName('*').length == 1); }, 'getElementsByTagName': function () { var doc = new DOMParser().parseFromString('' + ''+ '' + '','text/xml'); var childs = doc.documentElement.getElementsByTagName('child'); console.assert(childs.item(0).getAttribute('attr')=="1",childs.item(0)+''); console.assert(childs.item(1).getAttribute('attr')=="2",childs.item(1)+''); console.assert(childs.item(2).getAttribute('attr')=="3",childs.item(2)+''); console.assert(childs.length==3,3,childs.length); var childs = doc.getElementsByTagName('child'); console.assert(childs.item(0).getAttribute('attr')=="1",childs.item(0)+''); console.assert(childs.item(1).getAttribute('attr')=="2",childs.item(1)+''); console.assert(childs.item(2).getAttribute('attr')=="3",childs.item(2)+''); console.assert(childs.length==3,3,childs.length); var childs = doc.documentElement.getElementsByTagName('*'); for(var i=0,buf = [];ifoo'); var entries = feed.documentElement.getElementsByTagName('entry'); console.log(entries[0].nodeName); console.log(feed.documentElement.childNodes.item(0).nodeName); }, 'getElementsByTagNameNS': function () { var doc = new DOMParser().parseFromString('' + ''+ '' + '','text/xml'); var childs = doc.documentElement.getElementsByTagNameNS("http://test.com",'*'); console.assert(childs.length==6,childs.length); var childs = doc.getElementsByTagNameNS("http://test.com",'*'); console.assert(childs.length==7,childs.length); var childs = doc.documentElement.getElementsByTagNameNS("http://test.com",'test'); console.assert(childs.length==3,childs.length); var childs = doc.getElementsByTagNameNS("http://test.com",'test'); console.assert(childs.length==3,childs.length); }, 'getElementById': function () { var doc = new DOMParser().parseFromString('' + '' + '','text/xml'); console.assert(doc.getElementById('root')) console.assert(doc.getElementById('a1').getAttribute('title')=="1",doc.getElementById('a1')); console.assert(doc.getElementById('a2').getAttribute('title')=="2",doc.getElementById('a2')); console.assert(doc.getElementById('a2').getAttribute('title2')=="",doc.getElementById('a2')); }, "append exist child":function(){ var doc = new DOMParser().parseFromString('' + '' + '','text/xml'); var doc1 = doc; var str1=new XMLSerializer().serializeToString(doc); var doc2 = doc1.cloneNode(true); var doc3 = doc1.cloneNode(true); var doc4 = doc1.cloneNode(true); doc3.documentElement.appendChild(doc3.documentElement.lastChild); doc4.documentElement.appendChild(doc4.documentElement.firstChild); var str2=new XMLSerializer().serializeToString(doc2); var str3=new XMLSerializer().serializeToString(doc3); var str4=new XMLSerializer().serializeToString(doc4); console.assert(str1 == str2 && str2 == str3,str3,str1); console.assert(str3 != str4 && str3.length == str4.length,str3); }, "append exist other child":function(){ var doc = new DOMParser().parseFromString('' + '' + '','text/xml'); var doc1 = doc; var str1=new XMLSerializer().serializeToString(doc); var doc2 = doc1.cloneNode(true); console.assert(doc2.documentElement.lastChild.childNodes.length == 0); doc2.documentElement.appendChild(doc2.documentElement.firstChild.firstChild); var str2=new XMLSerializer().serializeToString(doc2); console.assert(doc2.documentElement.lastChild.childNodes.length == 1); console.assert(str1 != str2 && str1.length != str2.length,str3); var doc3 = new DOMParser().parseFromString(str2,'text/xml'); doc3.documentElement.firstChild.appendChild(doc3.documentElement.lastChild); var str3 = new XMLSerializer().serializeToString(doc3); console.assert(str1 == str3); }, "set textContent":function() { var doc = new DOMParser().parseFromString(''); var a = doc.documentElement.firstChild; var b = a.nextSibling; a.textContent = 'hello'; console.assert(doc.documentElement.toString() == 'hello'); b.textContent = 'there'; console.assert(doc.documentElement.toString() == 'hellothere'); b.textContent = ''; console.assert(doc.documentElement.toString() == 'hello'); doc.documentElement.textContent = 'bye'; console.assert(doc.documentElement.toString() == 'bye'); }, "nested append failed":function(){ }, "self append failed":function(){ } }).run(); // Run it