xmldoc.min.js 4.8 KB

1
  1. function createXmldoc(){var t;return function(t){var n;if("undefined"!=typeof module&&module.exports&&!global.xmldocAssumeBrowser)n=require("sax");else if(!(n=this.sax))throw new Error("Expected sax to be defined. Make sure you're including sax.js before this file.");function e(t,e){var n;e||(n=l[l.length-1]).parser&&(e=n.parser),this.name=t.name,this.attr=t.attributes,this.val="",this.children=[],this.firstChild=null,this.lastChild=null,this.line=e?e.line:null,this.column=e?e.column:null,this.position=e?e.position:null,this.startTagPosition=e?e.startTagPosition:null}function i(t){this.text=t}function r(t){this.cdata=t}function o(t){this.comment=t}function h(t){if(!(t=t&&t.toString().trim()))throw new Error("No XML to parse!");var e;this.doctype="",this.parser=n.parser(!0),(e=this.parser).onopentag=c,e.onclosetag=d,e.ontext=u,e.oncdata=f,e.oncomment=m,e.ondoctype=y,e.onerror=g,l=[this];try{this.parser.write(t)}finally{delete this.parser}}e.prototype._addChild=function(t){this.children.push(t),this.firstChild||(this.firstChild=t),this.lastChild=t},e.prototype._opentag=function(t){t=new e(t);this._addChild(t),l.unshift(t)},e.prototype._closetag=function(){l.shift()},e.prototype._text=function(t){void 0!==this.children&&(this.val+=t,this._addChild(new i(t)))},e.prototype._cdata=function(t){this.val+=t,this._addChild(new r(t))},e.prototype._comment=function(t){void 0!==this.children&&this._addChild(new o(t))},e.prototype._error=function(t){throw t},e.prototype.eachChild=function(t,e){for(var n=0,i=this.children.length;n<i;n++)if("element"===this.children[n].type&&!1===t.call(e,this.children[n],n,this.children))return},e.prototype.childNamed=function(t){for(var e=0,n=this.children.length;e<n;e++){var i=this.children[e];if(i.name===t)return i}},e.prototype.childrenNamed=function(t){for(var e=[],n=0,i=this.children.length;n<i;n++)this.children[n].name===t&&e.push(this.children[n]);return e},e.prototype.childWithAttribute=function(t,e){for(var n=0,i=this.children.length;n<i;n++){var r=this.children[n];if("element"===r.type&&(e&&r.attr[t]===e||!e&&r.attr[t]))return r}},e.prototype.descendantsNamed=function(t){for(var e=[],n=0,i=this.children.length;n<i;n++){var r=this.children[n];"element"===r.type&&(r.name===t&&e.push(r),e=e.concat(r.descendantsNamed(t)))}return e},e.prototype.descendantWithPath=function(t){for(var e=this,n=t.split("."),i=0,r=n.length;i<r;i++){if(!e||"element"!==e.type)return;e=e.childNamed(n[i])}return e},e.prototype.valueWithPath=function(t){var t=t.split("@"),e=this.descendantWithPath(t[0]);return e?1<t.length?e.attr[t[1]]:e.val:void 0},e.prototype.toString=function(t){return this.toStringWithIndent("",t)},e.prototype.toStringWithIndent=function(t,e){var n,i=t+"<"+this.name,r=e&&e.compressed?"":"\n";e&&e.preserveWhitespace;for(n in this.attr)Object.prototype.hasOwnProperty.call(this.attr,n)&&(i+=" "+n+'="'+v(this.attr[n])+'"');if(1===this.children.length&&"element"!==this.children[0].type)i+=">"+this.children[0].toString(e)+"</"+this.name+">";else if(this.children.length){i+=">"+r;for(var o=t+(e&&e.compressed?"":" "),h=0,s=this.children.length;h<s;h++)i+=this.children[h].toStringWithIndent(o,e)+r;i+=t+"</"+this.name+">"}else!e||!e.html||-1!==["area","base","br","col","embed","frame","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"].indexOf(this.name)?i+="/>":i+="></"+this.name+">";return i},i.prototype.toString=function(t){return _(v(this.text),t)},i.prototype.toStringWithIndent=function(t,e){return t+this.toString(e)},r.prototype.toString=function(t){return"<![CDATA["+_(this.cdata,t)+"]]>"},r.prototype.toStringWithIndent=function(t,e){return t+this.toString(e)},o.prototype.toString=function(t){return"\x3c!--"+_(v(this.comment),t)+"--\x3e"},o.prototype.toStringWithIndent=function(t,e){return t+this.toString(e)},e.prototype.type="element",i.prototype.type="text",r.prototype.type="cdata",o.prototype.type="comment";var s,a=h.prototype,p=e.prototype;for(s in p)p.hasOwnProperty(s)&&(a[s]=p[s]);h.prototype._opentag=function(t){void 0===this.children?e.call(this,t):e.prototype._opentag.apply(this,arguments)},h.prototype._doctype=function(t){this.doctype+=t};var l=null;function c(){l[0]&&l[0]._opentag.apply(l[0],arguments)}function d(){l[0]&&l[0]._closetag.apply(l[0],arguments)}function u(){l[0]&&l[0]._text.apply(l[0],arguments)}function f(){l[0]&&l[0]._cdata.apply(l[0],arguments)}function m(){l[0]&&l[0]._comment.apply(l[0],arguments)}function y(){l[0]&&l[0]._doctype.apply(l[0],arguments)}function g(){l[0]&&l[0]._error.apply(l[0],arguments)}function v(t){return t.toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g,"&apos;").replace(/"/g,"&quot;")}function _(t,e){var n=t;return e&&e.trimmed&&25<t.length&&(n=n.substring(0,25).trim()+"…"),n=e&&e.preserveWhitespace?n:n.trim()}t.XmlDocument=h,t.XmlElement=e,t.XmlTextNode=i,t.XmlCDataNode=r,t.XmlCommentNode=o}(t={}),t}