saxen.umd.min.js 6.3 KB

1
  1. !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.Saxen={})}(this,function(e){"use strict";var n=String.fromCharCode,t=Object.prototype.hasOwnProperty,r=/&#(\d+);|&#x([0-9a-f]+);|&(\w+);/gi,i={amp:"&",apos:"'",gt:">",lt:"<",quot:'"'};function s(e,r,s,u){return u?t.call(i,u)?i[u]:"&"+u+";":n(r||parseInt(s,16))}function u(e){return e.length>3&&-1!==e.indexOf("&")?e.replace(r,s):e}Object.keys(i).forEach(function(e){i[e.toUpperCase()]=i[e]});var o="http://www.w3.org/2001/XMLSchema-instance",f="xsi",a="xsi:type",c="non-whitespace outside of root node";function l(e){return new Error(e)}function d(e){return"missing namespace for prefix <"+e+">"}function g(e){return{get:e,enumerable:!0}}function b(e){var n,t={};for(n in e)t[n]=e[n];return t}function h(e){return e+"$uri"}function m(){return{line:0,column:0}}function x(e){throw e}e.Parser=function e(n){if(!this)return new e(n);var t,r,i,s,p,O,v,C,w,A=n&&n.proxy,k=x,y=m,q=!1,j=!1,N=null,E=!1;function M(e){e instanceof Error||(e=l(e)),N=e,k(e,y)}function P(e){p&&(e instanceof Error||(e=l(e)),p(e,y))}this.on=function(e,n){if("function"!=typeof n)throw l("required args <name, cb>");switch(e){case"openTag":r=n;break;case"text":t=n;break;case"closeTag":i=n;break;case"error":k=n;break;case"warn":p=n;break;case"cdata":s=n;break;case"attention":C=n;break;case"question":v=n;break;case"comment":O=n;break;default:throw l("unsupported event: "+e)}return this},this.ns=function(e){if(void 0===e&&(e={}),"object"!=typeof e)throw l("required args <nsMap={}>");var n,t={};for(n in e)t[n]=e[n];return t[o]=f,j=!0,w=t,this},this.parse=function(e){if("string"!=typeof e)throw l("required args <xml=string>");return N=null,function(e){var n,o,f,l,m,x,p,k,N,S,T,_=j?[]:null,D=j?function(e){var n,t,r={};for(n in e)r[t=e[n]]=t,r[h(t)]=n;return r}(w):null,I=[],L=0,U=!1,X=!1,$=0,z=0,B="",F=0;function G(){if(null!==T)return T;var e,n,t,r,i,s,o,f,c,l,g,m=j&&D.xmlns,x=j&&q?[]:null,p=F,O=B,v=O.length,C={},A={};e:for(;p<v;p++)if(c=!1,!(32===(l=O.charCodeAt(p))||l<14&&l>8)){for((l<65||l>122||l>90&&l<97)&&95!==l&&58!==l&&(P("illegal first char attribute name"),c=!0),g=p+1;g<v;g++)if(!((l=O.charCodeAt(g))>96&&l<123||l>64&&l<91||l>47&&l<59||46===l||45===l||95===l)){if(32===l||l<14&&l>8){P("missing attribute value"),p=g;continue e}if(61===l)break;P("illegal attribute name char"),c=!0}if("xmlns:xmlns"===(f=O.substring(p,g))&&(P("illegal declaration of xmlns"),c=!0),34===(l=O.charCodeAt(g+1)))-1===(g=O.indexOf('"',p=g+2))&&-1!==(g=O.indexOf("'",p))&&(P("attribute value quote missmatch"),c=!0);else if(39===l)-1===(g=O.indexOf("'",p=g+2))&&-1!==(g=O.indexOf('"',p))&&(P("attribute value quote missmatch"),c=!0);else for(P("missing attribute value quotes"),c=!0,g+=1;g<v&&!(32===(l=O.charCodeAt(g+1))||l<14&&l>8);g++);for(-1===g&&(P("missing closing quotes"),g=v,c=!0),c||(s=O.substring(p,g)),p=g;g+1<v&&!(32===(l=O.charCodeAt(g+1))||l<14&&l>8);g++)p===g&&(P("illegal character after attribute end"),c=!0);if(p=g+1,!c)if(f in A)P("attribute <"+f+"> already defined");else if(A[f]=!0,j)if(q){if(null!==(i="xmlns"===f?"xmlns":120===f.charCodeAt(0)&&"xmlns:"===f.substr(0,6)?f.substr(6):null)){if(e=u(s),n=h(i),!(o=w[e])){if("xmlns"===i||n in D&&D[n]!==e)do{o="ns"+L++}while(void 0!==D[o]);else o=i;w[e]=o}D[i]!==o&&(r||(D=b(D),r=!0),D[i]=o,"xmlns"===i&&(D[h(o)]=e,m=o),D[n]=e),C[f]=s;continue}x.push(f,s)}else-1!==(l=f.indexOf(":"))?(t=D[f.substring(0,l)])?((f=m===t?f.substr(l+1):t+f.substr(l))===a&&(-1!==(l=s.indexOf(":"))?(t=s.substring(0,l),t=D[t]||t,s=t+s.substring(l)):s=m+":"+s),C[f]=s):P(d(f.substring(0,l))):C[f]=s;else C[f]=s}if(q)for(p=0,v=x.length;p<v;p++){if(f=x[p++],s=x[p],-1!==(l=f.indexOf(":"))){if(!(t=D[f.substring(0,l)])){P(d(f.substring(0,l)));continue}(f=m===t?f.substr(l+1):t+f.substr(l))===a&&(-1!==(l=s.indexOf(":"))?(t=s.substring(0,l),t=D[t]||t,s=t+s.substring(l)):s=m+":"+s)}C[f]=s}return T=C}for(y=function(){for(var n,t,r=/(\r\n|\r|\n)/g,i=0,s=0,u=0,o=z;$>=u&&(n=r.exec(e))&&!((o=n[0].length+n.index)>$);)i+=1,u=o;return-1==$?(s=o,t=e.substring(z)):0===z?t=e.substring(z,$):(s=$-u,t=-1==z?e.substring($):e.substring($,z+1)),{data:t,line:i,column:s}},A&&(S=Object.create({},{name:g(function(){return k}),originalName:g(function(){return N}),attrs:g(G),ns:g(function(){return D})}));-1!==z;){if(-1===($=60===e.charCodeAt(z)?z:e.indexOf("<",z)))return I.length?M("unexpected end of file"):0===z?M("missing start tag"):void(z<e.length&&e.substring(z).trim()&&P(c));if(z!==$)if(I.length){if(t&&(t(e.substring(z,$),u,y),E))return}else if(e.substring(z,$).trim()&&(P(c),E))return;if(33===(m=e.charCodeAt($+1))){if(91===(l=e.charCodeAt($+2))&&"CDATA["===e.substr($+3,6)){if(-1===(z=e.indexOf("]]>",$)))return M("unclosed cdata");if(s&&(s(e.substring($+9,z),y),E))return;z+=3;continue}if(45===l&&45===e.charCodeAt($+3)){if(-1===(z=e.indexOf("--\x3e",$)))return M("unclosed comment");if(O&&(O(e.substring($+4,z),u,y),E))return;z+=3;continue}}if(63!==m){for(o=$+1;;o++){if(x=e.charCodeAt(o),isNaN(x))return z=-1,M("unclosed tag");if(34===x)l=e.indexOf('"',o+1),o=-1!==l?l:o;else if(39===x)l=e.indexOf("'",o+1),o=-1!==l?l:o;else if(62===x){z=o;break}}if(33!==m){if(T={},47===m){if(U=!1,X=!0,!I.length)return M("missing open tag");if(o=k=I.pop(),l=$+2+o.length,e.substring($+2,l)!==o)return M("closing tag mismatch");for(;l<z;l++)if(!(32===(m=e.charCodeAt(l))||m>8&&m<14))return M("close tag")}else{if(47===e.charCodeAt(z-1)?(o=k=e.substring($+1,z-1),U=!0,X=!0):(o=k=e.substring($+1,z),U=!0,X=!1),!(m>96&&m<123||m>64&&m<91||95===m||58===m))return M("illegal first char nodeName");for(l=1,f=o.length;l<f;l++)if(!((m=o.charCodeAt(l))>96&&m<123||m>64&&m<91||m>47&&m<59||45===m||95===m||46==m)){if(32===m||m<14&&m>8){k=o.substring(0,l),T=null;break}return M("invalid nodeName")}X||I.push(k)}if(j){if(n=D,U&&(X||_.push(n),null===T&&(q=-1!==o.indexOf("xmlns",l))&&(F=l,B=o,G(),q=!1)),N=k,-1!==(m=k.indexOf(":"))){if(!(p=D[k.substring(0,m)]))return M("missing namespace on <"+N+">");k=k.substr(m+1)}else p=D.xmlns;p&&(k=p+":"+k)}if(U&&(F=l,B=o,r&&(A?r(S,u,X,y):r(k,G,u,X,y),E)))return;if(X){if(i&&(i(A?S:k,u,U,y),E))return;j&&(D=U?n:_.pop())}z+=1}else{if(C&&(C(e.substring($,z+1),u,y),E))return;z+=1}}else{if(-1===(z=e.indexOf("?>",$)))return M("unclosed question");if(v&&(v(e.substring($,z+2),y),E))return;z+=2}}}(e),y=m,E=!1,N},this.stop=function(){E=!0}},e.decode=u,Object.defineProperty(e,"__esModule",{value:!0})});