xml-tokenizer 0.0.3 → 0.0.5

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.
package/README.md CHANGED
@@ -82,6 +82,13 @@ The following token types are supported:
82
82
  - **Text**: Text content between elements, including whitespace.
83
83
  - **Cdata**: `<![CDATA[text]]>`
84
84
 
85
+ ## 👀 Differences from [XML 1.0 Specification](https://www.w3.org/TR/xml/)
86
+
87
+ - **Attribute Value Handling:**
88
+ - **XML 1.0:** Attributes must be explicitly assigned a value in the format `Name="Value"`. An attribute without a value is not valid XML.
89
+ - **Parser Behavior:** Attributes without an explicit value are interpreted as `true` (e.g., `<element attribute/>` is parsed as `attribute="true"`).
90
+ - **Reason**: This behavior aligns with HTML-style parsing, which was necessary to handle HTML attributes without explicit values.
91
+
85
92
  ## 🚀 Benchmark
86
93
 
87
94
  The performance of `xml-tokenizer` was benchmarked against other popular XML parsers. These tests focus on XML to object conversion and node counting. Interestingly, the version of `xml-tokenizer` imported directly from npm performed significantly better. The reason for this discrepancy is unclear, but the results seem accurate based on external testing.
@@ -128,7 +135,7 @@ The `roxmltree` package with the Rust implementation can be found in the `_depre
128
135
  | roxmltree:wasmMix | 28.17 | 34.83 | 36.71 | 35.49 | ±0.91% |
129
136
  | roxmltree:wasm | 109.30 | 8.30 | 13.16 | 9.15 | ±3.31% |
130
137
 
131
- ### Why Port `tokenizer.rs` to TypeScript?
138
+ ### Why ported `tokenizer.rs` to TypeScript?
132
139
 
133
140
  We ported [`tokenizer.rs`](https://github.com/RazrFalcon/roxmltree/blob/master/src/tokenizer.rs) to TypeScript because frequent communication between Rust and TypeScript negated Rust's performance benefits. The stream architecture required constant interaction between Rust and TypeScript via the `tokenCallback`, reducing overall efficiency.
134
141
 
@@ -1 +1 @@
1
- "use strict";var s=require("./utils.js"),o=require("./XmlError.js"),u=Object.defineProperty,m=(h,t,e)=>t in h?u(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,l=(h,t,e)=>m(h,typeof t!="symbol"?t+"":t,e);class c{constructor(t,e=0){l(this,"_text"),l(this,"_pos"),l(this,"_end"),this._text=t,this._pos=e,this._end=this._text.length}clone(){return new c(this._text,this._pos)}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)}advance(t){this._pos+=t}startsWith(t){return this._text.startsWith(t,this._pos)}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}skipString(t){if(!this.startsWith(t))throw new o.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked());)this._pos+=1}consumeCharsWhile(t){const e=this._pos;return this.skipCharsWhile(t),this.sliceBack(e)}skipCharsWhile(t){for(;this._pos<this._end;){const e=this._text[this._pos];if(e==null||!s.isXmlChar(e.codePointAt(0)))throw new o.XmlError({type:"NonXmlChar",char:e!=null?e:"\uFFFD"},this.genTextPos());if(t(this,e))this._pos+=e.length;else break}}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&&s.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(s.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(s.HASH)){let e,r;this.tryConsumeCodeUnit(s.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(i=>i>=s.ZERO&&i<=s.NINE||i>=s.UPPERCASE_A&&i<=s.UPPERCASE_F||i>=s.LOWERCASE_A&&i<=s.LOWERCASE_F),r=16):(e=this.consumeCodeUnitsWhile(i=>s.isAsciiDigit(i)),r=10);const n=parseInt(e,r);isNaN(n)||!s.isXmlChar(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(s.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(!s.isXmlNameStart(e))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!s.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e;const r=this._pos;let n=null;for(;this._pos<this._end;){const p=this.currCodeUnitUnchecked();if(p===s.COLON)if(n==null)n=this._pos,this._pos+=1;else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));else if(s.isXmlName(p))this._pos+=1;else break}let i,a;if(n!=null?(i=this._text.slice(r,n),a=this.sliceBack(n+1)):(i="",a=this.sliceBack(r)),i.length>0&&!s.isXmlNameStart((t=i[0])==null?void 0:t.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));if(a.length>0){if(!s.isXmlNameStart((e=a[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[i,a]}consumeEq(){this.skipSpaces(),this.consumeCodeUnit(s.EQUALS),this.skipSpaces()}consumeQuote(){const t=this.currCodeUnit();if(t===s.SINGLE_QUOTE||t===s.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,n=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===s.LINE_FEED?(r++,n=1):n++;return{row:r,col:n}}}exports.XmlStream=c;
1
+ "use strict";var s=require("./utils.js"),o=require("./XmlError.js"),u=Object.defineProperty,m=(h,t,e)=>t in h?u(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,l=(h,t,e)=>m(h,typeof t!="symbol"?t+"":t,e);class c{constructor(t,e=0){l(this,"_text"),l(this,"_pos"),l(this,"_end"),this._text=t,this._pos=e,this._end=this._text.length}clone(){return new c(this._text,this._pos)}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)}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}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}skipString(t){if(!this.startsWith(t))throw new o.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked());)this._pos+=1}consumeCharsWhile(t){const e=this._pos;return this.skipCharsWhile(t),this.sliceBack(e)}skipCharsWhile(t){for(;this._pos<this._end;){const e=this._text[this._pos];if(e==null||!s.isXmlChar(e.codePointAt(0)))throw new o.XmlError({type:"NonXmlChar",char:e!=null?e:"\uFFFD"},this.genTextPos());if(t(this,e))this._pos+=e.length;else break}}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&&s.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(s.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(s.HASH)){let e,r;this.tryConsumeCodeUnit(s.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(i=>i>=s.ZERO&&i<=s.NINE||i>=s.UPPERCASE_A&&i<=s.UPPERCASE_F||i>=s.LOWERCASE_A&&i<=s.LOWERCASE_F),r=16):(e=this.consumeCodeUnitsWhile(i=>s.isAsciiDigit(i)),r=10);const n=parseInt(e,r);isNaN(n)||!s.isXmlChar(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(s.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(!s.isXmlNameStart(e))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!s.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e;const r=this._pos;let n=null;for(;this._pos<this._end;){const p=this.currCodeUnitUnchecked();if(p===s.COLON)if(n==null)n=this._pos,this._pos+=1;else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));else if(s.isXmlName(p))this._pos+=1;else break}let i,a;if(n!=null?(i=this._text.slice(r,n),a=this.sliceBack(n+1)):(i="",a=this.sliceBack(r)),i.length>0&&!s.isXmlNameStart((t=i[0])==null?void 0:t.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(r));if(a.length>0){if(!s.isXmlNameStart((e=a[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[i,a]}consumeQuote(){const t=this.currCodeUnit();if(t===s.SINGLE_QUOTE||t===s.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,n=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===s.LINE_FEED?(r++,n=1):n++;return{row:r,col:n}}}exports.XmlStream=c;
@@ -1 +1 @@
1
- "use strict";var r=require("./utils.js"),i=require("./XmlError.js");function U(t,n,s){if(t.startsWith("\uFEFF")&&t.advance(1),t.startsWith("<?xml ")&&P(t),m(t,s),t.skipSpaces(),t.startsWith("<!DOCTYPE")){if(!n)throw new i.XmlError({type:"DtdDetected"});w(t,s),m(t,s)}if(t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===r.LESS_THAN&&g(t,s),m(t,s),!t.atEnd())throw new i.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!--"))h(t,n);else if(t.startsWith("<?"))f(t,n);else break}function P(t){if(t.advance(5),E(t),!t.startsWith("version"))throw new i.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());u(t),E(t),t.startsWith("encoding")&&(u(t),E(t)),t.startsWith("standalone")&&u(t),t.skipSpaces(),t.skipString("?>")}function E(t){if(t.startsWithSpace())t.skipSpaces();else if(!t.startsWith("?>")&&!t.atEnd())throw new i.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos())}function h(t,n){const s=t.getPos();t.advance(4);const o=t.consumeCharsWhile((e,a)=>!(a==="-"&&e.startsWith("-->")));if(t.skipString("-->"),o.includes("--")||o.endsWith("-"))throw new i.XmlError({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:o,range:t.rangeFrom(s)})}function f(t,n){if(t.startsWith("<?xml "))throw new i.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const e=t.consumeCharsWhile((a,c)=>!(c==="?"&&a.startsWith("?>")));t.skipString("?>"),n({type:"ProcessingInstruction",target:o,content:e.length===0?void 0:e,range:t.rangeFrom(s)})}function w(t,n){const s=t.getPos();if(A(t),t.skipSpaces(),t.currCodeUnit()===r.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!ENTITY"))W(t,n);else if(t.startsWith("<!--"))h(t,n);else if(t.startsWith("<?"))f(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r.GREATER_THAN){t.advance(1);break}else throw new i.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith("<!ELEMENT")||t.startsWith("<!ATTLIST")||t.startsWith("<!NOTATION"))try{y(t)}catch(o){throw new i.XmlError({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new i.XmlError({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function A(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),T(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==r.OPEN_BRACKET&&n!==r.GREATER_THAN)throw new i.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function T(t){if(t.startsWith("SYSTEM")||t.startsWith("PUBLIC")){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(e=>e!==o),t.consumeCodeUnit(o),s==="PUBLIC"){t.consumeSpaces();const e=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==e),t.consumeCodeUnit(e)}return!0}return!1}function W(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(r.PERCENT);s||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const e=v(t,s);e!==null&&n({type:"EntityDeclaration",name:o,definition:e}),t.skipSpaces(),t.consumeCodeUnit(r.GREATER_THAN)}function v(t,n){const s=t.currCodeUnit();if(s===r.DOUBLE_QUOTE||s===r.SINGLE_QUOTE){const o=t.consumeQuote(),e=t.getPos();t.skipCodeUnitsWhile(c=>c!==o);const a=t.sliceBack(e);return t.consumeCodeUnit(o),a}else if(s===r.UPPERCASE_S||s===r.UPPERCASE_P){if(T(t))return n&&(t.skipSpaces(),t.startsWith("NDATA")&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new i.XmlError({type:"InvalidExternalID"},t.genTextPos())}else throw new i.XmlError({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function y(t){t.skipCodeUnitsWhile(n=>n!==r.GREATER_THAN),t.consumeCodeUnit(r.GREATER_THAN)}function g(t,n){const s=t.getPos();t.advance(1);const[o,e]=t.consumeQName();n({type:"ElementStart",prefix:o,local:e,start:s});let a=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const d=t.getPos(),l=t.currCodeUnit();if(l===r.SLASH){t.advance(1),t.consumeCodeUnit(r.GREATER_THAN);const p=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Empty"},range:p});break}else if(l===r.GREATER_THAN){t.advance(1);const p=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Open"},range:p}),a=!0;break}else{if(!c)throw new i.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[p,C,S]=u(t),k=t.getPos();n({type:"Attribute",range:{start:d,end:k},prefix:p,local:C,value:S})}}a&&x(t,n)}function u(t){const[n,s]=t.consumeQName();t.consumeEq();const o=t.consumeQuote(),e=String.fromCharCode(o),a=t.getPos();t.skipCharsWhile((d,l)=>l!==e&&l!=="<");const c=t.sliceBack(a);return t.consumeCodeUnit(o),[n,s,c]}function x(t,n){for(;!t.atEnd();)if(t.currCodeUnit()===r.LESS_THAN){const s=t.nextCodeUnit();if(s===r.EXCLAMATION_MARK)if(t.startsWith("<!--"))h(t,n);else if(t.startsWith("<![CDATA["))N(t,n);else throw new i.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===r.QUESTION_MARK)f(t,n);else if(s===r.SLASH){R(t,n);break}else g(t,n)}else I(t,n)}function N(t,n){const s=t.getPos();t.advance(9);const o=t.consumeCharsWhile((a,c)=>!(c==="]"&&a.startsWith("]]>")));t.skipString("]]>");const e=t.rangeFrom(s);n({type:"Cdata",text:o,range:e})}function R(t,n){const s=t.getPos();t.advance(2);const[o,e]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r.GREATER_THAN);const a=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:e},range:a})}function I(t,n){const s=t.getPos(),o=t.consumeCharsWhile((e,a)=>a!=="<");if(o.includes(">")&&o.includes("]]>"))throw new i.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)})}exports.parseXmlStream=U;
1
+ "use strict";var r=require("./utils.js"),i=require("./XmlError.js");function U(t,n,s){if(t.startsWith("\uFEFF")&&t.advance(1),t.startsWith("<?xml ")&&P(t),m(t,s),t.skipSpaces(),t.startsWith("<!DOCTYPE")){if(!n)throw new i.XmlError({type:"DtdDetected"});w(t,s),m(t,s)}if(t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===r.LESS_THAN&&g(t,s),m(t,s),!t.atEnd())throw new i.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!--"))h(t,n);else if(t.startsWith("<?"))f(t,n);else break}function P(t){if(t.advance(5),E(t),!t.startsWith("version"))throw new i.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());d(t),E(t),t.startsWith("encoding")&&(d(t),E(t)),t.startsWith("standalone")&&d(t),t.skipSpaces(),t.skipString("?>")}function E(t){if(t.startsWithSpace())t.skipSpaces();else if(!t.startsWith("?>")&&!t.atEnd())throw new i.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos())}function h(t,n){const s=t.getPos();t.advance(4);const o=t.consumeCharsWhile((e,a)=>!(a==="-"&&e.startsWith("-->")));if(t.skipString("-->"),o.includes("--")||o.endsWith("-"))throw new i.XmlError({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:o,range:t.rangeFrom(s)})}function f(t,n){if(t.startsWith("<?xml "))throw new i.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const e=t.consumeCharsWhile((a,c)=>!(c==="?"&&a.startsWith("?>")));t.skipString("?>"),n({type:"ProcessingInstruction",target:o,content:e.length===0?void 0:e,range:t.rangeFrom(s)})}function w(t,n){const s=t.getPos();if(A(t),t.skipSpaces(),t.currCodeUnit()===r.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!ENTITY"))W(t,n);else if(t.startsWith("<!--"))h(t,n);else if(t.startsWith("<?"))f(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r.GREATER_THAN){t.advance(1);break}else throw new i.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith("<!ELEMENT")||t.startsWith("<!ATTLIST")||t.startsWith("<!NOTATION"))try{v(t)}catch(o){throw new i.XmlError({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new i.XmlError({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function A(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),T(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==r.OPEN_BRACKET&&n!==r.GREATER_THAN)throw new i.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function T(t){if(t.startsWith("SYSTEM")||t.startsWith("PUBLIC")){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(e=>e!==o),t.consumeCodeUnit(o),s==="PUBLIC"){t.consumeSpaces();const e=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==e),t.consumeCodeUnit(e)}return!0}return!1}function W(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(r.PERCENT);s||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const e=y(t,s);e!==null&&n({type:"EntityDeclaration",name:o,definition:e}),t.skipSpaces(),t.consumeCodeUnit(r.GREATER_THAN)}function y(t,n){const s=t.currCodeUnit();if(s===r.DOUBLE_QUOTE||s===r.SINGLE_QUOTE){const o=t.consumeQuote(),e=t.getPos();t.skipCodeUnitsWhile(c=>c!==o);const a=t.sliceBack(e);return t.consumeCodeUnit(o),a}else if(s===r.UPPERCASE_S||s===r.UPPERCASE_P){if(T(t))return n&&(t.skipSpaces(),t.startsWith("NDATA")&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new i.XmlError({type:"InvalidExternalID"},t.genTextPos())}else throw new i.XmlError({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function v(t){t.skipCodeUnitsWhile(n=>n!==r.GREATER_THAN),t.consumeCodeUnit(r.GREATER_THAN)}function g(t,n){const s=t.getPos();t.advance(1);const[o,e]=t.consumeQName();n({type:"ElementStart",prefix:o,local:e,start:s});let a=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const l=t.getPos(),u=t.currCodeUnit();if(u===r.SLASH){t.advance(1),t.consumeCodeUnit(r.GREATER_THAN);const p=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Empty"},range:p});break}else if(u===r.GREATER_THAN){t.advance(1);const p=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Open"},range:p}),a=!0;break}else{if(!c)throw new i.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[p,C,S]=d(t),k=t.getPos();n({type:"Attribute",range:{start:l,end:k},prefix:p,local:C,value:S})}}a&&x(t,n)}function d(t){const[n,s]=t.consumeQName();let o;const e=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(r.EQUALS)){t.skipSpaces();const a=t.consumeQuote(),c=String.fromCharCode(a),l=t.getPos();t.skipCharsWhile((u,p)=>p!==c&&p!=="<"),o=t.sliceBack(l),t.consumeCodeUnit(a)}else t.goTo(e),o="true";return[n,s,o]}function x(t,n){for(;!t.atEnd();)if(t.currCodeUnit()===r.LESS_THAN){const s=t.nextCodeUnit();if(s===r.EXCLAMATION_MARK)if(t.startsWith("<!--"))h(t,n);else if(t.startsWith("<![CDATA["))N(t,n);else throw new i.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===r.QUESTION_MARK)f(t,n);else if(s===r.SLASH){R(t,n);break}else g(t,n)}else I(t,n)}function N(t,n){const s=t.getPos();t.advance(9);const o=t.consumeCharsWhile((a,c)=>!(c==="]"&&a.startsWith("]]>")));t.skipString("]]>");const e=t.rangeFrom(s);n({type:"Cdata",text:o,range:e})}function R(t,n){const s=t.getPos();t.advance(2);const[o,e]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r.GREATER_THAN);const a=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:e},range:a})}function I(t,n){const s=t.getPos(),o=t.consumeCharsWhile((e,a)=>a!=="<");if(o.includes(">")&&o.includes("]]>"))throw new i.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)})}exports.parseXmlStream=U;
@@ -1 +1 @@
1
- import{isXmlChar as u,isXmlSpaceByte as _,AMPERSAND as m,HASH as C,LOWERCASE_X as f,ZERO as U,NINE as x,UPPERCASE_A as w,UPPERCASE_F as y,LOWERCASE_A as k,LOWERCASE_F as g,isAsciiDigit as E,SEMICOLON as P,isXmlNameStart as a,isXmlName as d,COLON as S,EQUALS as v,SINGLE_QUOTE as N,DOUBLE_QUOTE as A,LINE_FEED as T}from"./utils.js";import{XmlError as n}from"./XmlError.js";var W=Object.defineProperty,F=(h,t,e)=>t in h?W(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,c=(h,t,e)=>F(h,typeof t!="symbol"?t+"":t,e);class p{constructor(t,e=0){c(this,"_text"),c(this,"_pos"),c(this,"_end"),this._text=t,this._pos=e,this._end=this._text.length}clone(){return new p(this._text,this._pos)}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new n({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new n({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}advance(t){this._pos+=t}startsWith(t){return this._text.startsWith(t,this._pos)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new n({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}skipString(t){if(!this.startsWith(t))throw new n({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked());)this._pos+=1}consumeCharsWhile(t){const e=this._pos;return this.skipCharsWhile(t),this.sliceBack(e)}skipCharsWhile(t){for(;this._pos<this._end;){const e=this._text[this._pos];if(e==null||!u(e.codePointAt(0)))throw new n({type:"NonXmlChar",char:e!=null?e:"\uFFFD"},this.genTextPos());if(t(this,e))this._pos+=e.length;else break}}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&&_(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new n({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new n({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(m))return null;let t;if(this.tryConsumeCodeUnit(C)){let e,i;this.tryConsumeCodeUnit(f)?(e=this.consumeCodeUnitsWhile(s=>s>=U&&s<=x||s>=w&&s<=y||s>=k&&s<=g),i=16):(e=this.consumeCodeUnitsWhile(s=>E(s)),i=10);const o=parseInt(e,i);isNaN(o)||!u(o)?t=null:t={type:"Char",value:String.fromCodePoint(o)}}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(P)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new n({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(e))throw new n({type:"InvalidName"},this.genTextPosFrom(t))}else if(!d(e))break;this._pos+=1}}consumeQName(){var t,e;const i=this._pos;let o=null;for(;this._pos<this._end;){const l=this.currCodeUnitUnchecked();if(l===S)if(o==null)o=this._pos,this._pos+=1;else throw new n({type:"InvalidName"},this.genTextPosFrom(i));else if(d(l))this._pos+=1;else break}let s,r;if(o!=null?(s=this._text.slice(i,o),r=this.sliceBack(o+1)):(s="",r=this.sliceBack(i)),s.length>0&&!a((t=s[0])==null?void 0:t.codePointAt(0)))throw new n({type:"InvalidName"},this.genTextPosFrom(i));if(r.length>0){if(!a((e=r[0])==null?void 0:e.codePointAt(0)))throw new n({type:"InvalidName"},this.genTextPosFrom(i))}else throw new n({type:"InvalidName"},this.genTextPosFrom(i));return[s,r]}consumeEq(){this.skipSpaces(),this.consumeCodeUnit(v),this.skipSpaces()}consumeQuote(){const t=this.currCodeUnit();if(t===N||t===A)return this._pos+=1,t;throw new n({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,o=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===T?(i++,o=1):o++;return{row:i,col:o}}}export{p as XmlStream};
1
+ import{isXmlChar as u,isXmlSpaceByte as _,AMPERSAND as m,HASH as C,LOWERCASE_X as f,ZERO as x,NINE as U,UPPERCASE_A as w,UPPERCASE_F as y,LOWERCASE_A as g,LOWERCASE_F as k,isAsciiDigit as E,SEMICOLON as P,isXmlNameStart as a,isXmlName as d,COLON as v,SINGLE_QUOTE as N,DOUBLE_QUOTE as S,LINE_FEED as A}from"./utils.js";import{XmlError as n}from"./XmlError.js";var T=Object.defineProperty,W=(h,t,e)=>t in h?T(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,c=(h,t,e)=>W(h,typeof t!="symbol"?t+"":t,e);class l{constructor(t,e=0){c(this,"_text"),c(this,"_pos"),c(this,"_end"),this._text=t,this._pos=e,this._end=this._text.length}clone(){return new l(this._text,this._pos)}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new n({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new n({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}advance(t){this._pos+=t}goTo(t){this._pos=t}startsWith(t){return this._text.startsWith(t,this._pos)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new n({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}skipString(t){if(!this.startsWith(t))throw new n({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked());)this._pos+=1}consumeCharsWhile(t){const e=this._pos;return this.skipCharsWhile(t),this.sliceBack(e)}skipCharsWhile(t){for(;this._pos<this._end;){const e=this._text[this._pos];if(e==null||!u(e.codePointAt(0)))throw new n({type:"NonXmlChar",char:e!=null?e:"\uFFFD"},this.genTextPos());if(t(this,e))this._pos+=e.length;else break}}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&&_(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new n({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new n({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(m))return null;let t;if(this.tryConsumeCodeUnit(C)){let e,i;this.tryConsumeCodeUnit(f)?(e=this.consumeCodeUnitsWhile(s=>s>=x&&s<=U||s>=w&&s<=y||s>=g&&s<=k),i=16):(e=this.consumeCodeUnitsWhile(s=>E(s)),i=10);const o=parseInt(e,i);isNaN(o)||!u(o)?t=null:t={type:"Char",value:String.fromCodePoint(o)}}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(P)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new n({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(e))throw new n({type:"InvalidName"},this.genTextPosFrom(t))}else if(!d(e))break;this._pos+=1}}consumeQName(){var t,e;const i=this._pos;let o=null;for(;this._pos<this._end;){const p=this.currCodeUnitUnchecked();if(p===v)if(o==null)o=this._pos,this._pos+=1;else throw new n({type:"InvalidName"},this.genTextPosFrom(i));else if(d(p))this._pos+=1;else break}let s,r;if(o!=null?(s=this._text.slice(i,o),r=this.sliceBack(o+1)):(s="",r=this.sliceBack(i)),s.length>0&&!a((t=s[0])==null?void 0:t.codePointAt(0)))throw new n({type:"InvalidName"},this.genTextPosFrom(i));if(r.length>0){if(!a((e=r[0])==null?void 0:e.codePointAt(0)))throw new n({type:"InvalidName"},this.genTextPosFrom(i))}else throw new n({type:"InvalidName"},this.genTextPosFrom(i));return[s,r]}consumeQuote(){const t=this.currCodeUnit();if(t===N||t===S)return this._pos+=1,t;throw new n({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,o=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===A?(i++,o=1):o++;return{row:i,col:o}}}export{l as XmlStream};
@@ -1 +1 @@
1
- import{LESS_THAN as C,GREATER_THAN as r,OPEN_BRACKET as w,PERCENT as W,DOUBLE_QUOTE as x,SINGLE_QUOTE as y,UPPERCASE_S as v,UPPERCASE_P as I,SLASH as S,EXCLAMATION_MARK as N,QUESTION_MARK as A}from"./utils.js";import{XmlError as i}from"./XmlError.js";function F(t,n,s){if(t.startsWith("\uFEFF")&&t.advance(1),t.startsWith("<?xml ")&&D(t),m(t,s),t.skipSpaces(),t.startsWith("<!DOCTYPE")){if(!n)throw new i({type:"DtdDetected"});L(t,s),m(t,s)}if(t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===C&&k(t,s),m(t,s),!t.atEnd())throw new i({type:"UnknownToken",message:"Not at end"},t.genTextPos())}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!--"))f(t,n);else if(t.startsWith("<?"))g(t,n);else break}function D(t){if(t.advance(5),h(t),!t.startsWith("version"))throw new i({type:"InvalidString",expected:"version"},t.genTextPos());l(t),h(t),t.startsWith("encoding")&&(l(t),h(t)),t.startsWith("standalone")&&l(t),t.skipSpaces(),t.skipString("?>")}function h(t){if(t.startsWithSpace())t.skipSpaces();else if(!t.startsWith("?>")&&!t.atEnd())throw new i({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos())}function f(t,n){const s=t.getPos();t.advance(4);const o=t.consumeCharsWhile((e,a)=>!(a==="-"&&e.startsWith("-->")));if(t.skipString("-->"),o.includes("--")||o.endsWith("-"))throw new i({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:o,range:t.rangeFrom(s)})}function g(t,n){if(t.startsWith("<?xml "))throw new i({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const e=t.consumeCharsWhile((a,c)=>!(c==="?"&&a.startsWith("?>")));t.skipString("?>"),n({type:"ProcessingInstruction",target:o,content:e.length===0?void 0:e,range:t.rangeFrom(s)})}function L(t,n){const s=t.getPos();if(O(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!ENTITY"))Q(t,n);else if(t.startsWith("<!--"))f(t,n);else if(t.startsWith("<?"))g(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);break}else throw new i({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith("<!ELEMENT")||t.startsWith("<!ATTLIST")||t.startsWith("<!NOTATION"))try{B(t)}catch(o){throw new i({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new i({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function O(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),E(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==w&&n!==r)throw new i({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function E(t){if(t.startsWith("SYSTEM")||t.startsWith("PUBLIC")){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(e=>e!==o),t.consumeCodeUnit(o),s==="PUBLIC"){t.consumeSpaces();const e=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==e),t.consumeCodeUnit(e)}return!0}return!1}function Q(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(W);s||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const e=_(t,s);e!==null&&n({type:"EntityDeclaration",name:o,definition:e}),t.skipSpaces(),t.consumeCodeUnit(r)}function _(t,n){const s=t.currCodeUnit();if(s===x||s===y){const o=t.consumeQuote(),e=t.getPos();t.skipCodeUnitsWhile(c=>c!==o);const a=t.sliceBack(e);return t.consumeCodeUnit(o),a}else if(s===v||s===I){if(E(t))return n&&(t.skipSpaces(),t.startsWith("NDATA")&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new i({type:"InvalidExternalID"},t.genTextPos())}else throw new i({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function B(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function k(t,n){const s=t.getPos();t.advance(1);const[o,e]=t.consumeQName();n({type:"ElementStart",prefix:o,local:e,start:s});let a=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const u=t.getPos(),p=t.currCodeUnit();if(p===S){t.advance(1),t.consumeCodeUnit(r);const d=t.rangeFrom(u);n({type:"ElementEnd",end:{type:"Empty"},range:d});break}else if(p===r){t.advance(1);const d=t.rangeFrom(u);n({type:"ElementEnd",end:{type:"Open"},range:d}),a=!0;break}else{if(!c)throw new i({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[d,U,T]=l(t),P=t.getPos();n({type:"Attribute",range:{start:u,end:P},prefix:d,local:U,value:T})}}a&&R(t,n)}function l(t){const[n,s]=t.consumeQName();t.consumeEq();const o=t.consumeQuote(),e=String.fromCharCode(o),a=t.getPos();t.skipCharsWhile((u,p)=>p!==e&&p!=="<");const c=t.sliceBack(a);return t.consumeCodeUnit(o),[n,s,c]}function R(t,n){for(;!t.atEnd();)if(t.currCodeUnit()===C){const s=t.nextCodeUnit();if(s===N)if(t.startsWith("<!--"))f(t,n);else if(t.startsWith("<![CDATA["))b(t,n);else throw new i({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===A)g(t,n);else if(s===S){M(t,n);break}else k(t,n)}else Y(t,n)}function b(t,n){const s=t.getPos();t.advance(9);const o=t.consumeCharsWhile((a,c)=>!(c==="]"&&a.startsWith("]]>")));t.skipString("]]>");const e=t.rangeFrom(s);n({type:"Cdata",text:o,range:e})}function M(t,n){const s=t.getPos();t.advance(2);const[o,e]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const a=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:e},range:a})}function Y(t,n){const s=t.getPos(),o=t.consumeCharsWhile((e,a)=>a!=="<");if(o.includes(">")&&o.includes("]]>"))throw new i({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)})}export{F as parseXmlStream};
1
+ import{LESS_THAN as C,GREATER_THAN as r,OPEN_BRACKET as w,PERCENT as W,DOUBLE_QUOTE as y,SINGLE_QUOTE as x,UPPERCASE_S as v,UPPERCASE_P as I,SLASH as S,EQUALS as N,EXCLAMATION_MARK as A,QUESTION_MARK as F}from"./utils.js";import{XmlError as i}from"./XmlError.js";function L(t,n,s){if(t.startsWith("\uFEFF")&&t.advance(1),t.startsWith("<?xml ")&&Q(t),m(t,s),t.skipSpaces(),t.startsWith("<!DOCTYPE")){if(!n)throw new i({type:"DtdDetected"});D(t,s),m(t,s)}if(t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===C&&E(t,s),m(t,s),!t.atEnd())throw new i({type:"UnknownToken",message:"Not at end"},t.genTextPos())}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!--"))f(t,n);else if(t.startsWith("<?"))g(t,n);else break}function Q(t){if(t.advance(5),h(t),!t.startsWith("version"))throw new i({type:"InvalidString",expected:"version"},t.genTextPos());u(t),h(t),t.startsWith("encoding")&&(u(t),h(t)),t.startsWith("standalone")&&u(t),t.skipSpaces(),t.skipString("?>")}function h(t){if(t.startsWithSpace())t.skipSpaces();else if(!t.startsWith("?>")&&!t.atEnd())throw new i({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos())}function f(t,n){const s=t.getPos();t.advance(4);const o=t.consumeCharsWhile((e,a)=>!(a==="-"&&e.startsWith("-->")));if(t.skipString("-->"),o.includes("--")||o.endsWith("-"))throw new i({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:o,range:t.rangeFrom(s)})}function g(t,n){if(t.startsWith("<?xml "))throw new i({type:"UnexpectedDeclaration"},t.genTextPos());const s=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const e=t.consumeCharsWhile((a,c)=>!(c==="?"&&a.startsWith("?>")));t.skipString("?>"),n({type:"ProcessingInstruction",target:o,content:e.length===0?void 0:e,range:t.rangeFrom(s)})}function D(t,n){const s=t.getPos();if(O(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith("<!ENTITY"))_(t,n);else if(t.startsWith("<!--"))f(t,n);else if(t.startsWith("<?"))g(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);break}else throw new i({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith("<!ELEMENT")||t.startsWith("<!ATTLIST")||t.startsWith("<!NOTATION"))try{R(t)}catch(o){throw new i({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new i({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function O(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),k(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==w&&n!==r)throw new i({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function k(t){if(t.startsWith("SYSTEM")||t.startsWith("PUBLIC")){const n=t.getPos();t.advance(6);const s=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(e=>e!==o),t.consumeCodeUnit(o),s==="PUBLIC"){t.consumeSpaces();const e=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==e),t.consumeCodeUnit(e)}return!0}return!1}function _(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(W);s||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const e=B(t,s);e!==null&&n({type:"EntityDeclaration",name:o,definition:e}),t.skipSpaces(),t.consumeCodeUnit(r)}function B(t,n){const s=t.currCodeUnit();if(s===y||s===x){const o=t.consumeQuote(),e=t.getPos();t.skipCodeUnitsWhile(c=>c!==o);const a=t.sliceBack(e);return t.consumeCodeUnit(o),a}else if(s===v||s===I){if(k(t))return n&&(t.skipSpaces(),t.startsWith("NDATA")&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new i({type:"InvalidExternalID"},t.genTextPos())}else throw new i({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function R(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function E(t,n){const s=t.getPos();t.advance(1);const[o,e]=t.consumeQName();n({type:"ElementStart",prefix:o,local:e,start:s});let a=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const d=t.getPos(),l=t.currCodeUnit();if(l===S){t.advance(1),t.consumeCodeUnit(r);const p=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Empty"},range:p});break}else if(l===r){t.advance(1);const p=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Open"},range:p}),a=!0;break}else{if(!c)throw new i({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[p,U,T]=u(t),P=t.getPos();n({type:"Attribute",range:{start:d,end:P},prefix:p,local:U,value:T})}}a&&b(t,n)}function u(t){const[n,s]=t.consumeQName();let o;const e=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(N)){t.skipSpaces();const a=t.consumeQuote(),c=String.fromCharCode(a),d=t.getPos();t.skipCharsWhile((l,p)=>p!==c&&p!=="<"),o=t.sliceBack(d),t.consumeCodeUnit(a)}else t.goTo(e),o="true";return[n,s,o]}function b(t,n){for(;!t.atEnd();)if(t.currCodeUnit()===C){const s=t.nextCodeUnit();if(s===A)if(t.startsWith("<!--"))f(t,n);else if(t.startsWith("<![CDATA["))M(t,n);else throw new i({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===F)g(t,n);else if(s===S){Y(t,n);break}else E(t,n)}else H(t,n)}function M(t,n){const s=t.getPos();t.advance(9);const o=t.consumeCharsWhile((a,c)=>!(c==="]"&&a.startsWith("]]>")));t.skipString("]]>");const e=t.rangeFrom(s);n({type:"Cdata",text:o,range:e})}function Y(t,n){const s=t.getPos();t.advance(2);const[o,e]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const a=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:e},range:a})}function H(t,n){const s=t.getPos(),o=t.consumeCharsWhile((e,a)=>a!=="<");if(o.includes(">")&&o.includes("]]>"))throw new i({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)})}export{L as parseXmlStream};
@@ -55,6 +55,12 @@ export declare class XmlStream {
55
55
  * @param n - The number of characters to advance.
56
56
  */
57
57
  advance(n: number): void;
58
+ /**
59
+ * Go to a new position in the stream.
60
+ *
61
+ * @param pos - The new position.
62
+ */
63
+ goTo(pos: number): void;
58
64
  /**
59
65
  * Checks if the stream starts with the given text.
60
66
  *
@@ -178,10 +184,6 @@ export declare class XmlStream {
178
184
  * @throws XmlError if an invalid qualified XML name is encountered.
179
185
  */
180
186
  consumeQName(): [string, string];
181
- /**
182
- * Consumes an equal sign, surrounded by optional whitespace.
183
- */
184
- consumeEq(): void;
185
187
  /**
186
188
  * Consumes a quote character.
187
189
  *
@@ -1 +1 @@
1
- {"version":3,"file":"XmlStream.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/XmlStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,UAAU,EAAE,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAyBtE;;;;;;GAMG;AACH,qBAAa,SAAS;IACrB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,IAAI,CAAS;gBAEF,IAAI,EAAE,MAAM,EAAE,GAAG,SAAI;IAMxC;;;;OAIG;IACI,KAAK,IAAI,SAAS;IAIzB;;;;OAIG;IACI,MAAM,IAAI,MAAM;IAIvB;;;;OAIG;IACI,KAAK,IAAI,OAAO;IAIvB;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAO7B;;;;OAIG;IACI,qBAAqB,IAAI,MAAM;IAItC;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAO7B;;;;OAIG;IACI,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAI/B;;;;;OAKG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIxC;;;;;OAKG;IACI,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAW9C;;;;;;OAMG;IACI,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAQpD;;;;;OAKG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOrC;;;;;OAKG;IACI,qBAAqB,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM;IAM9E;;;;OAIG;IACI,kBAAkB,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAMzE;;;;;;OAMG;IACI,iBAAiB,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM;IAMzF;;;;;OAKG;IACI,cAAc,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAapF;;;;;OAKG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIrC;;;;;OAKG;IACI,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIvC;;OAEG;IACI,UAAU,IAAI,IAAI;IAMzB;;;;OAIG;IACI,eAAe,IAAI,OAAO;IAIjC;;;;;OAKG;IACI,aAAa,IAAI,IAAI;IAe5B;;;;;OAKG;IACI,mBAAmB,IAAI,UAAU,GAAG,IAAI;IAiB/C;;;;OAIG;IACI,gBAAgB,IAAI,UAAU,GAAG,IAAI;IA4D5C;;;;;;OAMG;IACI,WAAW,IAAI,MAAM;IAY5B;;;;OAIG;IACI,QAAQ,IAAI,IAAI;IAgBvB;;;;;;OAMG;IACI,YAAY,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IAiDvC;;OAEG;IACI,SAAS,IAAI,IAAI;IAMxB;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAY7B;;;;;OAKG;IACI,UAAU,IAAI,QAAQ;IAI7B;;;;;;OAMG;IACI,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ;CAc5C"}
1
+ {"version":3,"file":"XmlStream.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/XmlStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,UAAU,EAAE,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAwBtE;;;;;;GAMG;AACH,qBAAa,SAAS;IACrB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,IAAI,CAAS;gBAEF,IAAI,EAAE,MAAM,EAAE,GAAG,SAAI;IAMxC;;;;OAIG;IACI,KAAK,IAAI,SAAS;IAIzB;;;;OAIG;IACI,MAAM,IAAI,MAAM;IAIvB;;;;OAIG;IACI,KAAK,IAAI,OAAO;IAIvB;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAO7B;;;;OAIG;IACI,qBAAqB,IAAI,MAAM;IAItC;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAO7B;;;;OAIG;IACI,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAI/B;;;;OAIG;IACI,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAI9B;;;;;OAKG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIxC;;;;;OAKG;IACI,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAW9C;;;;;;OAMG;IACI,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAQpD;;;;;OAKG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOrC;;;;;OAKG;IACI,qBAAqB,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM;IAM9E;;;;OAIG;IACI,kBAAkB,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAMzE;;;;;;OAMG;IACI,iBAAiB,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM;IAMzF;;;;;OAKG;IACI,cAAc,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAapF;;;;;OAKG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIrC;;;;;OAKG;IACI,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIvC;;OAEG;IACI,UAAU,IAAI,IAAI;IAMzB;;;;OAIG;IACI,eAAe,IAAI,OAAO;IAIjC;;;;;OAKG;IACI,aAAa,IAAI,IAAI;IAe5B;;;;;OAKG;IACI,mBAAmB,IAAI,UAAU,GAAG,IAAI;IAiB/C;;;;OAIG;IACI,gBAAgB,IAAI,UAAU,GAAG,IAAI;IA4D5C;;;;;;OAMG;IACI,WAAW,IAAI,MAAM;IAY5B;;;;OAIG;IACI,QAAQ,IAAI,IAAI;IAgBvB;;;;;;OAMG;IACI,YAAY,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IAiDvC;;;;;OAKG;IACI,YAAY,IAAI,MAAM;IAY7B;;;;;OAKG;IACI,UAAU,IAAI,QAAQ;IAI7B;;;;;;OAMG;IACI,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ;CAc5C"}
@@ -1 +1 @@
1
- {"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/tokenizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAC;AAe9C,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC7B,CAAC,EAAE,SAAS,EACZ,QAAQ,EAAE,OAAO,EACjB,aAAa,EAAE,cAAc,GAC3B,IAAI,CAgCN"}
1
+ {"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../../../src/tokenizer/tokenizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAC;AAgB9C,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC7B,CAAC,EAAE,SAAS,EACZ,QAAQ,EAAE,OAAO,EACjB,aAAa,EAAE,cAAc,GAC3B,IAAI,CAgCN"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-tokenizer",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Straightforward and typesafe XML tokenizer that streams tokens through a callback mechanism",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -26,9 +26,8 @@
26
26
  "sax": "^1.4.1",
27
27
  "saxen": "^10.0.0",
28
28
  "txml": "^5.1.1",
29
- "xml-tokenizer": "^0.0.2",
30
29
  "xml2js": "^0.6.2",
31
- "@blgc/config": "0.0.16"
30
+ "@blgc/config": "0.0.17"
32
31
  },
33
32
  "files": [
34
33
  "dist",