xml-tokenizer 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";var n=require("./ascii-constants.js"),a=require("./utils.js"),o=require("./XmlError.js"),_=Object.defineProperty,d=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,u=(h,t,e)=>t in h?_(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,E=(h,t)=>{for(var e in t||(t={}))C.call(t,e)&&u(h,e,t[e]);if(d)for(var e of d(t))f.call(t,e)&&u(h,e,t[e]);return h},p=(h,t,e)=>u(h,typeof t!="symbol"?t+"":t,e);class m{constructor(t,e={}){p(this,"_text"),p(this,"_pos"),p(this,"_end"),p(this,"config");const{pos:r=0,strictDocument:i=!0,allowDtd:s=!0,allowBooleanAttributes:l=!1,rawTextElements:c=null}=e;this._text=t,this._pos=r,this._end=this._text.length,this.config={strictDocument:i,allowDtd:s,allowBooleanAttributes:l,rawTextElements:c}}clone(){return new m(this._text,E({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o.XmlError({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!a.isXmlChar(this.currCodeUnitUnchecked()))throw new o.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}skipString(t){if(!this.startsWith(t))throw new o.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&a.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o.XmlError({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),r=e.consumeReference();return r!=null?(this._pos+=e.getPos()-t,r):null}consumeReference(){if(!this.tryConsumeCodeUnit(n.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(n.HASH)){let e,r;this.tryConsumeCodeUnit(n.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(s=>s>=n.ZERO&&s<=n.NINE||s>=n.UPPERCASE_A&&s<=n.UPPERCASE_F||s>=n.LOWERCASE_A&&s<=n.LOWERCASE_F),r=16):(e=this.consumeCodeUnitsWhile(s=>a.isAsciiDigit(s)),r=10);const i=parseInt(e,r);isNaN(i)||!a.isXmlChar(i)?t=null:t={type:"Char",value:String.fromCodePoint(i)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(n.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!a.isXmlNameStart(e))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!a.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e;const r=this._pos;let i=null;for(;this._pos<this._end;){const c=this.currCodeUnitUnchecked();if(c===n.COLON)if(i==null)i=this._pos,this._pos+=1;else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));else if(a.isXmlName(c))this._pos+=1;else break}let s,l;if(i!=null?(s=this._text.slice(r,i),l=this.sliceBack(i+1)):(s="",l=this.sliceBack(r)),s.length>0&&!a.isXmlNameStart((t=s[0])==null?void 0:t.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));if(l.length>0){if(!a.isXmlNameStart((e=l[0])==null?void 0:e.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r))}else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));return[s,l]}consumeQuote(){const t=this.currCodeUnit();if(t===n.SINGLE_QUOTE||t===n.DOUBLE_QUOTE)return this._pos+=1,t;throw new o.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let r=1,i=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===n.LINE_FEED?(r++,i=1):i++;return{row:r,col:i}}}exports.XmlStream=m;
1
+ "use strict";var n=require("./ascii-constants.js"),a=require("./utils.js"),o=require("./XmlError.js"),_=Object.defineProperty,d=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=(h,t,e)=>t in h?_(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,E=(h,t)=>{for(var e in t||(t={}))C.call(t,e)&&p(h,e,t[e]);if(d)for(var e of d(t))f.call(t,e)&&p(h,e,t[e]);return h},l=(h,t,e)=>p(h,typeof t!="symbol"?t+"":t,e);class u{constructor(t,e={}){l(this,"_text"),l(this,"_pos"),l(this,"_end"),l(this,"config");const{pos:r=0,strictDocument:i=!0,allowDtd:s=!0,rawTextElements:c=null}=e;this._text=t,this._pos=r,this._end=this._text.length,this.config={strictDocument:i,allowDtd:s,rawTextElements:c}}clone(){return new u(this._text,E({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o.XmlError({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!a.isXmlChar(this.currCodeUnitUnchecked()))throw new o.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}skipString(t){if(!this.startsWith(t))throw new o.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&a.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o.XmlError({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),r=e.consumeReference();return r!=null?(this._pos+=e.getPos()-t,r):null}consumeReference(){if(!this.tryConsumeCodeUnit(n.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(n.HASH)){let e,r;this.tryConsumeCodeUnit(n.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(s=>s>=n.ZERO&&s<=n.NINE||s>=n.UPPERCASE_A&&s<=n.UPPERCASE_F||s>=n.LOWERCASE_A&&s<=n.LOWERCASE_F),r=16):(e=this.consumeCodeUnitsWhile(s=>a.isAsciiDigit(s)),r=10);const i=parseInt(e,r);isNaN(i)||!a.isXmlChar(i)?t=null:t={type:"Char",value:String.fromCodePoint(i)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(n.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!a.isXmlNameStart(e))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!a.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e;const r=this._pos;let i=null;for(;this._pos<this._end;){const m=this.currCodeUnitUnchecked();if(m===n.COLON)if(i==null)i=this._pos,this._pos+=1;else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));else if(a.isXmlName(m))this._pos+=1;else break}let s,c;if(i!=null?(s=this._text.slice(r,i),c=this.sliceBack(i+1)):(s="",c=this.sliceBack(r)),s.length>0&&!a.isXmlNameStart((t=s[0])==null?void 0:t.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));if(c.length>0){if(!a.isXmlNameStart((e=c[0])==null?void 0:e.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r))}else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));return[s,c]}consumeQuote(){const t=this.currCodeUnit();if(t===n.SINGLE_QUOTE||t===n.DOUBLE_QUOTE)return this._pos+=1,t;throw new o.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let r=1,i=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===n.LINE_FEED?(r++,i=1):i++;return{row:r,col:i}}}exports.XmlStream=u;
@@ -1 +1 @@
1
- "use strict";var I=require("../get-q-name.js"),o=require("./ascii-constants.js"),a=require("./XmlError.js"),X=require("./XmlStream.js");const H="\uFEFF",g="<?xml ",C="?>",L="<!ENTITY",Q="<!ELEMENT",F="<!ATTLIST",D="<!NOTATION",U="<!DOCTYPE",k="<?",A="?>",p="<!--",w="-->",O="<![CDATA[",E="]]>",G="NDATA",W="PUBLIC",b="SYSTEM",B="version",M="encoding",K="standalone";function q(t,n,e={}){P(new X.XmlStream(t,e),n)}function P(t,n){if(t.startsWith(H)&&t.advance(1),t.startsWith(g)&&z(t),u(t,n),t.skipSpaces(),t.startsWith(U)||t.startsWith(U.toLowerCase())){if(!t.config.allowDtd)throw new a.XmlError({type:"DtdDetected"});Y(t,n),u(t,n)}if(t.skipSpaces(),t.config.strictDocument){if(!t.atEnd()&&t.currCodeUnit()===o.LESS_THAN&&T(t,n),u(t,n),!t.atEnd())throw new a.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(;!t.atEnd();)t.currCodeUnit()===o.LESS_THAN?T(t,n):x(t,n),u(t,n)}function u(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(p))f(t,n);else if(t.startsWith(k))h(t,n);else break}function z(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(C)&&!e.atEnd())throw new a.XmlError({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(B))throw new a.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());d(t),n(t),t.startsWith(M)&&(d(t),n(t)),t.startsWith(K)&&d(t),t.skipSpaces(),t.skipString(C)}function f(t,n){const e=t.getPos();t.advance(4);const s=t.consumeCodeUnitsWhile((r,i)=>!(r===o.HYPHEN&&i.startsWith(w)));if(t.skipString(w),s.includes("--")||s.endsWith("-"))throw new a.XmlError({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:s,range:t.rangeFrom(e)})}function h(t,n){if(t.startsWith(g))throw new a.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const s=t.consumeName();t.skipSpaces();const r=t.consumeCodeUnitsWhile((i,c)=>!(i===o.QUESTION_MARK&&c.startsWith(A)));t.skipString(A),n({type:"ProcessingInstruction",target:s,content:r.length===0?void 0:r,range:t.rangeFrom(e)})}function Y(t,n){const e=t.getPos();if($(t),t.skipSpaces(),t.currCodeUnit()===o.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(L))j(t,n);else if(t.startsWith(p))f(t,n);else if(t.startsWith(k))h(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===o.GREATER_THAN){t.advance(1);break}else throw new a.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(Q)||t.startsWith(F)||t.startsWith(D))try{V(t)}catch(s){throw new a.XmlError({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}else throw new a.XmlError({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function $(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),y(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==o.OPEN_SQUARE_BRACKET&&n!==o.GREATER_THAN)throw new a.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function y(t){if(t.startsWith(b)||t.startsWith(W)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const s=t.consumeQuote();if(t.consumeCodeUnitsWhile(r=>r!==s),t.consumeCodeUnit(s),e===W){t.consumeSpaces();const r=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==r),t.consumeCodeUnit(r)}return!0}return!1}function j(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(o.PERCENT);e||t.consumeSpaces();const s=t.consumeName();t.consumeSpaces();const r=J(t,e);r!==null&&n({type:"EntityDeclaration",name:s,definition:r}),t.skipSpaces(),t.consumeCodeUnit(o.GREATER_THAN)}function J(t,n){const e=t.currCodeUnit();if(e===o.DOUBLE_QUOTE||e===o.SINGLE_QUOTE){const s=t.consumeQuote(),r=t.getPos();t.skipCodeUnitsWhile(c=>c!==s);const i=t.sliceBack(r);return t.consumeCodeUnit(s),i}else if(e===o.UPPERCASE_S||e===o.UPPERCASE_P){if(y(t))return n&&(t.skipSpaces(),t.startsWith(G)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new a.XmlError({type:"InvalidExternalID"},t.genTextPos())}else throw new a.XmlError({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:e},t.genTextPos())}function V(t){t.skipCodeUnitsWhile(n=>n!==o.GREATER_THAN),t.consumeCodeUnit(o.GREATER_THAN)}function T(t,n){const e=t.getPos();t.advance(1);const[s,r]=t.consumeQName();n({type:"ElementStart",prefix:s,local:r,start:e});let i=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const m=t.getPos(),S=t.currCodeUnit();if(S===o.SLASH){t.advance(1),t.consumeCodeUnit(o.GREATER_THAN);const l=t.rangeFrom(m);n({type:"ElementEnd",end:{type:"Empty"},range:l});break}else if(S===o.GREATER_THAN){t.advance(1);const l=t.rangeFrom(m);n({type:"ElementEnd",end:{type:"Open"},range:l}),i=!0;break}else{if(!c)throw new a.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[l,v,R]=d(t),_=t.getPos();n({type:"Attribute",range:{start:m,end:_},prefix:l,local:v,value:R})}}if(i){if(t.config.rawTextElements!=null){const c=I.getQName(r,s);if(t.config.rawTextElements.includes(c)){N(t,n,`</${c}>`);return}}N(t,n)}}function d(t){const[n,e]=t.consumeQName();let s;const r=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(o.EQUALS)){t.skipSpaces();const i=t.consumeQuote();s=t.consumeCodeUnitsWhile(c=>c!==i&&c!==o.LESS_THAN),t.consumeCodeUnit(i)}else if(t.config.allowBooleanAttributes)t.goTo(r),s="true";else throw new a.XmlError({type:"InvalidChar",expected:o.EQUALS,actual:t.currCodeUnit()},t.genTextPos());return[n,e,s]}function N(t,n,e=o.LESS_THAN){for(;!t.atEnd();)if(typeof e=="number"?t.currCodeUnit()===e:t.startsWith(e)){const s=t.nextCodeUnit();if(s===o.EXCLAMATION_MARK)if(t.startsWith(p))f(t,n);else if(t.startsWith(O))Z(t,n);else throw new a.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===o.QUESTION_MARK)h(t,n);else if(s===o.SLASH){tt(t,n);break}else T(t,n)}else x(t,n,e)}function Z(t,n){const e=t.getPos();t.advance(9);const s=t.consumeCodeUnitsWhile((i,c)=>!(i===o.CLOSE_SQUARE_BRACKET&&c.startsWith(E)));t.skipString(E);const r=t.rangeFrom(e);n({type:"Cdata",text:s,range:r})}function tt(t,n){const e=t.getPos();t.advance(2);const[s,r]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(o.GREATER_THAN);const i=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:s,local:r},range:i})}function x(t,n,e=o.LESS_THAN){const s=t.getPos(),r=typeof e=="number"?t.consumeCodeUnitsWhile(i=>i!==e):t.consumeCodeUnitsWhile((i,c)=>!c.startsWith(e));if(r.includes(E))throw new a.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:r,range:t.rangeFrom(s)})}exports.tokenize=q,exports.tokenizeXmlStream=P;
1
+ "use strict";var X=require("../get-q-name.js"),s=require("./ascii-constants.js"),I=require("./utils.js"),c=require("./XmlError.js"),L=require("./XmlStream.js");const H="\uFEFF",g="<?xml ",C="?>",Q="<!ENTITY",D="<!ELEMENT",O="<!ATTLIST",F="<!NOTATION",U="<!DOCTYPE",A="<?",k="?>",m="<!--",w="-->",G="<![CDATA[",E="]]>",B="NDATA",W="PUBLIC",b="SYSTEM",q="version",M="encoding",z="standalone";function K(t,n,e={}){P(new L.XmlStream(t,e),n)}function P(t,n){if(t.startsWith(H)&&t.advance(1),t.startsWith(g)&&Y(t),d(t,n),t.skipSpaces(),t.startsWith(U)||t.startsWith(U.toLowerCase())){if(!t.config.allowDtd)throw new c.XmlError({type:"DtdDetected"});$(t,n),d(t,n)}if(t.skipSpaces(),t.config.strictDocument){if(!t.atEnd()&&t.currCodeUnit()===s.LESS_THAN&&S(t,n),d(t,n),!t.atEnd())throw new c.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(;!t.atEnd();)t.currCodeUnit()===s.LESS_THAN?S(t,n):x(t,n),d(t,n)}function d(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(m))f(t,n);else if(t.startsWith(A))h(t,n);else break}function Y(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(C)&&!e.atEnd())throw new c.XmlError({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(q))throw new c.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());p(t),n(t),t.startsWith(M)&&(p(t),n(t)),t.startsWith(z)&&p(t),t.skipSpaces(),t.skipString(C)}function f(t,n){const e=t.getPos();t.advance(4);const o=t.consumeCodeUnitsWhile((r,i)=>!(r===s.HYPHEN&&i.startsWith(w)));if(t.skipString(w),o.includes("--")||o.endsWith("-"))throw new c.XmlError({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:o,range:t.rangeFrom(e)})}function h(t,n){if(t.startsWith(g))throw new c.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const r=t.consumeCodeUnitsWhile((i,a)=>!(i===s.QUESTION_MARK&&a.startsWith(k)));t.skipString(k),n({type:"ProcessingInstruction",target:o,content:r.length===0?void 0:r,range:t.rangeFrom(e)})}function $(t,n){const e=t.getPos();if(j(t),t.skipSpaces(),t.currCodeUnit()===s.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(Q))J(t,n);else if(t.startsWith(m))f(t,n);else if(t.startsWith(A))h(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===s.GREATER_THAN){t.advance(1);break}else throw new c.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(D)||t.startsWith(O)||t.startsWith(F))try{Z(t)}catch(o){throw new c.XmlError({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function j(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),y(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==s.OPEN_SQUARE_BRACKET&&n!==s.GREATER_THAN)throw new c.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function y(t){if(t.startsWith(b)||t.startsWith(W)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(r=>r!==o),t.consumeCodeUnit(o),e===W){t.consumeSpaces();const r=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==r),t.consumeCodeUnit(r)}return!0}return!1}function J(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(s.PERCENT);e||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const r=V(t,e);r!==null&&n({type:"EntityDeclaration",name:o,definition:r}),t.skipSpaces(),t.consumeCodeUnit(s.GREATER_THAN)}function V(t,n){const e=t.currCodeUnit();if(e===s.DOUBLE_QUOTE||e===s.SINGLE_QUOTE){const o=t.consumeQuote(),r=t.getPos();t.skipCodeUnitsWhile(a=>a!==o);const i=t.sliceBack(r);return t.consumeCodeUnit(o),i}else if(e===s.UPPERCASE_S||e===s.UPPERCASE_P){if(y(t))return n&&(t.skipSpaces(),t.startsWith(B)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new c.XmlError({type:"InvalidExternalID"},t.genTextPos())}else throw new c.XmlError({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:e},t.genTextPos())}function Z(t){t.skipCodeUnitsWhile(n=>n!==s.GREATER_THAN),t.consumeCodeUnit(s.GREATER_THAN)}function S(t,n){const e=t.getPos();t.advance(1);const[o,r]=t.consumeQName();n({type:"ElementStart",prefix:o,local:r,start:e});let i=!1;for(;!t.atEnd();){const a=t.startsWithSpace();t.skipSpaces();const l=t.getPos(),T=t.currCodeUnit();if(T===s.SLASH){t.advance(1),t.consumeCodeUnit(s.GREATER_THAN);const u=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Empty"},range:u});break}else if(T===s.GREATER_THAN){t.advance(1);const u=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Open"},range:u}),i=!0;break}else{if(!a)throw new c.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[u,v,R]=p(t),_=t.getPos();n({type:"Attribute",range:{start:l,end:_},prefix:u,local:v,value:R})}}if(i){if(t.config.rawTextElements!=null){const a=X.getQName(r,o);if(t.config.rawTextElements.includes(a)){N(t,n,`</${a}>`);return}}N(t,n)}}function p(t){const[n,e]=t.consumeQName();let o;const r=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(s.EQUALS)){t.skipSpaces();const i=t.currCodeUnit();if(i===s.SINGLE_QUOTE||i===s.DOUBLE_QUOTE){const a=t.consumeQuote();o=t.consumeCodeUnitsWhile(l=>l!==a&&l!==s.LESS_THAN),t.consumeCodeUnit(a)}else if(!t.config.strictDocument)o=t.consumeCodeUnitsWhile(a=>!I.isXmlSpaceByte(a)&&a!==s.GREATER_THAN&&a!==s.SLASH&&a!==s.LESS_THAN);else throw new c.XmlError({type:"InvalidChar",expected:"a quote",actual:i},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(r),o="true";else throw new c.XmlError({type:"InvalidChar",expected:s.EQUALS,actual:t.currCodeUnit()},t.genTextPos());return[n,e,o]}function N(t,n,e=s.LESS_THAN){for(;!t.atEnd();)if(typeof e=="number"?t.currCodeUnit()===e:t.startsWith(e)){const o=t.nextCodeUnit();if(o===s.EXCLAMATION_MARK)if(t.startsWith(m))f(t,n);else if(t.startsWith(G))tt(t,n);else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(o===s.QUESTION_MARK)h(t,n);else if(o===s.SLASH){nt(t,n);break}else S(t,n)}else x(t,n,e)}function tt(t,n){const e=t.getPos();t.advance(9);const o=t.consumeCodeUnitsWhile((i,a)=>!(i===s.CLOSE_SQUARE_BRACKET&&a.startsWith(E)));t.skipString(E);const r=t.rangeFrom(e);n({type:"Cdata",text:o,range:r})}function nt(t,n){const e=t.getPos();t.advance(2);const[o,r]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(s.GREATER_THAN);const i=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:r},range:i})}function x(t,n,e=s.LESS_THAN){const o=t.getPos(),r=typeof e=="number"?t.consumeCodeUnitsWhile(i=>i!==e):t.consumeCodeUnitsWhile((i,a)=>!a.startsWith(e));if(r.includes(E))throw new c.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:r,range:t.rangeFrom(o)})}exports.tokenize=K,exports.tokenizeXmlStream=P;
@@ -1 +1 @@
1
- import{AMPERSAND as C,HASH as f,LOWERCASE_X as w,ZERO as U,NINE as y,UPPERCASE_A as x,UPPERCASE_F as g,LOWERCASE_A as E,LOWERCASE_F as P,SEMICOLON as k,COLON as v,SINGLE_QUOTE as S,DOUBLE_QUOTE as N,LINE_FEED as O}from"./ascii-constants.js";import{isXmlChar as d,isXmlSpaceByte as T,isAsciiDigit as b,isXmlNameStart as l,isXmlName as _}from"./utils.js";import{XmlError as o}from"./XmlError.js";var A=Object.defineProperty,m=Object.getOwnPropertySymbols,I=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable,p=(r,t,e)=>t in r?A(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,F=(r,t)=>{for(var e in t||(t={}))I.call(t,e)&&p(r,e,t[e]);if(m)for(var e of m(t))W.call(t,e)&&p(r,e,t[e]);return r},c=(r,t,e)=>p(r,typeof t!="symbol"?t+"":t,e);class u{constructor(t,e={}){c(this,"_text"),c(this,"_pos"),c(this,"_end"),c(this,"config");const{pos:i=0,strictDocument:n=!0,allowDtd:s=!0,allowBooleanAttributes:h=!1,rawTextElements:a=null}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:n,allowDtd:s,allowBooleanAttributes:h,rawTextElements:a}}clone(){return new u(this._text,F({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!d(this.currCodeUnitUnchecked()))throw new o({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}skipString(t){if(!this.startsWith(t))throw new o({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&T(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),i=e.consumeReference();return i!=null?(this._pos+=e.getPos()-t,i):null}consumeReference(){if(!this.tryConsumeCodeUnit(C))return null;let t;if(this.tryConsumeCodeUnit(f)){let e,i;this.tryConsumeCodeUnit(w)?(e=this.consumeCodeUnitsWhile(s=>s>=U&&s<=y||s>=x&&s<=g||s>=E&&s<=P),i=16):(e=this.consumeCodeUnitsWhile(s=>b(s)),i=10);const n=parseInt(e,i);isNaN(n)||!d(n)?t=null:t={type:"Char",value:String.fromCodePoint(n)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(k)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!l(e))throw new o({type:"InvalidName"},this.genTextPosFrom(t))}else if(!_(e))break;this._pos+=1}}consumeQName(){var t,e;const i=this._pos;let n=null;for(;this._pos<this._end;){const a=this.currCodeUnitUnchecked();if(a===v)if(n==null)n=this._pos,this._pos+=1;else throw new o({type:"InvalidName"},this.genTextPosFrom(i));else if(_(a))this._pos+=1;else break}let s,h;if(n!=null?(s=this._text.slice(i,n),h=this.sliceBack(n+1)):(s="",h=this.sliceBack(i)),s.length>0&&!l((t=s[0])==null?void 0:t.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i));if(h.length>0){if(!l((e=h[0])==null?void 0:e.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i))}else throw new o({type:"InvalidName"},this.genTextPosFrom(i));return[s,h]}consumeQuote(){const t=this.currCodeUnit();if(t===S||t===N)return this._pos+=1,t;throw new o({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,n=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===O?(i++,n=1):n++;return{row:i,col:n}}}export{u as XmlStream};
1
+ import{AMPERSAND as C,HASH as f,LOWERCASE_X as U,ZERO as w,NINE as y,UPPERCASE_A as x,UPPERCASE_F as g,LOWERCASE_A as E,LOWERCASE_F as P,SEMICOLON as k,COLON as v,SINGLE_QUOTE as S,DOUBLE_QUOTE as N,LINE_FEED as O}from"./ascii-constants.js";import{isXmlChar as d,isXmlSpaceByte as T,isAsciiDigit as b,isXmlNameStart as c,isXmlName as m}from"./utils.js";import{XmlError as o}from"./XmlError.js";var A=Object.defineProperty,_=Object.getOwnPropertySymbols,I=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable,l=(r,t,e)=>t in r?A(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,F=(r,t)=>{for(var e in t||(t={}))I.call(t,e)&&l(r,e,t[e]);if(_)for(var e of _(t))W.call(t,e)&&l(r,e,t[e]);return r},a=(r,t,e)=>l(r,typeof t!="symbol"?t+"":t,e);class p{constructor(t,e={}){a(this,"_text"),a(this,"_pos"),a(this,"_end"),a(this,"config");const{pos:i=0,strictDocument:n=!0,allowDtd:s=!0,rawTextElements:h=null}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:n,allowDtd:s,rawTextElements:h}}clone(){return new p(this._text,F({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!d(this.currCodeUnitUnchecked()))throw new o({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}skipString(t){if(!this.startsWith(t))throw new o({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&T(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),i=e.consumeReference();return i!=null?(this._pos+=e.getPos()-t,i):null}consumeReference(){if(!this.tryConsumeCodeUnit(C))return null;let t;if(this.tryConsumeCodeUnit(f)){let e,i;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(s=>s>=w&&s<=y||s>=x&&s<=g||s>=E&&s<=P),i=16):(e=this.consumeCodeUnitsWhile(s=>b(s)),i=10);const n=parseInt(e,i);isNaN(n)||!d(n)?t=null:t={type:"Char",value:String.fromCodePoint(n)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(k)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!c(e))throw new o({type:"InvalidName"},this.genTextPosFrom(t))}else if(!m(e))break;this._pos+=1}}consumeQName(){var t,e;const i=this._pos;let n=null;for(;this._pos<this._end;){const u=this.currCodeUnitUnchecked();if(u===v)if(n==null)n=this._pos,this._pos+=1;else throw new o({type:"InvalidName"},this.genTextPosFrom(i));else if(m(u))this._pos+=1;else break}let s,h;if(n!=null?(s=this._text.slice(i,n),h=this.sliceBack(n+1)):(s="",h=this.sliceBack(i)),s.length>0&&!c((t=s[0])==null?void 0:t.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i));if(h.length>0){if(!c((e=h[0])==null?void 0:e.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i))}else throw new o({type:"InvalidName"},this.genTextPosFrom(i));return[s,h]}consumeQuote(){const t=this.currCodeUnit();if(t===S||t===N)return this._pos+=1,t;throw new o({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,n=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===O?(i++,n=1):n++;return{row:i,col:n}}}export{p as XmlStream};
@@ -1 +1 @@
1
- import{getQName as O}from"../get-q-name.js";import{LESS_THAN as d,HYPHEN as R,QUESTION_MARK as U,GREATER_THAN as r,OPEN_SQUARE_BRACKET as b,PERCENT as B,DOUBLE_QUOTE as M,SINGLE_QUOTE as H,UPPERCASE_S as K,UPPERCASE_P as Y,SLASH as T,EQUALS as k,EXCLAMATION_MARK as X,CLOSE_SQUARE_BRACKET as z}from"./ascii-constants.js";import{XmlError as a}from"./XmlError.js";import{XmlStream as G}from"./XmlStream.js";const $="\uFEFF",w="<?xml ",W="?>",j="<!ENTITY",q="<!ELEMENT",J="<!ATTLIST",V="<!NOTATION",P="<!DOCTYPE",y="<?",x="?>",f="<!--",v="-->",Z="<![CDATA[",h="]]>",tt="NDATA",A="PUBLIC",nt="SYSTEM",st="version",et="encoding",ot="standalone";function it(t,n,s={}){N(new G(t,s),n)}function N(t,n){if(t.startsWith($)&&t.advance(1),t.startsWith(w)&&at(t),u(t,n),t.skipSpaces(),t.startsWith(P)||t.startsWith(P.toLowerCase())){if(!t.config.allowDtd)throw new a({type:"DtdDetected"});ct(t,n),u(t,n)}if(t.skipSpaces(),t.config.strictDocument){if(!t.atEnd()&&t.currCodeUnit()===d&&E(t,n),u(t,n),!t.atEnd())throw new a({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(;!t.atEnd();)t.currCodeUnit()===d?E(t,n):Q(t,n),u(t,n)}function u(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(f))g(t,n);else if(t.startsWith(y))C(t,n);else break}function at(t){function n(s){if(s.startsWithSpace())s.skipSpaces();else if(!s.startsWith(W)&&!s.atEnd())throw new a({type:"InvalidChar",expected:"a whitespace",actual:s.currCodeUnitUnchecked()},s.genTextPos())}if(t.advance(5),n(t),!t.startsWith(st))throw new a({type:"InvalidString",expected:"version"},t.genTextPos());l(t),n(t),t.startsWith(et)&&(l(t),n(t)),t.startsWith(ot)&&l(t),t.skipSpaces(),t.skipString(W)}function g(t,n){const s=t.getPos();t.advance(4);const e=t.consumeCodeUnitsWhile((o,i)=>!(o===R&&i.startsWith(v)));if(t.skipString(v),e.includes("--")||e.endsWith("-"))throw new a({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:e,range:t.rangeFrom(s)})}function C(t,n){if(t.startsWith(w))throw new a({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const e=t.consumeName();t.skipSpaces();const o=t.consumeCodeUnitsWhile((i,c)=>!(i===U&&c.startsWith(x)));t.skipString(x),n({type:"ProcessingInstruction",target:e,content:o.length===0?void 0:o,range:t.rangeFrom(s)})}function ct(t,n){const s=t.getPos();if(rt(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(j))pt(t,n);else if(t.startsWith(f))g(t,n);else if(t.startsWith(y))C(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);break}else throw new a({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(q)||t.startsWith(J)||t.startsWith(V))try{ut(t)}catch(e){throw new a({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new a({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function rt(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),I(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==b&&n!==r)throw new a({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function I(t){if(t.startsWith(nt)||t.startsWith(A)){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const e=t.consumeQuote();if(t.consumeCodeUnitsWhile(o=>o!==e),t.consumeCodeUnit(e),s===A){t.consumeSpaces();const o=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o)}return!0}return!1}function pt(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(B);s||t.consumeSpaces();const e=t.consumeName();t.consumeSpaces();const o=dt(t,s);o!==null&&n({type:"EntityDeclaration",name:e,definition:o}),t.skipSpaces(),t.consumeCodeUnit(r)}function dt(t,n){const s=t.currCodeUnit();if(s===M||s===H){const e=t.consumeQuote(),o=t.getPos();t.skipCodeUnitsWhile(c=>c!==e);const i=t.sliceBack(o);return t.consumeCodeUnit(e),i}else if(s===K||s===Y){if(I(t))return n&&(t.skipSpaces(),t.startsWith(tt)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new a({type:"InvalidExternalID"},t.genTextPos())}else throw new a({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function ut(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function E(t,n){const s=t.getPos();t.advance(1);const[e,o]=t.consumeQName();n({type:"ElementStart",prefix:e,local:o,start:s});let i=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const m=t.getPos(),S=t.currCodeUnit();if(S===T){t.advance(1),t.consumeCodeUnit(r);const p=t.rangeFrom(m);n({type:"ElementEnd",end:{type:"Empty"},range:p});break}else if(S===r){t.advance(1);const p=t.rangeFrom(m);n({type:"ElementEnd",end:{type:"Open"},range:p}),i=!0;break}else{if(!c)throw new a({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[p,D,_]=l(t),L=t.getPos();n({type:"Attribute",range:{start:m,end:L},prefix:p,local:D,value:_})}}if(i){if(t.config.rawTextElements!=null){const c=O(o,e);if(t.config.rawTextElements.includes(c)){F(t,n,`</${c}>`);return}}F(t,n)}}function l(t){const[n,s]=t.consumeQName();let e;const o=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(k)){t.skipSpaces();const i=t.consumeQuote();e=t.consumeCodeUnitsWhile(c=>c!==i&&c!==d),t.consumeCodeUnit(i)}else if(t.config.allowBooleanAttributes)t.goTo(o),e="true";else throw new a({type:"InvalidChar",expected:k,actual:t.currCodeUnit()},t.genTextPos());return[n,s,e]}function F(t,n,s=d){for(;!t.atEnd();)if(typeof s=="number"?t.currCodeUnit()===s:t.startsWith(s)){const e=t.nextCodeUnit();if(e===X)if(t.startsWith(f))g(t,n);else if(t.startsWith(Z))lt(t,n);else throw new a({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(e===U)C(t,n);else if(e===T){mt(t,n);break}else E(t,n)}else Q(t,n,s)}function lt(t,n){const s=t.getPos();t.advance(9);const e=t.consumeCodeUnitsWhile((i,c)=>!(i===z&&c.startsWith(h)));t.skipString(h);const o=t.rangeFrom(s);n({type:"Cdata",text:e,range:o})}function mt(t,n){const s=t.getPos();t.advance(2);const[e,o]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const i=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:e,local:o},range:i})}function Q(t,n,s=d){const e=t.getPos(),o=typeof s=="number"?t.consumeCodeUnitsWhile(i=>i!==s):t.consumeCodeUnitsWhile((i,c)=>!c.startsWith(s));if(o.includes(h))throw new a({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(e)})}export{it as tokenize,N as tokenizeXmlStream};
1
+ import{getQName as b}from"../get-q-name.js";import{LESS_THAN as d,HYPHEN as B,QUESTION_MARK as T,GREATER_THAN as r,OPEN_SQUARE_BRACKET as M,PERCENT as X,DOUBLE_QUOTE as k,SINGLE_QUOTE as w,UPPERCASE_S as H,UPPERCASE_P as Y,SLASH as f,EQUALS as W,EXCLAMATION_MARK as K,CLOSE_SQUARE_BRACKET as z}from"./ascii-constants.js";import{isXmlSpaceByte as q}from"./utils.js";import{XmlError as c}from"./XmlError.js";import{XmlStream as G}from"./XmlStream.js";const $="\uFEFF",P="<?xml ",y="?>",j="<!ENTITY",J="<!ELEMENT",V="<!ATTLIST",Z="<!NOTATION",x="<!DOCTYPE",v="<?",N="?>",h="<!--",A="-->",tt="<![CDATA[",g="]]>",nt="NDATA",I="PUBLIC",st="SYSTEM",et="version",ot="encoding",it="standalone";function at(t,n,s={}){D(new G(t,s),n)}function D(t,n){if(t.startsWith($)&&t.advance(1),t.startsWith(P)&&ct(t),l(t,n),t.skipSpaces(),t.startsWith(x)||t.startsWith(x.toLowerCase())){if(!t.config.allowDtd)throw new c({type:"DtdDetected"});rt(t,n),l(t,n)}if(t.skipSpaces(),t.config.strictDocument){if(!t.atEnd()&&t.currCodeUnit()===d&&U(t,n),l(t,n),!t.atEnd())throw new c({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(;!t.atEnd();)t.currCodeUnit()===d?U(t,n):L(t,n),l(t,n)}function l(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(h))C(t,n);else if(t.startsWith(v))S(t,n);else break}function ct(t){function n(s){if(s.startsWithSpace())s.skipSpaces();else if(!s.startsWith(y)&&!s.atEnd())throw new c({type:"InvalidChar",expected:"a whitespace",actual:s.currCodeUnitUnchecked()},s.genTextPos())}if(t.advance(5),n(t),!t.startsWith(et))throw new c({type:"InvalidString",expected:"version"},t.genTextPos());m(t),n(t),t.startsWith(ot)&&(m(t),n(t)),t.startsWith(it)&&m(t),t.skipSpaces(),t.skipString(y)}function C(t,n){const s=t.getPos();t.advance(4);const e=t.consumeCodeUnitsWhile((o,i)=>!(o===B&&i.startsWith(A)));if(t.skipString(A),e.includes("--")||e.endsWith("-"))throw new c({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:e,range:t.rangeFrom(s)})}function S(t,n){if(t.startsWith(P))throw new c({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const e=t.consumeName();t.skipSpaces();const o=t.consumeCodeUnitsWhile((i,a)=>!(i===T&&a.startsWith(N)));t.skipString(N),n({type:"ProcessingInstruction",target:e,content:o.length===0?void 0:o,range:t.rangeFrom(s)})}function rt(t,n){const s=t.getPos();if(pt(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(j))dt(t,n);else if(t.startsWith(h))C(t,n);else if(t.startsWith(v))S(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);break}else throw new c({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(J)||t.startsWith(V)||t.startsWith(Z))try{lt(t)}catch(e){throw new c({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new c({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function pt(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),F(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==M&&n!==r)throw new c({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function F(t){if(t.startsWith(st)||t.startsWith(I)){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const e=t.consumeQuote();if(t.consumeCodeUnitsWhile(o=>o!==e),t.consumeCodeUnit(e),s===I){t.consumeSpaces();const o=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o)}return!0}return!1}function dt(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(X);s||t.consumeSpaces();const e=t.consumeName();t.consumeSpaces();const o=ut(t,s);o!==null&&n({type:"EntityDeclaration",name:e,definition:o}),t.skipSpaces(),t.consumeCodeUnit(r)}function ut(t,n){const s=t.currCodeUnit();if(s===k||s===w){const e=t.consumeQuote(),o=t.getPos();t.skipCodeUnitsWhile(a=>a!==e);const i=t.sliceBack(o);return t.consumeCodeUnit(e),i}else if(s===H||s===Y){if(F(t))return n&&(t.skipSpaces(),t.startsWith(nt)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new c({type:"InvalidExternalID"},t.genTextPos())}else throw new c({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function lt(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function U(t,n){const s=t.getPos();t.advance(1);const[e,o]=t.consumeQName();n({type:"ElementStart",prefix:e,local:o,start:s});let i=!1;for(;!t.atEnd();){const a=t.startsWithSpace();t.skipSpaces();const p=t.getPos(),E=t.currCodeUnit();if(E===f){t.advance(1),t.consumeCodeUnit(r);const u=t.rangeFrom(p);n({type:"ElementEnd",end:{type:"Empty"},range:u});break}else if(E===r){t.advance(1);const u=t.rangeFrom(p);n({type:"ElementEnd",end:{type:"Open"},range:u}),i=!0;break}else{if(!a)throw new c({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[u,_,O]=m(t),R=t.getPos();n({type:"Attribute",range:{start:p,end:R},prefix:u,local:_,value:O})}}if(i){if(t.config.rawTextElements!=null){const a=b(o,e);if(t.config.rawTextElements.includes(a)){Q(t,n,`</${a}>`);return}}Q(t,n)}}function m(t){const[n,s]=t.consumeQName();let e;const o=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(W)){t.skipSpaces();const i=t.currCodeUnit();if(i===w||i===k){const a=t.consumeQuote();e=t.consumeCodeUnitsWhile(p=>p!==a&&p!==d),t.consumeCodeUnit(a)}else if(!t.config.strictDocument)e=t.consumeCodeUnitsWhile(a=>!q(a)&&a!==r&&a!==f&&a!==d);else throw new c({type:"InvalidChar",expected:"a quote",actual:i},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(o),e="true";else throw new c({type:"InvalidChar",expected:W,actual:t.currCodeUnit()},t.genTextPos());return[n,s,e]}function Q(t,n,s=d){for(;!t.atEnd();)if(typeof s=="number"?t.currCodeUnit()===s:t.startsWith(s)){const e=t.nextCodeUnit();if(e===K)if(t.startsWith(h))C(t,n);else if(t.startsWith(tt))mt(t,n);else throw new c({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(e===T)S(t,n);else if(e===f){ft(t,n);break}else U(t,n)}else L(t,n,s)}function mt(t,n){const s=t.getPos();t.advance(9);const e=t.consumeCodeUnitsWhile((i,a)=>!(i===z&&a.startsWith(g)));t.skipString(g);const o=t.rangeFrom(s);n({type:"Cdata",text:e,range:o})}function ft(t,n){const s=t.getPos();t.advance(2);const[e,o]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const i=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:e,local:o},range:i})}function L(t,n,s=d){const e=t.getPos(),o=typeof s=="number"?t.consumeCodeUnitsWhile(i=>i!==s):t.consumeCodeUnitsWhile((i,a)=>!a.startsWith(s));if(o.includes(g))throw new c({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(e)})}export{at as tokenize,D as tokenizeXmlStream};
@@ -200,10 +200,11 @@ export interface TXmlStreamConfig {
200
200
  * - Document must have exactly one root element
201
201
  * - No text nodes at document level (outside root element)
202
202
  * - Only comments and processing instructions allowed outside root element
203
+ * - Attribute requires an explicit value and the value must be quoted
203
204
  * When false:
204
205
  * - Multiple root elements allowed
205
206
  * - Text nodes allowed at document level
206
- * - More HTML-like permissive parsing
207
+ * - More HTML-like permissive parsing (unquoted attributes, boolean attributes)
207
208
  * @default true
208
209
  */
209
210
  strictDocument: boolean;
@@ -214,12 +215,6 @@ export interface TXmlStreamConfig {
214
215
  * @default null
215
216
  */
216
217
  rawTextElements: string[] | null;
217
- /**
218
- * Whether to allow HTML-style boolean attributes (e.g., <div hidden>).
219
- * When true, attributes without values are treated as having their name as the value.
220
- * @default false
221
- */
222
- allowBooleanAttributes: boolean;
223
218
  /**
224
219
  * Whether to allow DOCTYPE declarations.
225
220
  * @default true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-tokenizer",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "private": false,
5
5
  "description": "Straightforward and typesafe XML tokenizer that streams tokens through a callback mechanism",
6
6
  "keywords": [],