Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xmldoc

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmldoc - npm Package Compare versions

Comparing version 0.5.1 to 1.0.0

11

CHANGELOG.md
# Change Log
## [v0.5.1](https://github.com/nfarina/xmldoc/tree/v0.5.1) (2016-05-12)
[Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.5.0...v0.5.1)
**Closed issues:**
- Release notes for 0.5 [\#35](https://github.com/nfarina/xmldoc/issues/35)
**Merged pull requests:**
- GLOBAL is producing deprecation warnings in node V6 [\#36](https://github.com/nfarina/xmldoc/pull/36) ([jmalins](https://github.com/jmalins))
## [v0.5.0](https://github.com/nfarina/xmldoc/tree/v0.5.0) (2016-04-27)

@@ -4,0 +15,0 @@ [Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.4.0...v0.5.0)

129

lib/xmldoc.js

@@ -29,4 +29,2 @@ (function () {

this.val = "";
this.isValCdata = false;
this.isValComment = false;
this.children = [];

@@ -43,8 +41,5 @@ this.firstChild = null;

// SaxParser handlers
// Private methods
XmlElement.prototype._opentag = function(tag) {
var child = new XmlElement(tag);
XmlElement.prototype._addChild = function(child) {
// add to our children array

@@ -56,3 +51,12 @@ this.children.push(child);

this.lastChild = child;
};
// SaxParser handlers
XmlElement.prototype._opentag = function(tag) {
var child = new XmlElement(tag);
this._addChild(child);
delegates.unshift(child);

@@ -66,15 +70,23 @@ };

XmlElement.prototype._text = function(text) {
this.val = text;
if (typeof this.children === 'undefined')
return
this.val += text;
this._addChild(new XmlTextNode(text));
};
XmlElement.prototype._cdata = function(cdata) {
this.val = cdata;
this.isValCdata=true;
this.val += cdata;
this._addChild(new XmlCDataNode(cdata));
};
XmlElement.prototype._comment = function(comment) {
this.val = comment;
this.isValComment=true;
}
if (typeof this.children === 'undefined')
return
this._addChild(new XmlCommentNode(comment));
};
XmlElement.prototype._error = function(err) {

@@ -88,3 +100,4 @@ throw err;

for (var i=0, l=this.children.length; i<l; i++)
if (iterator.call(context, this.children[i], i, this.children) === false) return;
if (this.children[i].type === "element")
if (iterator.call(context, this.children[i], i, this.children) === false) return;
};

@@ -113,3 +126,3 @@

var child = this.children[i];
if ( (value && child.attr[name] === value) || (!value && child.attr[name]) )
if (child.type === "element" && ((value && child.attr[name] === value) || (!value && child.attr[name])))
return child;

@@ -125,3 +138,3 @@ }

for (var i=0, l=components.length; i<l; i++)
if (descendant)
if (descendant && descendant.type === "element")
descendant = descendant.childNamed(components[i]);

@@ -158,34 +171,66 @@ else

var finalVal = '';
if (this.isValCdata){
finalVal = '<![CDATA['+this.val+']]>';
} else if (preserveWhitespace) {
finalVal = escapeXML(this.val);
} else{
finalVal = escapeXML(this.val.trim());
if (this.children.length === 1 && this.children[0].type !== "element") {
s += ">" + this.children[0].toString(options) + "</" + this.name + ">";
}
if (options && options.trimmed && finalVal.length > 25)
finalVal = finalVal.substring(0,25).trim() + "…";
if (this.children.length) {
else if (this.children.length) {
s += ">" + linebreak;
var childIndent = indent + (options && options.compressed ? "" : " ");
if (finalVal.length)
s += childIndent + finalVal + linebreak;
for (var i=0, l=this.children.length; i<l; i++)
for (var i=0, l=this.children.length; i<l; i++) {
s += this.children[i].toStringWithIndent(childIndent, options) + linebreak;
}
s += indent + "</" + this.name + ">";
}
else if (finalVal.length) {
s += ">" + finalVal + "</" + this.name +">";
}
else s += "/>";
return s;
};
// Alternative XML nodes
function XmlTextNode (text) {
this.text = text;
}
XmlTextNode.prototype.toString = function(options) {
return formatText(escapeXML(this.text), options);
};
XmlTextNode.prototype.toStringWithIndent = function(indent, options) {
return indent+this.toString(options);
};
function XmlCDataNode (cdata) {
this.cdata = cdata;
}
XmlCDataNode.prototype.toString = function(options) {
return "<![CDATA["+formatText(this.cdata, options)+"]]>";
};
XmlCDataNode.prototype.toStringWithIndent = function(indent, options) {
return indent+this.toString(options);
};
function XmlCommentNode (comment) {
this.comment = comment;
}
XmlCommentNode.prototype.toString = function(options) {
return "<!--"+formatText(escapeXML(this.comment), options)+"-->";
};
XmlCommentNode.prototype.toStringWithIndent = function(indent, options) {
return indent+this.toString(options);
};
// Node type tag
XmlElement.prototype.type = "element";
XmlTextNode.prototype.type = "text";
XmlCDataNode.prototype.type = "cdata";
XmlCommentNode.prototype.type = "comment";
/*

@@ -274,2 +319,14 @@ XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy

// formats some text for debugging given a few options
function formatText(text, options) {
var finalText = text;
if (options && options.trimmed && text.length > 25)
finalText = finalText.substring(0,25).trim() + "…";
if (!(options && options.preserveWhitespace))
finalText = finalText.trim();
return finalText;
}
// Are we being used in a Node-like environment?

@@ -276,0 +333,0 @@ if (typeof module !== 'undefined' && module.exports && !global.xmldocAssumeBrowser)

@@ -9,3 +9,3 @@ {

},
"version": "0.5.1",
"version": "1.0.0",
"main": "./index",

@@ -17,3 +17,3 @@ "scripts": {

"dependencies": {
"sax": "~1.1.1"
"sax": "^1.2.1"
},

@@ -32,2 +32,6 @@ "license": {

"email": "nfarina@gmail.com"
},
{
"name": "Caleb Meredith",
"email": "calebmeredith8@gmail.com"
}

@@ -37,4 +41,4 @@ ],

"devDependencies": {
"tap": "^5.7.1"
"tap": "^8.0.1"
}
}

@@ -15,3 +15,3 @@

See [CHANGELOG.md](./CHANGELOG.md) for details.
See [CHANGELOG.md](./CHANGELOG.md) for details (built with [GitHub Changelog Generator](https://skywinder.github.io/github-changelog-generator/)).

@@ -18,0 +18,0 @@ ## Installation

@@ -8,6 +8,6 @@ var XmlDocument = require('../').XmlDocument

delete require.cache[require.resolve('../')];
// also un-require the actual xmldoc module pulled in by index.js ('../')
delete require.cache[require.resolve('../lib/xmldoc.js')];
// this signal will be picked up on by xmldoc.js

@@ -26,3 +26,3 @@ global.xmldocAssumeBrowser = true;

t.ok(global.XmlDocument);
t.end();

@@ -45,3 +45,3 @@ })

t.test('parse xml', function (t) {
var xmlString = '<hello>world</hello>';

@@ -56,15 +56,22 @@ var parsed = new XmlDocument(xmlString);

t.test('cdata handling', function (t) {
var xmlString = '<hello><![CDATA[<world>]]></hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "<world>");
t.equal(parsed.isValCdata, true);
t.end();
})
t.test('cdata and text handling', function (t) {
var xmlString = '<hello>(<![CDATA[<world>]]>)</hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "(<world>)");
t.end();
})
t.test('doctype handling', function (t) {
var docWithType = new XmlDocument('<!DOCTYPE HelloWorld><hello>world</hello>');
t.equal(docWithType.doctype, " HelloWorld");
var docWithoutType = new XmlDocument('<hello>world</hello>');

@@ -81,18 +88,101 @@ t.equal(docWithoutType.doctype, "");

t.test('comment handling', function (t) {
var xmlString = '<hello><!-- World --></hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, " World ");
t.equal(parsed.isValComment, true);
t.equal(parsed.val, "");
t.end();
})
t.test('comment and text handling', function (t) {
var xmlString = '<hello>(<!-- World -->)</hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "()");
t.end();
})
t.test('text, cdata, and comment handling', function (t) {
var xmlString = '<hello>Hello<!-- , --> <![CDATA[<world>]]>!</hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "Hello <world>!");
t.end();
})
t.test('text with elements handling', function (t) {
var xmlString = '<hello>hello, <world/>!</hello>';
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "hello, !");
t.end();
})
t.test('text before root node', function (t) {
var xmlString = '\n\n<hello>*</hello>';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('text after root node', function (t) {
var xmlString = '<hello>*</hello>\n\n';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('text before root node with version', function (t) {
var xmlString = '<?xml version="1.0"?>\n\n<hello>*</hello>';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('text after root node with version', function (t) {
var xmlString = '<?xml version="1.0"?><hello>*</hello>\n\n';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('comment before root node', function (t) {
var xmlString = '<!-- hello --><world>*</world>';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('comment after root node', function (t) {
var xmlString = '<hello>*</hello><!-- world -->';
var xml = new XmlDocument(xmlString);
t.equal(xml.val, '*');
t.equal(xml.children.length, 1);
t.end();
})
t.test('error handling', function (t) {
var xmlString = '<hello><unclosed-tag></hello>';
t.throws(function() {
var parsed = new XmlDocument(xmlString);
});
t.end();

@@ -102,6 +192,6 @@ })

t.test('tag locations', function (t) {
var xmlString = '<books><book title="Twilight"/></books>';
var books = new XmlDocument(xmlString);
var book = books.children[0];

@@ -117,12 +207,12 @@ t.equal(book.attr.title, "Twilight");

t.test('eachChild', function (t) {
var xmlString = '<books><book title="Twilight"/><book title="Twister"/></books>';
var books = new XmlDocument(xmlString);
expectedTitles = ["Twilight", "Twister"];
books.eachChild(function(book, i, books) {
t.equal(book.attr.title, expectedTitles[i]);
});
called = 0;

@@ -134,14 +224,37 @@ books.eachChild(function(book, i, books) {

t.equal(called, 1);
t.end();
})
t.test('eachChild with text and comments', function (t) {
var xmlString = '<books><book title="Twilight"/>text!<book title="Twister"/><!--comment!--></books>';
var books = new XmlDocument(xmlString);
expectedTitles = ["Twilight", "Twister"];
var elI = 0;
books.eachChild(function(book, i, books) {
t.equal(book.attr.title, expectedTitles[elI++]);
});
called = 0;
books.eachChild(function(book, i, books) {
called++;
return false; // test that returning false short-circuits the loop
});
t.equal(called, 1);
t.end();
})
t.test('childNamed', function (t) {
var xmlString = '<books><book/><good-book/></books>';
var books = new XmlDocument(xmlString);
var goodBook = books.childNamed('good-book');
t.equal(goodBook.name, 'good-book');
t.equal(goodBook.name, 'good-book');
var badBook = books.childNamed('bad-book');

@@ -153,7 +266,21 @@ t.equal(badBook, undefined);

t.test('childNamed with text', function (t) {
var xmlString = '<books><book/>text<good-book/></books>';
var books = new XmlDocument(xmlString);
var goodBook = books.childNamed('good-book');
t.equal(goodBook.name, 'good-book');
var badBook = books.childNamed('bad-book');
t.equal(badBook, undefined);
t.end();
})
t.test('childNamed', function (t) {
var xmlString = '<fruits><apple sweet="yes"/><orange/><apple sweet="no"/><banana/></fruits>';
var fruits = new XmlDocument(xmlString);
var apples = fruits.childrenNamed('apple');

@@ -167,27 +294,45 @@ t.equal(apples.length, 2);

t.test('childWithAttribute', function (t) {
var xmlString = '<fruits><apple pick="no"/><orange rotten="yes"/><apple pick="yes"/><banana/></fruits>';
var fruits = new XmlDocument(xmlString);
var pickedFruit = fruits.childWithAttribute('pick', 'yes');
t.equal(pickedFruit.name, 'apple');
t.equal(pickedFruit.attr.pick, 'yes');
var rottenFruit = fruits.childWithAttribute('rotten');
t.equal(rottenFruit.name, 'orange');
var peeled = fruits.childWithAttribute('peeled');
t.equal(peeled, undefined);
t.end();
})
t.test('childWithAttribute with text', function (t) {
var xmlString = '<fruits><apple pick="no"/><orange rotten="yes"/>text<apple pick="yes"/><banana/></fruits>';
var fruits = new XmlDocument(xmlString);
var pickedFruit = fruits.childWithAttribute('pick', 'yes');
t.equal(pickedFruit.name, 'apple');
t.equal(pickedFruit.attr.pick, 'yes');
var rottenFruit = fruits.childWithAttribute('rotten');
t.equal(rottenFruit.name, 'orange');
var peeled = fruits.childWithAttribute('peeled');
t.equal(peeled, undefined);
t.end();
})
t.test('descendantWithPath', function (t) {
var xmlString = '<book><author><first>George R.R.</first><last>Martin</last></author></book>';
var book = new XmlDocument(xmlString);
var lastNameNode = book.descendantWithPath('author.last');
t.equal(lastNameNode.val, 'Martin');
var middleNameNode = book.descendantWithPath('author.middle');

@@ -198,14 +343,31 @@ t.equal(middleNameNode, undefined);

t.equal(publisherNameNode, undefined);
t.end();
})
t.test('descendantWithPath with text', function (t) {
var xmlString = '<book><author>text<first>George R.R.</first><last>Martin</last></author></book>';
var book = new XmlDocument(xmlString);
var lastNameNode = book.descendantWithPath('author.last');
t.equal(lastNameNode.val, 'Martin');
var middleNameNode = book.descendantWithPath('author.middle');
t.equal(middleNameNode, undefined);
var publisherNameNode = book.descendantWithPath('publisher.first');
t.equal(publisherNameNode, undefined);
t.end();
})
t.test('valueWithPath', function (t) {
var xmlString = '<book><author><first>George R.R.</first><last hyphenated="no">Martin</last></author></book>';
var book = new XmlDocument(xmlString);
var lastName = book.valueWithPath('author.last');
t.equal(lastName, 'Martin');
var lastNameHyphenated = book.valueWithPath('author.last@hyphenated');

@@ -220,7 +382,24 @@ t.equal(lastNameHyphenated, "no");

t.test('valueWithPath with text', function (t) {
var xmlString = '<book><author>text<first>George R.R.</first><last hyphenated="no">Martin</last></author></book>';
var book = new XmlDocument(xmlString);
var lastName = book.valueWithPath('author.last');
t.equal(lastName, 'Martin');
var lastNameHyphenated = book.valueWithPath('author.last@hyphenated');
t.equal(lastNameHyphenated, "no");
var publisherName = book.valueWithPath('publisher.last@hyphenated');
t.equal(publisherName, undefined);
t.end();
})
t.test('toString', function (t) {
var xmlString = '<books><book title="Twilight"/></books>';
var doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<books>\n <book title="Twilight"/>\n</books>');

@@ -237,5 +416,15 @@ t.equal(doc.toString({compressed:true}), '<books><book title="Twilight"/></books>');

doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<hello><![CDATA[<world>]]></hello>');
xmlString = '<hello>Hello<!-- , --> <![CDATA[<world>]]>!</hello>';
doc = new XmlDocument(xmlString);
t.equal(doc.toString({preserveWhitespace:true}), '<hello>\n Hello\n <!-- , -->\n \n <![CDATA[<world>]]>\n !\n</hello>');
xmlString = '<hello>hello, <world/>!</hello>';
doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<hello>\n hello,\n <world/>\n !\n</hello>');
xmlString = '<hello>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam et accumsan nisi.</hello>';

@@ -250,7 +439,7 @@ doc = new XmlDocument(xmlString);

Object.prototype.cruftyExtension = "You don't want this string to be exported!";
var xmlString = '<books><book title="Twilight"/></books>';
var doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<books>\n <book title="Twilight"/>\n</books>');
t.equal(doc.toString(), '<books>\n <book title="Twilight"/>\n</books>');
}

@@ -257,0 +446,0 @@ finally {

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