Socket
Socket
Sign inDemoInstall

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 1.2.0 to 1.3.0

57

lib/xmldoc.js

@@ -23,10 +23,16 @@ (function () {

/*
/**
* XmlElement is our basic building block. Everything is an XmlElement; even XmlDocument
* behaves like an XmlElement by inheriting its attributes and functions.
*/
function XmlElement(tag, parser) {
// If you didn't hand us a parser (common case) see if we can grab one
// from the current execution stack.
if (!parser) {
var delegate = delegates[delegates.length - 1];
function XmlElement(tag) {
// Capture the parser object off of the XmlDocument delegate
var parser = delegates[delegates.length - 1].parser;
if (delegate.parser) {
parser = delegate.parser;
}
}

@@ -41,6 +47,6 @@ this.name = tag.name;

// Assign parse information
this.line = parser.line;
this.column = parser.column;
this.position = parser.position;
this.startTagPosition = parser.startTagPosition;
this.line = parser ? parser.line : null;
this.column = parser ? parser.column : null;
this.position = parser ? parser.position : null;
this.startTagPosition = parser ? parser.startTagPosition : null;
}

@@ -136,16 +142,16 @@

};
XmlElement.prototype.descendantsNamed = function (name) {
var matches = [];
var matches = [];
for (var i = 0, l = this.children.length; i < l; i++) {
var child = this.children[i];
if (child.type === "element") {
if (child.name === name) matches.push(child);
matches = matches.concat(child.descendantsNamed(name));
}
}
for (var i = 0, l = this.children.length; i < l; i++) {
var child = this.children[i];
if (child.type === "element") {
if (child.name === name) matches.push(child);
matches = matches.concat(child.descendantsNamed(name));
}
}
return matches;
};
return matches;
};

@@ -276,7 +282,6 @@ XmlElement.prototype.descendantWithPath = function (path) {

/*
/**
* XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy
* of XmlElements.
*/
function XmlDocument(xml) {

@@ -299,6 +304,8 @@ xml && (xml = xml.toString().trim());

this.parser.write(xml);
// Remove the parser as it is no longer needed and should not be exposed to clients
delete this.parser;
try {
this.parser.write(xml);
} finally {
// Remove the parser as it is no longer needed and should not be exposed to clients
delete this.parser;
}
}

@@ -305,0 +312,0 @@

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

},
"version": "1.2.0",
"version": "1.3.0",
"main": "./index",

@@ -12,0 +12,0 @@ "scripts": {

@@ -1,11 +0,10 @@

var XmlDocument = require('../').XmlDocument
var t = require('tap')
var XmlDocument = require("../").XmlDocument;
var t = require("tap");
t.test('verify sax global in browser', function (t) {
t.test("verify sax global in browser", function (t) {
// "un-require" the xmldoc module that we loaded up top
delete require.cache[require.resolve('../')];
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')];
delete require.cache[require.resolve("../lib/xmldoc.js")];

@@ -15,167 +14,157 @@ // this signal will be picked up on by xmldoc.js

t.throws(function() {
require('../');
t.throws(function () {
require("../");
});
// try again, but this time satisfy the sax check
delete require.cache[require.resolve('../')];
delete require.cache[require.resolve('../lib/xmldoc.js')];
delete require.cache[require.resolve("../")];
delete require.cache[require.resolve("../lib/xmldoc.js")];
global.sax = {};
require('../');
require("../");
t.ok(global.XmlDocument);
t.end();
})
});
t.test('extend util', function(t) {
delete require.cache[require.resolve('../')];
delete require.cache[require.resolve('../lib/xmldoc.js')];
t.test("extend util", function (t) {
delete require.cache[require.resolve("../")];
delete require.cache[require.resolve("../lib/xmldoc.js")];
Object.prototype.cruftyExtension = "blah";
try {
require('../');
}
finally {
require("../");
} finally {
delete Object.prototype.cruftyExtension;
}
t.end();
})
});
t.test('parse xml', function (t) {
var xmlString = '<hello>world</hello>';
t.test("parse xml", function (t) {
var xmlString = "<hello>world</hello>";
var parsed = new XmlDocument(xmlString);
t.ok(parsed);
t.throws(function() { new XmlDocument(); });
t.throws(function() { new XmlDocument(" "); });
t.throws(function () {
new XmlDocument();
});
t.throws(function () {
new XmlDocument(" ");
});
t.end();
})
});
t.test('cdata handling', function (t) {
var xmlString = '<hello><![CDATA[<world>]]></hello>';
t.test("cdata handling", function (t) {
var xmlString = "<hello><![CDATA[<world>]]></hello>";
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "<world>");
t.end();
})
});
t.test('cdata and text handling', function (t) {
var xmlString = '<hello>(<![CDATA[<world>]]>)</hello>';
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.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>');
var docWithoutType = new XmlDocument("<hello>world</hello>");
t.equal(docWithoutType.doctype, "");
t.throws(function() {
new XmlDocument('<hello><!DOCTYPE HelloWorld>world</hello>');
t.throws(function () {
new XmlDocument("<hello><!DOCTYPE HelloWorld>world</hello>");
});
t.end();
})
});
t.test('comment handling', function (t) {
var xmlString = '<hello><!-- World --></hello>';
t.test("comment handling", function (t) {
var xmlString = "<hello><!-- World --></hello>";
var parsed = new XmlDocument(xmlString);
t.equal(parsed.val, "");
t.end();
})
});
t.test('comment and text handling', function (t) {
var xmlString = '<hello>(<!-- World -->)</hello>';
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>';
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>';
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>';
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('text after root node', function (t) {
var xmlString = '<hello>*</hello>\n\n';
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('text before root node with version', function (t) {
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('text after root node with version', function (t) {
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('comment before root node', function (t) {
var xmlString = '<!-- hello --><world>*</world>';
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('comment after root node', function (t) {
var xmlString = '<hello>*</hello><!-- world -->';
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.val, "*");
t.equal(xml.children.length, 1);
t.end();
})
});
t.test('error handling', function (t) {
t.test("error handling", function (t) {
var xmlString = "<hello><unclosed-tag></hello>";
var xmlString = '<hello><unclosed-tag></hello>';
t.throws(function() {
t.throws(function () {
var parsed = new XmlDocument(xmlString);

@@ -185,6 +174,5 @@ });

t.end();
})
});
t.test('tag locations', function (t) {
t.test("tag locations", function (t) {
var xmlString = '<books><book title="Twilight"/></books>';

@@ -200,7 +188,7 @@ var books = new XmlDocument(xmlString);

t.end();
})
});
t.test('eachChild', function (t) {
var xmlString = '<books><book title="Twilight"/><book title="Twister"/></books>';
t.test("eachChild", function (t) {
var xmlString =
'<books><book title="Twilight"/><book title="Twister"/></books>';
var books = new XmlDocument(xmlString);

@@ -210,3 +198,3 @@

books.eachChild(function(book, i, books) {
books.eachChild(function (book, i, books) {
t.equal(book.attr.title, expectedTitles[i]);

@@ -216,3 +204,3 @@ });

called = 0;
books.eachChild(function(book, i, books) {
books.eachChild(function (book, i, books) {
called++;

@@ -224,7 +212,7 @@ return false; // test that returning false short-circuits the loop

t.end();
})
});
t.test('eachChild with text and comments', function (t) {
var xmlString = '<books><book title="Twilight"/>text!<book title="Twister"/><!--comment!--></books>';
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);

@@ -236,3 +224,3 @@

books.eachChild(function(book, i, books) {
books.eachChild(function (book, i, books) {
t.equal(book.attr.title, expectedTitles[elI++]);

@@ -242,3 +230,3 @@ });

called = 0;
books.eachChild(function(book, i, books) {
books.eachChild(function (book, i, books) {
called++;

@@ -250,166 +238,163 @@ return false; // test that returning false short-circuits the loop

t.end();
})
});
t.test('childNamed', function (t) {
var xmlString = '<books><book/><good-book/></books>';
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');
var goodBook = books.childNamed("good-book");
t.equal(goodBook.name, "good-book");
var badBook = books.childNamed('bad-book');
var badBook = books.childNamed("bad-book");
t.equal(badBook, undefined);
t.end();
})
});
t.test('childNamed with text', function (t) {
var xmlString = '<books><book/>text<good-book/></books>';
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 goodBook = books.childNamed("good-book");
t.equal(goodBook.name, "good-book");
var badBook = books.childNamed('bad-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>';
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');
var apples = fruits.childrenNamed("apple");
t.equal(apples.length, 2);
t.equal(apples[0].attr.sweet, 'yes');
t.equal(apples[1].attr.sweet, 'no');
t.equal(apples[0].attr.sweet, "yes");
t.equal(apples[1].attr.sweet, "no");
t.end();
})
});
t.test('childWithAttribute', function (t) {
var xmlString = '<fruits><apple pick="no"/><orange rotten="yes"/><apple pick="yes"/><banana/></fruits>';
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 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 rottenFruit = fruits.childWithAttribute("rotten");
t.equal(rottenFruit.name, "orange");
var peeled = fruits.childWithAttribute('peeled');
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>';
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 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 rottenFruit = fruits.childWithAttribute("rotten");
t.equal(rottenFruit.name, "orange");
var peeled = fruits.childWithAttribute('peeled');
var peeled = fruits.childWithAttribute("peeled");
t.equal(peeled, undefined);
t.end();
})
});
t.test('descendantsNamed', function (t) {
var xmlString = '<navigation><item id="1"/><divider/><item id="2"><item id="2.1"/><item id="2.2"><item id="2.2.1"/></item><divider/><item id="3"/></item></navigation>';
t.test("descendantsNamed", function (t) {
var xmlString =
'<navigation><item id="1"/><divider/><item id="2"><item id="2.1"/><item id="2.2"><item id="2.2.1"/></item><divider/><item id="3"/></item></navigation>';
var navigation = new XmlDocument(xmlString);
var items = navigation.descendantsNamed('item');
var items = navigation.descendantsNamed("item");
t.equal(items.length, 6);
t.equal(items[0].attr.id, '1');
t.equal(items[1].attr.id, '2');
t.equal(items[2].attr.id, '2.1');
t.equal(items[3].attr.id, '2.2');
t.equal(items[4].attr.id, '2.2.1');
t.equal(items[5].attr.id, '3');
t.equal(items[0].attr.id, "1");
t.equal(items[1].attr.id, "2");
t.equal(items[2].attr.id, "2.1");
t.equal(items[3].attr.id, "2.2");
t.equal(items[4].attr.id, "2.2.1");
t.equal(items[5].attr.id, "3");
t.end();
})
});
t.test('descendantWithPath', function (t) {
var xmlString = '<book><author><first>George R.R.</first><last>Martin</last></author></book>';
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 lastNameNode = book.descendantWithPath("author.last");
t.equal(lastNameNode.val, "Martin");
var middleNameNode = book.descendantWithPath('author.middle');
var middleNameNode = book.descendantWithPath("author.middle");
t.equal(middleNameNode, undefined);
var publisherNameNode = book.descendantWithPath('publisher.first');
var publisherNameNode = book.descendantWithPath("publisher.first");
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>';
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 lastNameNode = book.descendantWithPath("author.last");
t.equal(lastNameNode.val, "Martin");
var middleNameNode = book.descendantWithPath('author.middle');
var middleNameNode = book.descendantWithPath("author.middle");
t.equal(middleNameNode, undefined);
var publisherNameNode = book.descendantWithPath('publisher.first');
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>';
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 lastName = book.valueWithPath("author.last");
t.equal(lastName, "Martin");
var lastNameHyphenated = book.valueWithPath('author.last@hyphenated');
var lastNameHyphenated = book.valueWithPath("author.last@hyphenated");
t.equal(lastNameHyphenated, "no");
var publisherName = book.valueWithPath('publisher.last@hyphenated');
var publisherName = book.valueWithPath("publisher.last@hyphenated");
t.equal(publisherName, undefined);
t.end();
})
});
t.test('valueWithPath with text', function (t) {
var xmlString = '<book><author>text<first>George R.R.</first><last hyphenated="no">Martin</last></author></book>';
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 lastName = book.valueWithPath("author.last");
t.equal(lastName, "Martin");
var lastNameHyphenated = book.valueWithPath('author.last@hyphenated');
var lastNameHyphenated = book.valueWithPath("author.last@hyphenated");
t.equal(lastNameHyphenated, "no");
var publisherName = book.valueWithPath('publisher.last@hyphenated');
var publisherName = book.valueWithPath("publisher.last@hyphenated");
t.equal(publisherName, undefined);
t.end();
})
});
t.test('toString', function (t) {
t.test("toString", function (t) {
var xmlString = '<books><book title="Twilight"/></books>';

@@ -419,34 +404,45 @@ var doc = new XmlDocument(xmlString);

t.equal(doc.toString(), '<books>\n <book title="Twilight"/>\n</books>');
t.equal(doc.toString({compressed:true}), '<books><book title="Twilight"/></books>');
t.equal(
doc.toString({ compressed: true }),
'<books><book title="Twilight"/></books>',
);
xmlString = '<hello> world </hello>';
xmlString = "<hello> world </hello>";
doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<hello>world</hello>');
t.equal(doc.toString({preserveWhitespace:true}), '<hello> world </hello>');
t.equal(doc.toString(), "<hello>world</hello>");
t.equal(doc.toString({ preserveWhitespace: true }), "<hello> world </hello>");
xmlString = '<hello><![CDATA[<world>]]></hello>';
xmlString = "<hello><![CDATA[<world>]]></hello>";
doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<hello><![CDATA[<world>]]></hello>');
t.equal(doc.toString(), "<hello><![CDATA[<world>]]></hello>");
xmlString = '<hello>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>');
t.equal(
doc.toString({ preserveWhitespace: true }),
"<hello>\n Hello\n <!-- , -->\n \n <![CDATA[<world>]]>\n !\n</hello>",
);
xmlString = '<hello>hello, <world/>!</hello>';
xmlString = "<hello>hello, <world/>!</hello>";
doc = new XmlDocument(xmlString);
t.equal(doc.toString(), '<hello>\n hello,\n <world/>\n !\n</hello>');
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>';
xmlString =
"<hello>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam et accumsan nisi.</hello>";
doc = new XmlDocument(xmlString);
t.equal(doc.toString(), xmlString);
t.equal(doc.toString({trimmed:true}), '<hello>Lorem ipsum dolor sit ame…</hello>')
t.equal(
doc.toString({ trimmed: true }),
"<hello>Lorem ipsum dolor sit ame…</hello>",
);
try {
// test that adding stuff to the Object prototype doesn't interfere with attribute exporting
Object.prototype.cruftyExtension = "You don't want this string to be exported!";
Object.prototype.cruftyExtension =
"You don't want this string to be exported!";

@@ -457,12 +453,11 @@ var xmlString = '<books><book title="Twilight"/></books>';

t.equal(doc.toString(), '<books>\n <book title="Twilight"/>\n</books>');
}
finally {
} finally {
delete Object.prototype.cruftyExtensionMethod;
}
xmlString = '<hello>world<earth/><moon/></hello>';
xmlString = "<hello>world<earth/><moon/></hello>";
doc = new XmlDocument(xmlString);
t.equal(doc.toString({compressed:true}), xmlString);
t.equal(doc.toString({ compressed: true }), xmlString);
t.end();
})
});
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