-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsax.min.js
1 lines (1 loc) · 3.22 KB
/
tsax.min.js
1
const openBracketCC="<".charCodeAt(0),closeBracketCC=">".charCodeAt(0),minusCC="-".charCodeAt(0),slashCC="/".charCodeAt(0),exclamationCC="!".charCodeAt(0),questionCC="?".charCodeAt(0),openCornerBracketCC="[".charCodeAt(0),letterDCC="D".charCodeAt(0),letterxCC="x".charCodeAt(0),spaceCC=" ".charCodeAt(0),nameEndChars=charCodeMap(" \t\n\r/>?["),quoteChars=charCodeMap(`"'`),attributeNameEndChars=charCodeMap("=>");function charCodeMap(t){var e=[];for(const r of t.split(""))e[r.charCodeAt(0)]=!0;return e}function tSax(i){let c=0,r=-1,s=-1,C=-1,u=-1,a=-1,t=-1,d=!1,n=!1,o=void 0;const f={},h={},l={lt:"<",gt:">",amp:"&",quot:'"',apos:"'"};function g(t,e){return o=e+(" at "+function(t){let e=0,r=0,n=0;for(;0<r&&r<t;)n=r,r=i.indexOf("\n",r),e+=1;return e+1+":"+(t-n+1)}(t)),"error"}function p(t,e){return g(t,"Unexpected end of file while scanning for "+e)}function x(t,e,r,n){return u=c+e,a=i.indexOf(n,u),c=a+r,0<a?t:p(u,n)}function m(){return r=c+2,c+=3,t=A(nameEndChars),(C=i.indexOf("?>",t))<0?p(r,"'?>'"):(u=t+1,a=C>=u?C:u,c=C+2,"processingInstruction")}function A(t){for(;!t[i.charCodeAt(c)]&&c<i.length;)c+=1;return c}function b(t){let e=t?t.indexOf("&"):-1;if(!t||e<0)return t;let r=-1,n="";for(;0<=e;){n+=t.substring(r+1,e),(r=t.indexOf(";",e+2))<0&&g(u+e+2,"Missing semicolon");var a=t.substring(e+1,r),o=l[a]||function(t){var e=t.charCodeAt(1)===letterxCC?parseInt(t.substring(2),16):parseInt(t.substring(1),10);if(!isNaN(e))return e=String.fromCharCode(e),l[t]=e}(a);if(!o)return void g(u+r,`Unresolveable entity "${a}"`);n+=o,e=t.indexOf("&",r+1)}return n+t.substring(r+1)}return{next:function(){if(s=-1,a=-1,t=-1,d=!1,n=!1,i.charCodeAt(c)!==openBracketCC)return n=!0,"error"===x("text",0,0,"<")?"eof":"text";switch(i.charCodeAt(c+1)){case slashCC:if(r=c+2,(s=i.indexOf(">",c))<0)return p(r,"'>'");for(c=s+1;i.charCodeAt(s-1)<=spaceCC;)--s;return"endTag";case questionCC:return m();case exclamationCC:switch(i.charCodeAt(c+2)){case minusCC:return x("comment",4,3,"--\x3e");case openCornerBracketCC:return x("cdata",9,3,"]]>");case letterDCC:{r=c+10,c+=11,s=A(nameEndChars);let t=1;do{switch(i[c]){case"<":var e;"?"===i[c+1]?(e=[r,s],m(),[r,s]=e):t+=1;break;case">":--t}}while(c+=1,0<t&&c<i.length);return u=s,a=c-1,0===t?"doctype":p(u,"doctype end");return}default:return g(c,"Unexpected character sequence "+i.substring(c,c+3))}}return n=!0,r=c+1,c+=2,s=A(nameEndChars),(C=i.indexOf(">",s))<0?p(r,"'>'"):(c=C+1,d=!0,i.charCodeAt(C-1)===slashCC?"singleTag":"startTag")},tagName:function(){return 0<s?i.substring(r,s):void 0},localName:function(){var t,e=this.tagName();return e&&(f[e]||(t=(e=e).split(":").pop()||"",f[e]=t))},prefix:function(){var t,e=this.tagName();return e&&(h[e]||(t=1===(t=(e=e).split(":")).length?"":t[0],h[e]=t))},piTarget:function(){return 0<t?i.substring(r,t):void 0},text:function(t){var e;if(!(a<0))return e=i.substring(u,a),t||!n?e:b(e)},attributes:function(t){if(d){var e={};if(C-s<5)return e;for(c=s;;){var r=c+1;if(i.charCodeAt(A(attributeNameEndChars))===closeBracketCC)return c+=1,e;var n=i.substring(r,c).trim(),a=A(quoteChars)+1,o=i[a-1],o=i.indexOf(o,a);if(c>=i.length)return p(r,"attribute delimiters");r=i.substring(a,o);if(t)e[n]=r;else{a=b(r);if(void 0===a)return;e[n]=a}c=o+1}}},error:function(){return o}}}try{module.exports.tSax=tSax}catch(t){}