Comparing version 1.1.2 to 1.1.3
(function () { | ||
var sax; | ||
var sax; | ||
if ( | ||
typeof module !== "undefined" && | ||
module.exports && | ||
!global.xmldocAssumeBrowser | ||
) { | ||
// We're being used in a Node-like environment | ||
sax = require("sax"); | ||
} else { | ||
// assume it's attached to the Window object in a browser | ||
sax = this.sax; | ||
if (typeof module !== 'undefined' && module.exports && !global.xmldocAssumeBrowser) { | ||
// We're being used in a Node-like environment | ||
sax = require('sax'); | ||
} | ||
else { | ||
// assume it's attached to the Window object in a browser | ||
sax = this.sax; | ||
if (!sax) { | ||
// no sax for you! | ||
throw new Error( | ||
"Expected sax to be defined. Make sure you're including sax.js before this file.", | ||
); | ||
} | ||
} | ||
if (!sax) // no sax for you! | ||
throw new Error("Expected sax to be defined. Make sure you're including sax.js before this file."); | ||
} | ||
/* | ||
* XmlElement is our basic building block. Everything is an XmlElement; even XmlDocument | ||
* behaves like an XmlElement by inheriting its attributes and functions. | ||
*/ | ||
/* | ||
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) { | ||
// Capture the parser object off of the XmlDocument delegate | ||
var parser = delegates[delegates.length - 1].parser; | ||
function XmlElement(tag) { | ||
// Capture the parser object off of the XmlDocument delegate | ||
var parser = delegates[delegates.length - 1].parser; | ||
this.name = tag.name; | ||
this.attr = tag.attributes; | ||
this.val = ""; | ||
this.children = []; | ||
this.firstChild = null; | ||
this.lastChild = null; | ||
this.name = tag.name; | ||
this.attr = tag.attributes; | ||
this.val = ""; | ||
this.children = []; | ||
this.firstChild = null; | ||
this.lastChild = null; | ||
// Assign parse information | ||
this.line = parser.line; | ||
this.column = parser.column; | ||
this.position = parser.position; | ||
this.startTagPosition = parser.startTagPosition; | ||
} | ||
// Assign parse information | ||
this.line = parser.line; | ||
this.column = parser.column; | ||
this.position = parser.position; | ||
this.startTagPosition = parser.startTagPosition; | ||
} | ||
// Private methods | ||
// Private methods | ||
XmlElement.prototype._addChild = function (child) { | ||
// add to our children array | ||
this.children.push(child); | ||
XmlElement.prototype._addChild = function(child) { | ||
// add to our children array | ||
this.children.push(child); | ||
// update first/last pointers | ||
if (!this.firstChild) this.firstChild = child; | ||
this.lastChild = child; | ||
}; | ||
// update first/last pointers | ||
if (!this.firstChild) this.firstChild = child; | ||
this.lastChild = child; | ||
}; | ||
// SaxParser handlers | ||
// SaxParser handlers | ||
XmlElement.prototype._opentag = function (tag) { | ||
var child = new XmlElement(tag); | ||
XmlElement.prototype._opentag = function(tag) { | ||
this._addChild(child); | ||
var child = new XmlElement(tag); | ||
delegates.unshift(child); | ||
}; | ||
this._addChild(child); | ||
XmlElement.prototype._closetag = function () { | ||
delegates.shift(); | ||
}; | ||
delegates.unshift(child); | ||
}; | ||
XmlElement.prototype._text = function (text) { | ||
if (typeof this.children === "undefined") return; | ||
XmlElement.prototype._closetag = function() { | ||
delegates.shift(); | ||
}; | ||
this.val += text; | ||
XmlElement.prototype._text = function(text) { | ||
if (typeof this.children === 'undefined') | ||
return | ||
this._addChild(new XmlTextNode(text)); | ||
}; | ||
this.val += text; | ||
XmlElement.prototype._cdata = function (cdata) { | ||
this.val += cdata; | ||
this._addChild(new XmlTextNode(text)); | ||
}; | ||
this._addChild(new XmlCDataNode(cdata)); | ||
}; | ||
XmlElement.prototype._cdata = function(cdata) { | ||
this.val += cdata; | ||
XmlElement.prototype._comment = function (comment) { | ||
if (typeof this.children === "undefined") return; | ||
this._addChild(new XmlCDataNode(cdata)); | ||
}; | ||
this._addChild(new XmlCommentNode(comment)); | ||
}; | ||
XmlElement.prototype._comment = function(comment) { | ||
if (typeof this.children === 'undefined') | ||
return | ||
XmlElement.prototype._error = function (err) { | ||
throw err; | ||
}; | ||
this._addChild(new XmlCommentNode(comment)); | ||
}; | ||
// Useful functions | ||
XmlElement.prototype._error = function(err) { | ||
throw err; | ||
}; | ||
XmlElement.prototype.eachChild = function (iterator, context) { | ||
for (var i = 0, l = this.children.length; i < l; i++) | ||
if (this.children[i].type === "element") | ||
if ( | ||
iterator.call(context, this.children[i], i, this.children) === false | ||
) | ||
return; | ||
}; | ||
// Useful functions | ||
XmlElement.prototype.childNamed = function (name) { | ||
for (var i = 0, l = this.children.length; i < l; i++) { | ||
var child = this.children[i]; | ||
if (child.name === name) return child; | ||
} | ||
return undefined; | ||
}; | ||
XmlElement.prototype.eachChild = function(iterator, context) { | ||
for (var i=0, l=this.children.length; i<l; i++) | ||
if (this.children[i].type === "element") | ||
if (iterator.call(context, this.children[i], i, this.children) === false) return; | ||
}; | ||
XmlElement.prototype.childrenNamed = function (name) { | ||
var matches = []; | ||
XmlElement.prototype.childNamed = function(name) { | ||
for (var i=0, l=this.children.length; i<l; i++) { | ||
var child = this.children[i]; | ||
if (child.name === name) return child; | ||
} | ||
return undefined; | ||
}; | ||
for (var i = 0, l = this.children.length; i < l; i++) | ||
if (this.children[i].name === name) matches.push(this.children[i]); | ||
XmlElement.prototype.childrenNamed = function(name) { | ||
var matches = []; | ||
return matches; | ||
}; | ||
for (var i=0, l=this.children.length; i<l; i++) | ||
if (this.children[i].name === name) | ||
matches.push(this.children[i]); | ||
XmlElement.prototype.childWithAttribute = function (name, value) { | ||
for (var i = 0, l = this.children.length; i < l; i++) { | ||
var child = this.children[i]; | ||
if ( | ||
child.type === "element" && | ||
((value && child.attr[name] === value) || (!value && child.attr[name])) | ||
) | ||
return child; | ||
} | ||
return undefined; | ||
}; | ||
return matches; | ||
}; | ||
XmlElement.prototype.descendantWithPath = function (path) { | ||
var descendant = this; | ||
var components = path.split("."); | ||
XmlElement.prototype.childWithAttribute = function(name,value) { | ||
for (var i=0, l=this.children.length; i<l; i++) { | ||
var child = this.children[i]; | ||
if (child.type === "element" && ((value && child.attr[name] === value) || (!value && child.attr[name]))) | ||
return child; | ||
} | ||
return undefined; | ||
}; | ||
for (var i = 0, l = components.length; i < l; i++) | ||
if (descendant && descendant.type === "element") | ||
descendant = descendant.childNamed(components[i]); | ||
else return undefined; | ||
XmlElement.prototype.descendantWithPath = function(path) { | ||
var descendant = this; | ||
var components = path.split('.'); | ||
return descendant; | ||
}; | ||
for (var i=0, l=components.length; i<l; i++) | ||
if (descendant && descendant.type === "element") | ||
descendant = descendant.childNamed(components[i]); | ||
else | ||
return undefined; | ||
XmlElement.prototype.valueWithPath = function (path) { | ||
var components = path.split("@"); | ||
var descendant = this.descendantWithPath(components[0]); | ||
if (descendant) | ||
return components.length > 1 | ||
? descendant.attr[components[1]] | ||
: descendant.val; | ||
else return undefined; | ||
}; | ||
return descendant; | ||
}; | ||
// String formatting (for debugging) | ||
XmlElement.prototype.valueWithPath = function(path) { | ||
var components = path.split('@'); | ||
var descendant = this.descendantWithPath(components[0]); | ||
if (descendant) | ||
return components.length > 1 ? descendant.attr[components[1]] : descendant.val; | ||
else | ||
return undefined; | ||
}; | ||
XmlElement.prototype.toString = function (options) { | ||
return this.toStringWithIndent("", options); | ||
}; | ||
// String formatting (for debugging) | ||
XmlElement.prototype.toStringWithIndent = function (indent, options) { | ||
var s = indent + "<" + this.name; | ||
var linebreak = options && options.compressed ? "" : "\n"; | ||
var preserveWhitespace = options && options.preserveWhitespace; | ||
XmlElement.prototype.toString = function(options) { | ||
return this.toStringWithIndent("", options); | ||
}; | ||
for (var name in this.attr) | ||
if (Object.prototype.hasOwnProperty.call(this.attr, name)) | ||
s += " " + name + '="' + escapeXML(this.attr[name]) + '"'; | ||
XmlElement.prototype.toStringWithIndent = function(indent, options) { | ||
var s = indent + "<" + this.name; | ||
var linebreak = options && options.compressed ? "" : "\n"; | ||
var preserveWhitespace = options && options.preserveWhitespace; | ||
if (this.children.length === 1 && this.children[0].type !== "element") { | ||
s += ">" + this.children[0].toString(options) + "</" + this.name + ">"; | ||
} else if (this.children.length) { | ||
s += ">" + linebreak; | ||
for (var name in this.attr) | ||
if (Object.prototype.hasOwnProperty.call(this.attr, name)) | ||
s += " " + name + '="' + escapeXML(this.attr[name]) + '"'; | ||
var childIndent = indent + (options && options.compressed ? "" : " "); | ||
if (this.children.length === 1 && this.children[0].type !== "element") { | ||
s += ">" + this.children[0].toString(options) + "</" + this.name + ">"; | ||
} | ||
else if (this.children.length) { | ||
s += ">" + linebreak; | ||
for (var i = 0, l = this.children.length; i < l; i++) { | ||
s += | ||
this.children[i].toStringWithIndent(childIndent, options) + linebreak; | ||
} | ||
var childIndent = indent + (options && options.compressed ? "" : " "); | ||
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 (options && options.html) { | ||
var whiteList = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"embed", | ||
"frame", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"menuitem", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr", | ||
]; | ||
if (whiteList.indexOf(this.name) !== -1) s += "/>"; | ||
else s += "></" + this.name + ">"; | ||
} else { | ||
s += "/>"; | ||
} | ||
s += indent + "</" + this.name + ">"; | ||
} | ||
else if (options && options.html) { | ||
var whiteList = [ | ||
"area", "base", "br", "col", "embed", "frame", "hr", "img", "input", | ||
"keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr" | ||
]; | ||
if (whiteList.indexOf(this.name) !== -1) s += "/>"; | ||
else s += "></" + this.name + ">"; | ||
} | ||
else { | ||
s += "/>"; | ||
} | ||
return s; | ||
}; | ||
return s; | ||
}; | ||
// Alternative XML nodes | ||
// Alternative XML nodes | ||
function XmlTextNode(text) { | ||
this.text = text; | ||
} | ||
function XmlTextNode (text) { | ||
this.text = text; | ||
} | ||
XmlTextNode.prototype.toString = function (options) { | ||
return formatText(escapeXML(this.text), options); | ||
}; | ||
XmlTextNode.prototype.toString = function(options) { | ||
return formatText(escapeXML(this.text), options); | ||
}; | ||
XmlTextNode.prototype.toStringWithIndent = function (indent, options) { | ||
return indent + this.toString(options); | ||
}; | ||
XmlTextNode.prototype.toStringWithIndent = function(indent, options) { | ||
return indent+this.toString(options); | ||
}; | ||
function XmlCDataNode(cdata) { | ||
this.cdata = cdata; | ||
} | ||
function XmlCDataNode (cdata) { | ||
this.cdata = cdata; | ||
} | ||
XmlCDataNode.prototype.toString = function (options) { | ||
return "<![CDATA[" + formatText(this.cdata, options) + "]]>"; | ||
}; | ||
XmlCDataNode.prototype.toString = function(options) { | ||
return "<![CDATA["+formatText(this.cdata, options)+"]]>"; | ||
}; | ||
XmlCDataNode.prototype.toStringWithIndent = function (indent, options) { | ||
return indent + this.toString(options); | ||
}; | ||
XmlCDataNode.prototype.toStringWithIndent = function(indent, options) { | ||
return indent+this.toString(options); | ||
}; | ||
function XmlCommentNode(comment) { | ||
this.comment = comment; | ||
} | ||
function XmlCommentNode (comment) { | ||
this.comment = comment; | ||
} | ||
XmlCommentNode.prototype.toString = function (options) { | ||
return "<!--" + formatText(escapeXML(this.comment), options) + "-->"; | ||
}; | ||
XmlCommentNode.prototype.toString = function(options) { | ||
return "<!--"+formatText(escapeXML(this.comment), options)+"-->"; | ||
}; | ||
XmlCommentNode.prototype.toStringWithIndent = function (indent, options) { | ||
return indent + this.toString(options); | ||
}; | ||
XmlCommentNode.prototype.toStringWithIndent = function(indent, options) { | ||
return indent+this.toString(options); | ||
}; | ||
// Node type tag | ||
// Node type tag | ||
XmlElement.prototype.type = "element"; | ||
XmlTextNode.prototype.type = "text"; | ||
XmlCDataNode.prototype.type = "cdata"; | ||
XmlCommentNode.prototype.type = "comment"; | ||
XmlElement.prototype.type = "element"; | ||
XmlTextNode.prototype.type = "text"; | ||
XmlCDataNode.prototype.type = "cdata"; | ||
XmlCommentNode.prototype.type = "comment"; | ||
/* | ||
* XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy | ||
* of XmlElements. | ||
*/ | ||
/* | ||
XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy | ||
of XmlElements. | ||
*/ | ||
function XmlDocument(xml) { | ||
xml && (xml = xml.toString().trim()); | ||
function XmlDocument(xml) { | ||
xml && (xml = xml.toString().trim()); | ||
if (!xml) throw new Error("No XML to parse!"); | ||
if (!xml) | ||
throw new Error("No XML to parse!"); | ||
// Stores doctype (if defined) | ||
this.doctype = ""; | ||
// Stores doctype (if defined) | ||
this.doctype = ""; | ||
// Expose the parser to the other delegates while the parser is running | ||
this.parser = sax.parser(true); // strict | ||
addParserEvents(this.parser); | ||
// Expose the parser to the other delegates while the parser is running | ||
this.parser = sax.parser(true); // strict | ||
addParserEvents(this.parser); | ||
// We'll use the file-scoped "delegates" var to remember what elements we're currently | ||
// parsing; they will push and pop off the stack as we get deeper into the XML hierarchy. | ||
// It's safe to use a global because JS is single-threaded. | ||
delegates = [this]; | ||
// We'll use the file-scoped "delegates" var to remember what elements we're currently | ||
// parsing; they will push and pop off the stack as we get deeper into the XML hierarchy. | ||
// It's safe to use a global because JS is single-threaded. | ||
delegates = [this]; | ||
this.parser.write(xml); | ||
this.parser.write(xml); | ||
// Remove the parser as it is no longer needed and should not be exposed to clients | ||
delete this.parser; | ||
} | ||
// Remove the parser as it is no longer needed and should not be exposed to clients | ||
delete this.parser; | ||
} | ||
// make XmlDocument inherit XmlElement's methods | ||
extend(XmlDocument.prototype, XmlElement.prototype); | ||
// make XmlDocument inherit XmlElement's methods | ||
extend(XmlDocument.prototype, XmlElement.prototype); | ||
XmlDocument.prototype._opentag = function(tag) { | ||
if (typeof this.children === 'undefined') | ||
// the first tag we encounter should be the root - we'll "become" the root XmlElement | ||
XmlElement.call(this,tag); | ||
else | ||
XmlDocument.prototype._opentag = function (tag) { | ||
if (typeof this.children === "undefined") | ||
// the first tag we encounter should be the root - we'll "become" the root XmlElement | ||
XmlElement.call(this, tag); | ||
// all other tags will be the root element's children | ||
XmlElement.prototype._opentag.apply(this,arguments); | ||
}; | ||
else XmlElement.prototype._opentag.apply(this, arguments); | ||
}; | ||
XmlDocument.prototype._doctype = function(doctype) { | ||
this.doctype += doctype; | ||
} | ||
XmlDocument.prototype._doctype = function (doctype) { | ||
this.doctype += doctype; | ||
}; | ||
// file-scoped global stack of delegates | ||
var delegates = null; | ||
// file-scoped global stack of delegates | ||
var delegates = null; | ||
/* | ||
Helper functions | ||
*/ | ||
/* | ||
* Helper functions | ||
*/ | ||
function addParserEvents(parser) { | ||
parser.onopentag = parser_opentag; | ||
parser.onclosetag = parser_closetag; | ||
parser.ontext = parser_text; | ||
parser.oncdata = parser_cdata; | ||
parser.oncomment = parser_comment; | ||
parser.ondoctype = parser_doctype; | ||
parser.onerror = parser_error; | ||
} | ||
function addParserEvents(parser) { | ||
parser.onopentag = parser_opentag; | ||
parser.onclosetag = parser_closetag; | ||
parser.ontext = parser_text; | ||
parser.oncdata = parser_cdata; | ||
parser.oncomment = parser_comment; | ||
parser.ondoctype = parser_doctype; | ||
parser.onerror = parser_error; | ||
} | ||
// create these closures and cache them by keeping them file-scoped | ||
function parser_opentag() { delegates[0] && delegates[0]._opentag.apply(delegates[0],arguments) } | ||
function parser_closetag() { delegates[0] && delegates[0]._closetag.apply(delegates[0],arguments) } | ||
function parser_text() { delegates[0] && delegates[0]._text.apply(delegates[0],arguments) } | ||
function parser_cdata() { delegates[0] && delegates[0]._cdata.apply(delegates[0],arguments) } | ||
function parser_comment() { delegates[0] && delegates[0]._comment.apply(delegates[0],arguments) } | ||
function parser_doctype() { delegates[0] && delegates[0]._doctype.apply(delegates[0],arguments) } | ||
function parser_error() { delegates[0] && delegates[0]._error.apply(delegates[0],arguments) } | ||
// create these closures and cache them by keeping them file-scoped | ||
function parser_opentag() { | ||
delegates[0] && delegates[0]._opentag.apply(delegates[0], arguments); | ||
} | ||
function parser_closetag() { | ||
delegates[0] && delegates[0]._closetag.apply(delegates[0], arguments); | ||
} | ||
function parser_text() { | ||
delegates[0] && delegates[0]._text.apply(delegates[0], arguments); | ||
} | ||
function parser_cdata() { | ||
delegates[0] && delegates[0]._cdata.apply(delegates[0], arguments); | ||
} | ||
function parser_comment() { | ||
delegates[0] && delegates[0]._comment.apply(delegates[0], arguments); | ||
} | ||
function parser_doctype() { | ||
delegates[0] && delegates[0]._doctype.apply(delegates[0], arguments); | ||
} | ||
function parser_error() { | ||
delegates[0] && delegates[0]._error.apply(delegates[0], arguments); | ||
} | ||
// a relatively standard extend method | ||
function extend(destination, source) { | ||
for (var prop in source) | ||
if (source.hasOwnProperty(prop)) | ||
destination[prop] = source[prop]; | ||
} | ||
// a relatively standard extend method | ||
function extend(destination, source) { | ||
for (var prop in source) | ||
if (source.hasOwnProperty(prop)) destination[prop] = source[prop]; | ||
} | ||
// escapes XML entities like "<", "&", etc. | ||
function escapeXML(value){ | ||
return value.toString().replace(/&/g, '&').replace(/</g, "<").replace(/>/g, ">").replace(/'/g, ''').replace(/"/g, '"'); | ||
} | ||
// escapes XML entities like "<", "&", etc. | ||
function escapeXML(value) { | ||
return value | ||
.toString() | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/'/g, "'") | ||
.replace(/"/g, """); | ||
} | ||
// formats some text for debugging given a few options | ||
function formatText(text, options) { | ||
var finalText = text; | ||
// 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(); | ||
if (options && options.trimmed && text.length > 25) { | ||
finalText = finalText.substring(0, 25).trim() + "…"; | ||
} | ||
return finalText; | ||
} | ||
if (!(options && options.preserveWhitespace)) { | ||
finalText = finalText.trim(); | ||
} | ||
// Are we being used in a Node-like environment? | ||
if (typeof module !== 'undefined' && module.exports && !global.xmldocAssumeBrowser) | ||
return finalText; | ||
} | ||
// Are we being used in a Node-like environment? | ||
if ( | ||
typeof module !== "undefined" && | ||
module.exports && | ||
!global.xmldocAssumeBrowser | ||
) { | ||
module.exports.XmlDocument = XmlDocument; | ||
else | ||
module.exports.XmlElement = XmlElement; | ||
module.exports.XmlTextNode = XmlTextNode; | ||
module.exports.XmlCDataNode = XmlCDataNode; | ||
module.exports.XmlCommentNode = XmlCommentNode; | ||
} else { | ||
this.XmlDocument = XmlDocument; | ||
this.XmlElement = XmlElement; | ||
this.XmlTextNode = XmlTextNode; | ||
this.XmlCDataNode = XmlCDataNode; | ||
this.XmlCommentNode = XmlCommentNode; | ||
} | ||
})(); |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "./index", | ||
@@ -16,9 +16,3 @@ "scripts": { | ||
}, | ||
"dependencies": { | ||
"sax": "^1.2.1" | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://raw.github.com/nfarina/xmldoc-js/master/LICENSE" | ||
}, | ||
"license": "https://raw.github.com/nfarina/xmldoc-js/master/LICENSE", | ||
"repository": { | ||
@@ -40,4 +34,8 @@ "type": "git", | ||
"devDependencies": { | ||
"tap": "^8.0.1" | ||
"prettier": "^2.7.1", | ||
"tap": "^16.3.0" | ||
}, | ||
"dependencies": { | ||
"sax": "^1.2.4" | ||
} | ||
} |
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
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
40106
14
686
2
1
Updatedsax@^1.2.4