Comparing version 0.1.7 to 0.1.8
208
dom.js
@@ -147,36 +147,38 @@ /* | ||
while(i--){ | ||
if(list[i] == node){return i} | ||
if(list[i] === node){return i} | ||
} | ||
} | ||
function _addNamedNode(list,node,old){ | ||
if(old){ | ||
list[_findNodeIndex(list,old)] = node; | ||
function _addNamedNode(el,list,newAttr,oldAttr){ | ||
if(oldAttr){ | ||
list[_findNodeIndex(list,oldAttr)] = newAttr; | ||
}else{ | ||
list[list.length++] = node; | ||
list[list.length++] = newAttr; | ||
} | ||
var el = list._ownerElement; | ||
var doc = el && el.ownerDocument; | ||
if(doc){ | ||
//doc._setOwnerElement(node,el); | ||
node.ownerElement = el; | ||
//_updateAttribute(el,node); | ||
if(el){ | ||
newAttr.ownerElement = el; | ||
var doc = el.ownerDocument; | ||
if(doc){ | ||
oldAttr && _onRemoveAttribute(doc,el,oldAttr); | ||
_onAddAttribute(doc,el,newAttr); | ||
} | ||
} | ||
return old || null; | ||
} | ||
function _removeNamedNode(list,node){ | ||
var i = list.length; | ||
var lastIndex = i-1; | ||
while(i--){ | ||
var c = list[i]; | ||
if(node === c){ | ||
var old = c; | ||
while(i<lastIndex){ | ||
list[i] = list[++i] | ||
function _removeNamedNode(el,list,attr){ | ||
var i = _findNodeIndex(list,attr); | ||
if(i>=0){ | ||
var lastIndex = list.length-1 | ||
while(i<lastIndex){ | ||
list[i] = list[++i] | ||
} | ||
list.length = lastIndex; | ||
if(el){ | ||
var doc = el.ownerDocument; | ||
if(doc){ | ||
_onRemoveAttribute(doc,el,attr); | ||
attr.ownerElement = null; | ||
} | ||
list.length = lastIndex; | ||
node.ownerElement = null; | ||
return old; | ||
} | ||
}else{ | ||
throw DOMException(NOT_FOUND_ERR,new Error()) | ||
} | ||
@@ -193,16 +195,26 @@ } | ||
while(i--){ | ||
var node = this[i]; | ||
if(node.nodeName == key){ | ||
return node; | ||
var attr = this[i]; | ||
if(attr.nodeName == key){ | ||
return attr; | ||
} | ||
} | ||
}, | ||
setNamedItem: function(node) { | ||
var old = this.getNamedItemNS(node.nodeName); | ||
return _addNamedNode(this,node,old); | ||
setNamedItem: function(attr) { | ||
var el = attr.ownerElement; | ||
if(el && el!=this._ownerElement){ | ||
el.removeAttributeNode(attr); | ||
} | ||
var oldAttr = this.getNamedItem(attr.nodeName); | ||
_addNamedNode(this._ownerElement,this,attr,oldAttr); | ||
return oldAttr; | ||
}, | ||
/* returns Node */ | ||
setNamedItemNS: function(node) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR | ||
var old = this.getNamedItemNS(node.namespaceURI,node.localName); | ||
return _addNamedNode(this,node,old); | ||
setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR | ||
var el = attr.ownerElement; | ||
if(el && el!=this._ownerElement){ | ||
el.removeAttributeNode(attr); | ||
} | ||
oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName); | ||
_addNamedNode(this._ownerElement,this,attr,oldAttr); | ||
return oldAttr; | ||
}, | ||
@@ -212,12 +224,15 @@ | ||
removeNamedItem: function(key) { | ||
var node = this.getNamedItem(key); | ||
if(node){ | ||
_removeNamedNode(this,node); | ||
}else{ | ||
throw DOMException(NOT_FOUND_ERR,new Error()) | ||
} | ||
var attr = this.getNamedItem(key); | ||
_removeNamedNode(this._ownerElement,this,attr); | ||
return attr; | ||
},// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR | ||
//for level2 | ||
removeNamedItemNS:function(namespaceURI,localName){ | ||
var attr = this.getNamedItemNS(namespaceURI,localName); | ||
_removeNamedNode(this._ownerElement,this,attr); | ||
return attr; | ||
}, | ||
getNamedItemNS: function(namespaceURI, localName) { | ||
@@ -232,10 +247,2 @@ var i = this.length; | ||
return null; | ||
}, | ||
removeNamedItemNS:function(namespaceURI,localName){ | ||
var node = this.getNamedItemNS(namespaceURI,localName); | ||
if(node){ | ||
_removeNamedNode(this,node); | ||
}else{ | ||
throw DOMException(NOT_FOUND_ERR,new Error()) | ||
} | ||
} | ||
@@ -342,2 +349,3 @@ }; | ||
var child = this.firstChild; | ||
var i = 0; | ||
while(child){ | ||
@@ -352,2 +360,5 @@ var next = child.nextSibling; | ||
} | ||
if(1000 == i++){ | ||
console.log(this.lastChild .nextSibling==null) | ||
} | ||
} | ||
@@ -473,25 +484,17 @@ }, | ||
*/ | ||
function _removeChild(parentNode,oldChild){ | ||
var previous = null,child= parentNode.firstChild; | ||
while(child){ | ||
var next = child.nextSibling; | ||
if(child === oldChild){ | ||
oldChild.parentNode = null;//remove it as a flag of not in document | ||
//_visitNode(oldNode,function(node){oldChild.ownerDocument = null;})//can not remove ownerDocument | ||
if(previous){ | ||
previous.nextSibling = next; | ||
}else{ | ||
parentNode.firstChild = next; | ||
} | ||
if(next){ | ||
next.previousSibling = previous; | ||
}else{ | ||
parentNode.lastChild = previous; | ||
} | ||
_onUpdateChild(parentNode.ownerDocument,parentNode); | ||
return child; | ||
} | ||
previous = child; | ||
child = next; | ||
function _removeChild(parentNode,child){ | ||
var previous = child.previousSibling; | ||
var next = child.nextSibling; | ||
if(previous){ | ||
previous.nextSibling = next; | ||
}else{ | ||
parentNode.firstChild = next | ||
} | ||
if(next){ | ||
next.previousSibling = previous; | ||
}else{ | ||
parentNode.lastChild = previous; | ||
} | ||
_onUpdateChild(parentNode.ownerDocument,parentNode); | ||
return child; | ||
} | ||
@@ -501,3 +504,3 @@ /** | ||
*/ | ||
function _insertBefore(parentNode,newChild,refChild){ | ||
function _insertBefore(parentNode,newChild,nextChild){ | ||
var cp = newChild.parentNode; | ||
@@ -513,9 +516,8 @@ if(cp){ | ||
} | ||
if(refChild == null){ | ||
var pre = parentNode.lastChild; | ||
parentNode.lastChild = newLast; | ||
}else{ | ||
var pre = refChild.previousSibling; | ||
newLast.nextSibling = refChild.nextSibling; | ||
} | ||
var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild; | ||
newFirst.previousSibling = pre; | ||
newLast.nextSibling = nextChild; | ||
if(pre){ | ||
@@ -526,3 +528,5 @@ pre.nextSibling = newFirst; | ||
} | ||
newFirst.previousSibling = pre; | ||
if(nextChild == null){ | ||
parentNode.lastChild = newLast; | ||
} | ||
do{ | ||
@@ -532,15 +536,15 @@ newFirst.parentNode = parentNode; | ||
_onUpdateChild(parentNode.ownerDocument||parentNode,parentNode); | ||
//console.log(parentNode.lastChild.nextSibling == null) | ||
} | ||
function _appendSingleChild(parentNode,newChild){ | ||
var pre = parentNode.lastChild; | ||
var cp = newChild.parentNode; | ||
if(cp){ | ||
if(cp !== parentChild){ | ||
cp.removeChild(newChild);//remove and update | ||
newChild.parentNode = parentNode; | ||
} | ||
}else{ | ||
newChild.parentNode = parentNode; | ||
var pre = parentNode.lastChild; | ||
cp.removeChild(newChild);//remove and update | ||
var pre = parentNode.lastChild; | ||
} | ||
var pre = parentNode.lastChild; | ||
newChild.parentNode = parentNode; | ||
newChild.previousSibling = pre; | ||
newChild.nextSibling = null; | ||
if(pre){ | ||
@@ -553,2 +557,3 @@ pre.nextSibling = newChild; | ||
_onUpdateChild(parentNode.ownerDocument,parentNode,newChild); | ||
//console.log("__aa",parentNode.lastChild.nextSibling == null) | ||
} | ||
@@ -733,21 +738,10 @@ Document.prototype = { | ||
setAttributeNode : function(newAttr){ | ||
var old = this.attributes.setNamedItem(newAttr); | ||
old && _onRemoveAttribute(this.ownerDocument,this,old); | ||
_onAddAttribute(this.ownerDocument,this,newAttr); | ||
return old; | ||
return this.attributes.setNamedItem(newAttr); | ||
}, | ||
setAttributeNodeNS : function(newAttr){ | ||
return this.attributes.setNamedItemNS(newAttr); | ||
}, | ||
removeAttributeNode : function(oldAttr){ | ||
var old = this.attributes.removeNamedItem(oldAttr); | ||
old && _onRemoveAttribute(this,old); | ||
return old | ||
return this.attributes.removeNamedItem(oldAttr.nodeName); | ||
}, | ||
setAttributeNodeNS : function(newAttr){ | ||
var attrs = this.attributes; | ||
var old = attrs.getNamedItemNS(newAttr.namespaceURI,newAttr.localName); | ||
_addNamedNode(attrs,newAttr,old) | ||
old && _onRemoveAttribute(this.ownerDocument,this,old); | ||
_onAddAttribute(this.ownerDocument,this,newAttr); | ||
return old; | ||
}, | ||
//get real attribute name,and remove it by removeAttributeNode | ||
@@ -798,2 +792,6 @@ removeAttributeNS : function(namespaceURI, localName){ | ||
}; | ||
Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName; | ||
Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS; | ||
_extends(Element,Node); | ||
@@ -822,2 +820,8 @@ function Attr() { | ||
}, | ||
appendChild:function(newChild){ | ||
//if(!(newChild instanceof CharacterData)){ | ||
throw new Error(ExceptionMessage[3]) | ||
//} | ||
Node.prototype.appendChild.apply(this,arguments) | ||
}, | ||
deleteData: function(offset, count) { | ||
@@ -824,0 +828,0 @@ this.replaceData(offset,count,""); |
{ | ||
"name": "xmldom", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "A W3C Standard based DOMParser and XMLSerializer (DOM Level2 CORE). ", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
var wows = require('vows'); | ||
var DOMParser = require('xmldom').DOMParser; | ||
var XMLSerializer = require('xmldom').XMLSerializer; | ||
// Create a Test Suite | ||
@@ -9,7 +9,7 @@ wows.describe('XML Namespace Parse').addBatch({ | ||
var dom = new DOMParser().parseFromString('<xml xmlns="http://test.com" xmlns:t="http://test.com" xmlns:t2="http://test2.com">' + | ||
var doc = new DOMParser().parseFromString('<xml xmlns="http://test.com" xmlns:t="http://test.com" xmlns:t2="http://test2.com">' + | ||
'<t:test/><test/><t2:test/>'+ | ||
'<child attr="1"><test><child attr="2"/></test></child>' + | ||
'<child attr="3"/></xml>','text/xml'); | ||
var childs = dom.documentElement.getElementsByTagName('child'); | ||
var childs = doc.documentElement.getElementsByTagName('child'); | ||
console.assert(childs.item(0).getAttribute('attr')=="1",childs.item(0)+''); | ||
@@ -20,4 +20,13 @@ console.assert(childs.item(1).getAttribute('attr')=="2",childs.item(1)+''); | ||
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 = dom.documentElement.getElementsByTagName('*'); | ||
var childs = doc.documentElement.getElementsByTagName('*'); | ||
for(var i=0,buf = [];i<childs.length;i++){ | ||
@@ -37,21 +46,79 @@ buf.push(childs[i].tagName) | ||
'getElementsByTagNameNS': function () { | ||
var dom = new DOMParser().parseFromString('<xml xmlns="http://test.com" xmlns:t="http://test.com" xmlns:t2="http://test2.com">' + | ||
var doc = new DOMParser().parseFromString('<xml xmlns="http://test.com" xmlns:t="http://test.com" xmlns:t2="http://test2.com">' + | ||
'<t:test/><test/><t2:test/>'+ | ||
'<child attr="1"><test><child attr="2"/></test></child>' + | ||
'<child attr="3"/></xml>','text/xml'); | ||
var childs = dom.documentElement.getElementsByTagNameNS("http://test.com",'*'); | ||
var childs = doc.documentElement.getElementsByTagNameNS("http://test.com",'*'); | ||
console.assert(childs.length==6,childs.length); | ||
var childs = dom.documentElement.getElementsByTagNameNS("http://test.com",'test'); | ||
var childs = doc.getElementsByTagNameNS("http://test.com",'*'); | ||
console.assert(childs.length==6,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 dom = new DOMParser().parseFromString('<xml xmlns="http://test.com" id="root">' + | ||
var doc = new DOMParser().parseFromString('<xml xmlns="http://test.com" id="root">' + | ||
'<child id="a1" title="1"><child id="a2" title="2"/></child>' + | ||
'<child id="a1" title="3"/></xml>','text/xml'); | ||
console.assert(dom.getElementById('root')) | ||
console.assert(dom.getElementById('a1').getAttribute('title')=="1",dom.getElementById('a1')); | ||
console.assert(dom.getElementById('a2').getAttribute('title')=="2",dom.getElementById('a2')); | ||
console.assert(dom.getElementById('a2').getAttribute('title2')=="",dom.getElementById('a2')); | ||
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('<xml xmlns="http://test.com" id="root">' + | ||
'<child1 id="a1" title="1"><child11 id="a2" title="2"/></child1>' + | ||
'<child2 id="a1" title="3"/><child3 id="a1" title="3"/></xml>','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('<xml xmlns="http://test.com" id="root">' + | ||
'<child1 id="a1" title="1"><child11 id="a2" title="2"><child/></child11></child1>' + | ||
'<child2 id="a1" title="3"/><child3 id="a1" title="3"/></xml>','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); | ||
}, | ||
"nested append failed":function(){ | ||
}, | ||
"self append failed":function(){ | ||
} | ||
}).run(); // Run it |
require('./element'); | ||
require('./level3'); | ||
require('./clone'); | ||
require('./attr'); |
@@ -6,3 +6,4 @@ var wows = require('vows'); | ||
wows.describe('XML Namespace Parse').addBatch({ | ||
"test":function(){} | ||
//see namespace.js | ||
}).run(); // Run it |
var wows = require('vows'); | ||
var assert = require('assert'); | ||
var DOMParser = require('xmldom').DOMParser; | ||
var parser = new DOMParser(); | ||
var XMLSerializer = require('xmldom').XMLSerializer; | ||
// Create a Test Suite | ||
wows.describe('XML Parser').addBatch({ | ||
"file parse":{ | ||
// 'element': function () { | ||
// var dom = parser.parseFromString('<xml><child/></xml>'); | ||
// console.assert (dom.childNodes.length== 1); | ||
// console.assert (dom.documentElement.childNodes.length== 1); | ||
// console.assert (dom.documentElement.tagName== 'xml'); | ||
// console.assert (dom.documentElement.firstChild.tagName== 'child'); | ||
// }, | ||
// 'cdata': function () { | ||
// var dom = parser.parseFromString('<xml>start <![CDATA[<encoded>]]> end</xml>'); | ||
// var root = dom.documentElement; | ||
// console.assert ( root.firstChild.data =='start '); | ||
// console.assert ( root.firstChild.nextSibling.data =='<encoded>'); | ||
// }, | ||
// 'cdata comment': function(){ | ||
// var dom = parser.parseFromString('<xml>start <![CDATA[<encoded>]]> <!-- comment -->end</xml>'); | ||
// var root = dom.documentElement; | ||
// console.assert ( root.firstChild.nodeValue =='start '); | ||
// console.assert ( root.firstChild.nextSibling.nodeValue =='<encoded>'); | ||
// console.assert ( root.firstChild.nextSibling.nextSibling.nextSibling.nodeValue ==' comment '); | ||
// console.assert ( root.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nodeValue =='end'); | ||
// }, | ||
// 'default namespace': function () { | ||
// var dom = parser.parseFromString('<xml xmlns="http://test.com"></xml>'); | ||
// var root = dom.documentElement; | ||
// console.assert(root.namespaceURI=='http://test.com') | ||
// } | ||
test:function(){ | ||
var fs = require('fs'); | ||
var path = require('path') | ||
function saxFile(source){ | ||
var sax = new (require('../sax').XMLReader)(); | ||
var handler = { | ||
startDocument:Function.prototype, | ||
endDocument:Function.prototype, | ||
startElement:Function.prototype, | ||
startCDATA:Function.prototype, | ||
endCDATA:Function.prototype, | ||
endElement:Function.prototype, | ||
startDocument:Function.prototype, | ||
processingInstruction:Function.prototype, | ||
characters:Function.prototype | ||
}; | ||
sax.contentHandler = handler; | ||
sax.lexicalHandler = handler; | ||
sax.errorHandler = handler; | ||
sax.parse(source); | ||
return handler.document; | ||
} | ||
var data = fs.readFileSync(path.resolve(__dirname,'./test.xml'), 'ascii'); | ||
// console.time(); | ||
// saxFile(data); | ||
// console.timeEnd(); | ||
console.time(); | ||
var Dom = require('xmldom').DOMParser; | ||
var doc = new Dom().parseFromString(data); | ||
console.log(doc.childNodes[0].localName); | ||
console.timeEnd(); | ||
// var DomJS = require("dom-js").DomJS; | ||
//var domjs = new DomJS(); | ||
// console.time(); | ||
// | ||
// | ||
// | ||
//var string = data | ||
////'<xml><!-- the comment --><elem someAtt="fat & red">Hello & World</elem></xml>'; | ||
//domjs.parse(string, function(err, dom) { | ||
// console.log(require('util').inspect(dom, false, 2)); | ||
// //console.log("serializes to : " + dom.toXml()); | ||
//}); | ||
// | ||
// console.timeEnd(); | ||
} | ||
} | ||
}).run(); // Run it | ||
var doc = new DOMParser().parseFromString('<xml xmlns="http://test.com" id="root">' + | ||
'<child1 id="a1" title="1"><child11 id="a2" title="2"/></child1>' + | ||
'<child2 id="a1" title="3"/><child3 id="a1" title="3"/></xml>','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 == str3,str3,str1); | ||
//console.assert(str3 != str4 && str3.length == str4.length,str3); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23
2
1280943
2034