Socket
Socket
Sign inDemoInstall

xmldom

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmldom - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

.project

5

dom-parser.js

@@ -10,2 +10,3 @@ function DOMParser(){

sax.errorHandler = handler;
sax.parse(source);

@@ -53,2 +54,6 @@ return handler.document;

endElement:function(namespaceURI, localName, qName) {
var tagName = this.currentElement.tagName;
if(qName != tagName){
console.warn("end tag name: "+qName+' is not match the current start tagName:'+tagName);
}
this.currentElement = this.currentElement.parentNode;

@@ -55,0 +60,0 @@ },

60

dom.js

@@ -399,20 +399,20 @@ /*

copy(NodeType,Node.prototype);
/**
* @param callback return true for continue,false for break
* @return boolean true: break visit;
*/
function _visitNode(node,callback){
if(!callback(node)){
return false;
if(callback(node)){
return true;
}
var next = node.firstChild;
if(next){
if(!_visitNode(next,callback)){
return false;
}
}
if(next=node.nextSibling){
return _visitNode(next,callback);
}
return true;
if(node = node.firstChild){
do{
if(_visitNode(node,callback)){return true}
}while(node=node.nextSibling)
}
}
function Document(){

@@ -547,5 +547,4 @@ }

rtv = node;
return false;
return true;
}
return true;
}

@@ -594,4 +593,4 @@ })

node.ownerDocument = this;
node.target = target;
node.data = data;
node.tagName = node.target = target;
node.nodeValue= node.data = data;
return node;

@@ -899,3 +898,3 @@ },

case PROCESSING_INSTRUCTION_NODE:
return buf.push( "<?",node.nodeName," ",node.data,"?>");
return buf.push( "<?",node.target," ",node.data,"?>");
case ENTITY_REFERENCE_NODE:

@@ -1014,26 +1013,1 @@ return buf.push( '&',node.nodeName,';');

}
/*
DOM level2 attribute:
Object Document: doctype|implementation|documentElement
Object Node:
[readonly]: nodeName|nodeType|parentNode|childNodes|firstChild|lastChild|previousSibling|nextSibling|attributes|ownerDocument|namespaceURI|localName
nodeValue|prefix
Object NodeList: length
Object NamedNodeMap: length
Object CharacterData
[readonly]: length
data
Object Attr
[readonly]: name|specified|ownerElement
value
Object Element: tagName
Object DocumentType: name|entities|notations|publicId|systemId|internalSubset
Object Notation: publicId|systemId
Object Entity: publicId|systemId|notationName
Object EntityReference
Object ProcessingInstruction
[readonly]: target
data
*/
{
"name": "xmldom",
"version": "0.1.2",
"version": "0.1.3",
"description": "A W3C Standard based DOMParser and XMLSerializer (DOM Level2 CORE). ",

@@ -18,6 +18,16 @@ "keywords": [

"contributors": [
{
"name" : "Yaron Naveh",
"email" : "yaronn01@gmail.com",
"web" : "http://webservices20.blogspot.com/"
},
{
"name" : "Harutyun Amirjanyan",
"email" : "amirjanyan@gmail.com",
"web" : "https://github.com/nightwing"
}
],
"bugs": {
"email": "jindw@xidea.org",
"url": "http://code.google.com/p/lite/issues/list"
"url": "http://github.com/jindw/xmldom/issues"
},

@@ -40,2 +50,5 @@ "licenses": [

},
"devDependencies" : {
"wows" : "*"
},
"engines" : { "node" : ">=0.1" },

@@ -42,0 +55,0 @@ "directories": {

@@ -25,1 +25,123 @@ Introduction

console.info(doc)
API Reference
=====
* [DOMParser](https://developer.mozilla.org/en/DOMParser)
parseFromString(xmlsource,mimeType)
* [XMLSerializer](https://developer.mozilla.org/en/XMLSerializer)
serializeToString(node)
DOM level2 method and attribute:
------
* [Node](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247)
attribute:
nodeValue|prefix
readonly attribute:
nodeName|nodeType|parentNode|childNodes|firstChild|lastChild|previousSibling|nextSibling|attributes|ownerDocument|namespaceURI|localName
method:
insertBefore(newChild, refChild)
replaceChild(newChild, oldChild)
removeChild(oldChild)
appendChild(newChild)
hasChildNodes()
cloneNode(deep)
normalize()
isSupported(feature, version)
hasAttributes()
* [DOMImplementation](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-102161490)
method:
hasFeature(feature, version)
createDocumentType(qualifiedName, publicId, systemId)
createDocument(namespaceURI, qualifiedName, doctype)
* [Document](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#i-Document) : Node
readonly attribute:
doctype|implementation|documentElement
method:
createElement(tagName)
createDocumentFragment()
createTextNode(data)
createComment(data)
createCDATASection(data)
createProcessingInstruction(target, data)
createAttribute(name)
createEntityReference(name)
getElementsByTagName(tagname)
importNode(importedNode, deep)
createElementNS(namespaceURI, qualifiedName)
createAttributeNS(namespaceURI, qualifiedName)
getElementsByTagNameNS(namespaceURI, localName)
getElementById(elementId)
* [DocumentFragment](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-B63ED1A3) : Node
* [Element](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-745549614) : Node
readonly attribute:
tagName
method:
getAttribute(name)
setAttribute(name, value)
removeAttribute(name)
getAttributeNode(name)
setAttributeNode(newAttr)
removeAttributeNode(oldAttr)
getElementsByTagName(name)
getAttributeNS(namespaceURI, localName)
setAttributeNS(namespaceURI, qualifiedName, value)
removeAttributeNS(namespaceURI, localName)
getAttributeNodeNS(namespaceURI, localName)
setAttributeNodeNS(newAttr)
getElementsByTagNameNS(namespaceURI, localName)
hasAttribute(name)
hasAttributeNS(namespaceURI, localName)
* [Attr](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-637646024) : Node
attribute:
value
readonly attribute:
name|specified|ownerElement
[NodeList](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177)
readonly attribute:
length
method:
item(index)
* [NamedNodeMap](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1780488922)
readonly attribute:
length
method:
getNamedItem(name)
setNamedItem(arg)
removeNamedItem(name)
item(index)
getNamedItemNS(namespaceURI, localName)
setNamedItemNS(arg)
removeNamedItemNS(namespaceURI, localName)
* [CharacterData](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-FF21A306) : Node
method:
substringData(offset, count)
appendData(arg)
insertData(offset, arg)
deleteData(offset, count)
replaceData(offset, count, arg)
* [Text](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1312295772) : CharacterData
method:
splitText(offset)
* [CDATASection](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-667469212)
* [Comment](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1728279322) : CharacterData
* [DocumentType](http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-412266927)
readonly attribute:
name|entities|notations|publicId|systemId|internalSubset
* Notation : Node
readonly attribute:
publicId|systemId
* Entity : Node
readonly attribute:
publicId|systemId|notationName
* EntityReference : Node
* ProcessingInstruction : Node
attribute:
data
readonly attribute:
target
DOM level 3 support:
-----
* Element : Node
isDefaultNamespace(namespaceURI){
lookupNamespaceURI(prefix)
//var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
function XMLReader(){
var reader = this;
this._stack = [{nsMap:{},nsStack:{}}];
this._entityReplacer = function(a){//no this
//&lt;&gt;&amp;&qute;&#160;&#xa0;
return _entityReplacer(a,reader);
}
}
function _entityReplacer(a,reader){
var map = reader._entityMap;
var k = a.slice(1,-1);
if(k.charAt(0) == '#'){
return String.fromCharCode(parseInt(k.substr(1).replace('x','0x')))
}else if(k in map){
return map[k];
}else{
reader.errorHandler && reader.errorHandler.error('entity not found:'+a);
return a;
}
}
XMLReader.prototype = {
parse:function(source){
this.contentHandler.startDocument();
parse(this,source);
this.contentHandler.endDocument();
var contentHandler = this.contentHandler;
contentHandler.startDocument();
parse(source,this.entityMap,contentHandler,this.lexicalHandler,this.errorHandler);
contentHandler.endDocument();
},
_entityMap:{'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"},
entityMap:{'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"},
}
function parse(reader,source){
function parse(source,entityMap,contentHandler,lexHandler,errorHandler){
function entityReplacer(a){
var k = a.slice(1,-1);
if(k.charAt(0) == '#'){
return String.fromCharCode(parseInt(k.substr(1).replace('x','0x')))
}else if(k in entityMap){
return entityMap[k];
}else{
errorHandler.error('entity not found:'+a);
return a;
}
}
function appendText(end){
var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer);
contentHandler.characters(xt,0,end-start);
start = end
}
var parseStack = [{nsMap:{}}];
var start = 0;
while(true){
var i = source.indexOf('<');
var next = source.charAt(i+1);
if(i<0){
appendText(reader,source,source.length);
return;
var i = source.indexOf('<',start);
if(i>start){
appendText(i);
}
if(i>0){
appendText(reader,source,i);
source = source.substring(i);
}
if(next == '/'){
var end = source.indexOf('>',3);
var qName = source.substring(2,end);
var config = reader._stack.pop();
source = source.substring(end+1);
reader.contentHandler.endElement(config.uri,config.localName,qName);
switch(source.charAt(i+1)){
case '/':
var end = source.indexOf('>',i+3);
var qName = source.substring(i+2,end);
var config = parseStack.pop();
contentHandler.endElement(config.uri,config.localName,qName);
for(qName in config.nsMap){
reader.contentHandler.endPrefixMapping(qName) ; //reuse qName as prefix
contentHandler.endPrefixMapping(qName) ; //reuse qName as prefix
}
end++;
break;
// end elment
}else if(next == '?'){// <?...?>
source = parseInstruction(reader,source);
}else if(next == '!'){// <!doctype,<![CDATA,<!--
source = parseDCC(reader,source);
case '?':// <?...?>
end = parseInstruction(source,i,lexHandler);
break;
case '!':// <!doctype,<![CDATA,<!--
end = parseDCC(source,i,contentHandler,lexHandler);
break;
default:
if(i<0){
//appendText(reader,source,source.length);
//console.dir(errorHandler.error)
if(!source.substr(start).match(/^\s*$/)){
errorHandler.error('source code out of document root');
}
return;
}else{
end = parseElementStart(source,i,contentHandler,entityReplacer,parseStack);
}
//try{var t = new Date();
//}finally{var r = new Date - r;if(r>0){console.log(r);}}
}
if(end<0){
appendText(i+1);
}else{
source = parseElementStart(reader,source);
start = end;
}

@@ -62,8 +79,8 @@ }

function parseElementStart(reader,source){
var tokens = split(source);
function parseElementStart(source,start,contentHandler,entityReplacer,parseStack){
var tokens = split(source,start);
var qName = tokens[0][0];
var localName = qName.substr(qName.indexOf(':')+1);
var end = tokens.pop();
var nsMap;
var elnsMap;
var attrs = new Attributes();

@@ -74,2 +91,3 @@ var unsetURIs = [];

var i=1;
// console.log(source.substr(start,10),qName)
while(i<len){

@@ -95,3 +113,3 @@ var m = tokens[i++];

}
value = value.replace(/&#?\w+;/g,reader._entityReplacer);;
value = value.replace(/&#?\w+;/g,entityReplacer);;
//TODO:encode value

@@ -101,3 +119,3 @@ }

attr.uri = 'http://www.w3.org/2000/xmlns/';
(nsMap || (nsMap = {}))[prefix == 'xmlns'?attr.localName:''] = value;
(elnsMap || (elnsMap = {}))[prefix == 'xmlns'?attr.localName:''] = value;
}else if(prefix){

@@ -114,99 +132,60 @@ if(prefix == 'xml'){

}
var stack = reader._stack;
var top = stack[stack.length-1];
var parentConfig = parseStack[parseStack.length-1];
var nsMap= parentConfig.nsMap;
var config = {qName:qName};
var nsStack = top.nsStack;
//print(stack+'#'+nsStack)
nsStack = config.nsStack = (nsMap?_set_proto_(nsMap,nsStack):nsStack);
config.uri = nsStack[qName.slice(0,-localName.length-1)];
if(elnsMap){
nsMap = _set_proto_(elnsMap,nsMap)
}
config.nsMap = nsMap;
config.uri = nsMap[qName.slice(0,-localName.length-1)];
while(attr = unsetURIs.pop()){
attr.uri = nsStack[attr.prefix];
attr.uri = nsMap[attr.prefix];
}
if(nsMap){
for(prefix in nsMap){
reader.contentHandler.startPrefixMapping(prefix, nsMap[prefix])
if(elnsMap){
for(prefix in elnsMap){
contentHandler.startPrefixMapping(prefix, elnsMap[prefix])
}
}
reader.contentHandler.startElement(config.uri,localName,qName,attrs);
contentHandler.startElement(config.uri,localName,qName,attrs);
if(end[0].charAt() == '/'){
reader.contentHandler.endElement(config.uri,localName,qName);
if(nsMap){
for(prefix in nsMap){
reader.contentHandler.endPrefixMapping(prefix)
contentHandler.endElement(config.uri,localName,qName);
if(elnsMap){
for(prefix in elnsMap){
contentHandler.endPrefixMapping(prefix)
}
}
}else{
stack.push(config);
parseStack.push(config);
}
return source.substr(end.index + end[0].length)
return end.index + end[0].length
}
function _set_proto_(thiz,parent){
thiz.__proto__ = parent;
return thiz;
}
if(!(_set_proto_({},_set_proto_.prototype) instanceof _set_proto_)){
_set_proto_ = function(thiz,parent){
function p(){};
p.prototype = parent;
p = new p();
for(parent in thiz){
p[parent] = thiz[parent];
}
return p;
}
}
function split(source){
var match;
var buf = [];
var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
reg.lastIndex = 0;
reg.exec(source);//skip <
while(match = reg.exec(source)){
buf.push(match);
if(match[1])return buf;
}
}
function appendText(reader,source,len){
source = source.substr(0,len).replace(/&#?\w+;/g,reader._entityReplacer);
reader.contentHandler.characters(source,0,len);
}
function parseInstruction(reader,source){
var match = source.match(/^<?(^S*)\s*([\s\S]*?)?>/);
if(match){
var len = match[0].length;
reader.contentHandler.processingInstruction(match[1], match[2]) ;
}else{//error
appendText(reader,source,len =2);
}
return source.substring(len);
}
function parseDCC(reader,source){//sure start with '<!'
var next= source.charAt(2)
if(next == '-'){
if(source.charAt(3) == '-'){
var end = source.indexOf('-->');
function parseDCC(source,start,contentHandler,lex){//sure start with '<!'
var next= source.charAt(start+2)
switch(next){
case '-':
if(source.charAt(start + 3) === '-'){
var end = source.indexOf('-->',start+4);
//append comment source.substring(4,end)//<!--
var lex = reader.lexicalHandler
lex && lex.comment(source,4,end-4);
return source.substring(end+3)
lex && lex.comment(source,start+4,end-start-4);
return end+3;
}else{
//error
appendText(reader,source,3)
return source.substr(3);
return -1;
}
}else{
if(/^<!\[CDATA\[/.test(source)){
var end = source.indexOf(']]>');
var lex = reader.lexicalHandler;
case '[':
if(source.substr(start+3,6) == 'CDATA['){
var end = source.indexOf(']]>',start+9);
lex && lex.startCDATA();
appendText(reader,source.substring(9,end),0,end-9);
contentHandler.characters(source,start+9,end-start-9);
lex && lex.endCDATA()
return source.substring(end+3);
return end+3;
}
//<!DOCTYPE
//startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
var matchs = split(source);
var matchs = split(source,start);
var len = matchs.length;

@@ -217,14 +196,29 @@ if(len>1 && /!doctype/i.test(matchs[0][0])){

var sysid = len>4 && matchs[4][0];
var lex = reader.lexicalHandler;
var lastMatch = matchs[len-1]
lex && lex.startDTD(name,pubid,sysid);
lex && lex.endDTD();
matchs = matchs[len-1]
return source.substr(matchs.index+matchs[0].length)
}else{
appendText(reader,source,2)
return source.substr(2);
return lastMatch.index+lastMatch[0].length
}
}
return -1;
}
function parseInstruction(source,start,contentHandler){
var end = source.indexOf('?>',start);
if(end){
var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
if(match){
var len = match[0].length;
contentHandler.processingInstruction(match[1], match[2]) ;
return end+2;
}else{//error
return -1;
}
}
return -1;
}
/**

@@ -255,2 +249,44 @@ * @param source

function _set_proto_(thiz,parent){
thiz.__proto__ = parent;
return thiz;
}
if(!(_set_proto_({},_set_proto_.prototype) instanceof _set_proto_)){
_set_proto_ = function(thiz,parent){
function p(){};
p.prototype = parent;
p = new p();
for(parent in thiz){
p[parent] = thiz[parent];
}
return p;
}
}
function split(source,start){
var match;
var buf = [];
var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
reg.lastIndex = start;
reg.exec(source);//skip <
while(match = reg.exec(source)){
buf.push(match);
if(match[1])return buf;
}
}
function split2(source){
var match;
var buf = [];
var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
reg.lastIndex = 0;
reg.exec(source);//skip <
while(match = reg.exec(source)){
buf.push(match);
if(match[1])return buf;
}
}
if(typeof require == 'function'){

@@ -257,0 +293,0 @@ exports.XMLReader = XMLReader;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc