xml-tokenizer 0.0.35 → 0.0.37
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
|
|
1
|
+
"use strict";var o=require("./ascii-constants.js"),c=require("./utils.js"),n=require("./XmlError.js"),C=Object.defineProperty,g=Object.getOwnPropertySymbols,x=Object.prototype.hasOwnProperty,E=Object.prototype.propertyIsEnumerable,m=(a,t,e)=>t in a?C(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,f=(a,t)=>{for(var e in t||(t={}))x.call(t,e)&&m(a,e,t[e]);if(g)for(var e of g(t))E.call(t,e)&&m(a,e,t[e]);return a},d=(a,t,e)=>m(a,typeof t!="symbol"?t+"":t,e);class _{constructor(t,e={}){d(this,"_text"),d(this,"_pos"),d(this,"_end"),d(this,"config");const{pos:i=0,strictDocument:r=!0,allowDtd:s=!0,rawTextElements:p=null,implicitSelfClosingElements:h=null,contextSliceSize:l=!1,debugLogger:u}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:r,allowDtd:s,rawTextElements:p,implicitSelfClosingElements:h,contextSliceSize:l,debugLogger:u},this.debug("XmlStream initialized",{textLength:this._text.length,initialPos:i,platform:process.platform,nodeVersion:process.version,textSample:this._text.slice(0,Math.min(100,this._text.length))+(this._text.length>100?"...":"")})}debug(t,...e){this.config.debugLogger&&this.config.debugLogger(t,...e)}clone(){return new _(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 n.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 n.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new n.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(!c.isXmlChar(this.currCodeUnitUnchecked()))throw new n.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),r=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==r)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new n.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&&c.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new n.XmlError({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(o.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(o.HASH)){let e,i;this.tryConsumeCodeUnit(o.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(s=>s>=o.ZERO&&s<=o.NINE||s>=o.UPPERCASE_A&&s<=o.UPPERCASE_F||s>=o.LOWERCASE_A&&s<=o.LOWERCASE_F),i=16):(e=this.consumeCodeUnitsWhile(s=>c.isAsciiDigit(s)),i=10);const r=parseInt(e,i);isNaN(r)||!c.isXmlChar(r)?t=null:t={type:"Char",value:String.fromCodePoint(r)}}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(o.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new n.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(!c.isXmlNameStart(e))throw new n.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!c.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e,i,r;const s=this._pos;let p=null;for(this.debug("Starting consumeQName",{position:this._pos,nextChars:this._text.slice(this._pos,this._pos+10)});this._pos<this._end;){const u=this.currCodeUnitUnchecked();if(u===o.COLON)if(p==null)p=this._pos,this._pos+=1,this.debug("Found colon splitter",{position:this._pos});else throw this.debug("Multiple colons found - error",{position:this._pos}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));else if(c.isXmlName(u))this._pos+=1;else break}let h,l;if(p!=null?(h=this._text.slice(s,p),l=this.sliceBack(p+1)):(h="",l=this.sliceBack(s)),this.debug("Finished consumeQName",{prefix:h,local:l,startPos:s,endPos:this._pos}),h.length>0&&!c.isXmlNameStart((t=h[0])==null?void 0:t.codePointAt(0)))throw this.debug("Invalid prefix start character",{prefix:h,firstChar:h[0],codePoint:(e=h[0])==null?void 0:e.codePointAt(0)}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));if(l.length>0){if(!c.isXmlNameStart((i=l[0])==null?void 0:i.codePointAt(0)))throw this.debug("Invalid local name start character",{local:l,firstChar:l[0],codePoint:(r=l[0])==null?void 0:r.codePointAt(0)}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debug("Empty local name - error"),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));return[h,l]}consumeQuote(){const t=this.currCodeUnit();if(t===o.SINGLE_QUOTE||t===o.DOUBLE_QUOTE)return this._pos+=1,t;throw new n.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),r=Math.max(0,t-i),s=Math.min(this._end,t+i+e%2);return this._text.slice(r,s)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,r=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===o.LINE_FEED?(i++,r=1):r++;return f({row:i,col:r},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}getTextSnippet(t,e){return this._text.slice(t,t+e)}}exports.XmlStream=_;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`).slice(0,10):void 0}),o}}function p(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(f))E(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(m))S(t,n);else if(t.startsWith(A))h(t,n);else break}function J(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(y)&&!e.atEnd())throw new a.XmlError({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(q))throw new a.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());d(t),n(t),t.startsWith(z)&&(d(t),n(t)),t.startsWith(K)&&d(t),t.skipSpaces(),t.skipString(y)}function E(t,n){const e=t.getPos();t.advance(4);const o=t.consumeCodeUnitsWhile((i,c)=>!(i===s.HYPHEN&&c.startsWith(P)));if(t.skipString(P),o.includes("--")||o.endsWith("-"))throw new a.XmlError({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:o,range:t.rangeFrom(e)},t)}function h(t,n){if(t.startsWith(U))throw new a.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const i=t.consumeCodeUnitsWhile((c,r)=>!(c===s.QUESTION_MARK&&r.startsWith(w)));t.skipString(w),n({type:"ProcessingInstruction",target:o,content:i.length===0?void 0:i,range:t.rangeFrom(e)},t)}function S(t,n){if(!t.config.allowDtd)throw new a.XmlError({type:"DtdDetected"});const e=t.getPos();if(V(t),t.skipSpaces(),t.currCodeUnit()===s.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(Q))$(t,n);else if(t.startsWith(f))E(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 a.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(F)||t.startsWith(O)||t.startsWith(b))try{Z(t)}catch(o){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 V(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),v(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==s.OPEN_SQUARE_BRACKET&&n!==s.GREATER_THAN)throw new a.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function v(t){if(t.startsWith(M)||t.startsWith(x)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o),e===x){t.consumeSpaces();const i=t.consumeQuote();t.consumeCodeUnitsWhile(c=>c!==i),t.consumeCodeUnit(i)}return!0}return!1}function $(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(s.PERCENT);e||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const i=j(t,e);i!==null&&n({type:"EntityDeclaration",name:o,definition:i},t),t.skipSpaces(),t.consumeCodeUnit(s.GREATER_THAN)}function j(t,n){const e=t.currCodeUnit();if(e===s.DOUBLE_QUOTE||e===s.SINGLE_QUOTE){const o=t.consumeQuote(),i=t.getPos();t.skipCodeUnitsWhile(r=>r!==o);const c=t.sliceBack(i);return t.consumeCodeUnit(o),c}else if(e===s.UPPERCASE_S||e===s.UPPERCASE_P){if(v(t))return n&&(t.skipSpaces(),t.startsWith(B)&&(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 Z(t){t.skipCodeUnitsWhile(n=>n!==s.GREATER_THAN),t.consumeCodeUnit(s.GREATER_THAN)}function T(t,n){const e=t.getPos();if(t.config.debug){const r=t.currCodeUnit();t.debugLog("parseElement started",JSON.stringify({position:e,character:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),context:t.getTextAround(e,30),textPos:t.genTextPosFrom(e),platform:typeof process!="undefined"?process.arch:"unknown"},null,2))}t.advance(1);let o,i;try{[o,i]=t.consumeQName(),t.debugLog("parseElement consumeQName succeeded",JSON.stringify({prefix:o,local:i,position:t.getPos()},null,2)),n({type:"ElementStart",prefix:o,local:i,start:e},t)}catch(r){throw t.debugLog("parseElement consumeQName FAILED",JSON.stringify({error:r instanceof Error?r.message:String(r),errorType:r instanceof a.XmlError?r.variant.type:"unknown",position:t.getPos(),startPosition:e,context:t.getTextAround(t.getPos(),50),platform:typeof process!="undefined"?process.arch:"unknown",stackTrace:r instanceof Error?r.stack:void 0},null,2)),r}let c=!1;for(;!t.atEnd();){const r=t.startsWithSpace();t.skipSpaces();const l=t.getPos(),C=t.currCodeUnit();if(C===s.SLASH){t.advance(1),t.consumeCodeUnit(s.GREATER_THAN);const u=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else if(C===s.GREATER_THAN){t.advance(1);const u=t.rangeFrom(l);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(k.getQName(i,o))){n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else{n({type:"ElementEnd",end:{type:"Open"},range:u},t),c=!0;break}}else{if(!r)throw new a.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[u,R,X]=d(t),_=t.getPos();n({type:"Attribute",range:{start:l,end:_},prefix:u,local:R,value:X},t)}}if(c){if(t.config.rawTextElements!=null){const r=k.getQName(i,o);if(t.config.rawTextElements.includes(r)){W(t,n,`</${r}>`);return}}W(t,n)}}function d(t){const[n,e]=t.consumeQName();let o;const i=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(s.EQUALS)){t.skipSpaces();const c=t.currCodeUnit();if(c===s.SINGLE_QUOTE||c===s.DOUBLE_QUOTE){const r=t.consumeQuote();o=t.consumeCodeUnitsWhile(l=>l!==r&&l!==s.LESS_THAN),t.consumeCodeUnit(r)}else if(!t.config.strictDocument)o=t.consumeCodeUnitsWhile(r=>!I.isXmlSpaceByte(r)&&r!==s.GREATER_THAN&&r!==s.SLASH&&r!==s.LESS_THAN);else throw new a.XmlError({type:"InvalidChar",expected:"a quote",actual:c},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(i),o="true";else throw new a.XmlError({type:"InvalidChar",expected:s.EQUALS,actual:t.currCodeUnit()},t.genTextPos());return[n,e,o]}function W(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(f))E(t,n);else if(t.startsWith(G))tt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(m))S(t,n);else throw new a.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 T(t,n)}else L(t,n,e)}function tt(t,n){const e=t.getPos();t.advance(9);const o=t.consumeCodeUnitsWhile((c,r)=>!(c===s.CLOSE_SQUARE_BRACKET&&r.startsWith(g)));t.skipString(g);const i=t.rangeFrom(e);n({type:"Cdata",text:o,range:i},t)}function nt(t,n){const e=t.getPos();t.advance(2);const[o,i]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(s.GREATER_THAN);const c=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:i},range:c},t)}function L(t,n,e=s.LESS_THAN){const o=t.getPos(),i=typeof e=="number"?t.consumeCodeUnitsWhile(c=>c!==e):t.consumeCodeUnitsWhile((c,r)=>!r.startsWith(e));if(t.config.strictDocument&&i.includes(g))throw new a.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:i,range:t.rangeFrom(o)},t)}exports.tokenize=Y,exports.tokenizeXmlStream=N;
|
|
1
|
+
"use strict";var g=require("../get-q-name.js"),o=require("./ascii-constants.js"),I=require("./utils.js"),c=require("./XmlError.js"),L=require("./XmlStream.js");const F="\uFEFF",P="<?xml ",w="?>",H="<!ENTITY",Q="<!ELEMENT",D="<!ATTLIST",O="<!NOTATION",E="<!DOCTYPE",x="<?",W="?>",f="<!--",y="-->",G="<![CDATA[",h="]]>",M="NDATA",N="PUBLIC",B="SYSTEM",q="version",z="encoding",Y="standalone";function K(t,n,e={}){const s=new L.XmlStream(t,e);s.debug("Starting tokenization",{stringLength:t.length}),v(s,n)}function v(t,n){if(t.debug("Starting XML stream tokenization"),t.startsWith(F)&&(t.debug("Skipping BOM"),t.advance(1)),t.startsWith(P)&&(t.debug("Found XML declaration"),$(t)),m(t,n),t.config.strictDocument){if(t.debug("Parsing in strict document mode"),t.skipSpaces(),t.startsWithIgnoreCase(E)&&(t.debug("Found DOCTYPE declaration"),C(t,n),m(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===o.LESS_THAN&&(t.debug("Starting root element parsing"),U(t,n)),m(t,n),!t.atEnd())throw t.debug("Unexpected content after root element",{position:t.getPos(),nextChars:t.getTextSnippet(t.getPos(),10)}),new c.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(t.debug("Parsing in non-strict mode");!t.atEnd();)t.currCodeUnit()===o.LESS_THAN?U(t,n):_(t,n),m(t,n);t.debug("Finished XML stream tokenization")}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(f))S(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(E))C(t,n);else if(t.startsWith(x))T(t,n);else break}function $(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(w)&&!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(z)&&(p(t),n(t)),t.startsWith(Y)&&p(t),t.skipSpaces(),t.skipString(w)}function S(t,n){const e=t.getPos();t.advance(4);const s=t.consumeCodeUnitsWhile((i,a)=>!(i===o.HYPHEN&&a.startsWith(y)));if(t.skipString(y),s.includes("--")||s.endsWith("-"))throw new c.XmlError({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:s,range:t.rangeFrom(e)},t)}function T(t,n){if(t.startsWith(P))throw new c.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const s=t.consumeName();t.skipSpaces();const i=t.consumeCodeUnitsWhile((a,r)=>!(a===o.QUESTION_MARK&&r.startsWith(W)));t.skipString(W),n({type:"ProcessingInstruction",target:s,content:i.length===0?void 0:i,range:t.rangeFrom(e)},t)}function C(t,n){if(!t.config.allowDtd)throw new c.XmlError({type:"DtdDetected"});const e=t.getPos();if(j(t),t.skipSpaces(),t.currCodeUnit()===o.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(H))J(t,n);else if(t.startsWith(f))S(t,n);else if(t.startsWith(x))T(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===o.GREATER_THAN){t.advance(1);break}else throw new c.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(Q)||t.startsWith(D)||t.startsWith(O))try{Z(t)}catch(s){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(),b(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==o.OPEN_SQUARE_BRACKET&&n!==o.GREATER_THAN)throw new c.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function b(t){if(t.startsWith(B)||t.startsWith(N)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const s=t.consumeQuote();if(t.consumeCodeUnitsWhile(i=>i!==s),t.consumeCodeUnit(s),e===N){t.consumeSpaces();const i=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==i),t.consumeCodeUnit(i)}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 i=V(t,e);i!==null&&n({type:"EntityDeclaration",name:s,definition:i},t),t.skipSpaces(),t.consumeCodeUnit(o.GREATER_THAN)}function V(t,n){const e=t.currCodeUnit();if(e===o.DOUBLE_QUOTE||e===o.SINGLE_QUOTE){const s=t.consumeQuote(),i=t.getPos();t.skipCodeUnitsWhile(r=>r!==s);const a=t.sliceBack(i);return t.consumeCodeUnit(s),a}else if(e===o.UPPERCASE_S||e===o.UPPERCASE_P){if(b(t))return n&&(t.skipSpaces(),t.startsWith(M)&&(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!==o.GREATER_THAN),t.consumeCodeUnit(o.GREATER_THAN)}function U(t,n){const e=t.getPos();t.advance(1),t.debug("Starting element parsing",{position:e,nextChars:t.getTextSnippet(e,10)});const[s,i]=t.consumeQName();t.debug("Element name parsed",{prefix:s,local:i}),n({type:"ElementStart",prefix:s,local:i,start:e},t);let a=!1;for(;!t.atEnd();){const r=t.startsWithSpace();t.skipSpaces();const l=t.getPos(),u=t.currCodeUnit();if(t.debug("Processing element content",{position:l,currCodeUnit:u,hasSpace:r}),u===o.SLASH){t.debug("Found self-closing tag"),t.advance(1),t.consumeCodeUnit(o.GREATER_THAN);const d=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Empty"},range:d},t);break}else if(u===o.GREATER_THAN){t.advance(1);const d=t.rangeFrom(l);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(g.getQName(i,s))){t.debug("Found implicit self-closing element",{element:g.getQName(i,s)}),n({type:"ElementEnd",end:{type:"Empty"},range:d},t);break}else{t.debug("Found opening tag end"),n({type:"ElementEnd",end:{type:"Open"},range:d},t),a=!0;break}}else{if(!r)throw t.debug("Missing whitespace before attribute",{position:t.getPos(),currCodeUnit:String.fromCharCode(u)}),new c.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());t.debug("Parsing attribute");const[d,k,A]=p(t),X=t.getPos();t.debug("Attribute parsed",{prefix:d,local:k,value:A}),n({type:"Attribute",range:{start:l,end:X},prefix:d,local:k,value:A},t)}}if(a){if(t.config.rawTextElements!=null){const r=g.getQName(i,s);if(t.config.rawTextElements.includes(r)){t.debug("Processing raw text element content",{element:r}),R(t,n,`</${r}>`);return}}t.debug("Processing element content"),R(t,n)}t.debug("Finished element parsing",{prefix:s,local:i})}function p(t){const[n,e]=t.consumeQName();let s;const i=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(o.EQUALS)){t.skipSpaces();const a=t.currCodeUnit();if(a===o.SINGLE_QUOTE||a===o.DOUBLE_QUOTE){const r=t.consumeQuote();s=t.consumeCodeUnitsWhile(l=>l!==r&&l!==o.LESS_THAN),t.consumeCodeUnit(r)}else if(!t.config.strictDocument)s=t.consumeCodeUnitsWhile(r=>!I.isXmlSpaceByte(r)&&r!==o.GREATER_THAN&&r!==o.SLASH&&r!==o.LESS_THAN);else throw new c.XmlError({type:"InvalidChar",expected:"a quote",actual:a},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(i),s="true";else throw new c.XmlError({type:"InvalidChar",expected:o.EQUALS,actual:t.currCodeUnit()},t.genTextPos());return[n,e,s]}function R(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(f))S(t,n);else if(t.startsWith(G))tt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(E))C(t,n);else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===o.QUESTION_MARK)T(t,n);else if(s===o.SLASH){nt(t,n);break}else U(t,n)}else _(t,n,e)}function tt(t,n){const e=t.getPos();t.advance(9);const s=t.consumeCodeUnitsWhile((a,r)=>!(a===o.CLOSE_SQUARE_BRACKET&&r.startsWith(h)));t.skipString(h);const i=t.rangeFrom(e);n({type:"Cdata",text:s,range:i},t)}function nt(t,n){const e=t.getPos();t.advance(2);const[s,i]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(o.GREATER_THAN);const a=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:s,local:i},range:a},t)}function _(t,n,e=o.LESS_THAN){const s=t.getPos(),i=typeof e=="number"?t.consumeCodeUnitsWhile(a=>a!==e):t.consumeCodeUnitsWhile((a,r)=>!r.startsWith(e));if(t.config.strictDocument&&i.includes(h))throw new c.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:i,range:t.rangeFrom(s)},t)}exports.tokenize=K,exports.tokenizeXmlStream=v;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{AMPERSAND as
|
|
1
|
+
import{AMPERSAND as x,HASH as S,LOWERCASE_X as U,ZERO as w,NINE as y,UPPERCASE_A as P,UPPERCASE_F as v,LOWERCASE_A as E,LOWERCASE_F as b,SEMICOLON as k,COLON as N,SINGLE_QUOTE as T,DOUBLE_QUOTE as A,LINE_FEED as O}from"./ascii-constants.js";import{isXmlChar as m,isXmlSpaceByte as I,isAsciiDigit as W,isXmlNameStart as u,isXmlName as g}from"./utils.js";import{XmlError as n}from"./XmlError.js";var F=Object.defineProperty,f=Object.getOwnPropertySymbols,L=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable,d=(c,t,e)=>t in c?F(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,C=(c,t)=>{for(var e in t||(t={}))L.call(t,e)&&d(c,e,t[e]);if(f)for(var e of f(t))R.call(t,e)&&d(c,e,t[e]);return c},p=(c,t,e)=>d(c,typeof t!="symbol"?t+"":t,e);class _{constructor(t,e={}){p(this,"_text"),p(this,"_pos"),p(this,"_end"),p(this,"config");const{pos:i=0,strictDocument:o=!0,allowDtd:s=!0,rawTextElements:a=null,implicitSelfClosingElements:r=null,contextSliceSize:h=!1,debugLogger:l}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:o,allowDtd:s,rawTextElements:a,implicitSelfClosingElements:r,contextSliceSize:h,debugLogger:l},this.debug("XmlStream initialized",{textLength:this._text.length,initialPos:i,platform:process.platform,nodeVersion:process.version,textSample:this._text.slice(0,Math.min(100,this._text.length))+(this._text.length>100?"...":"")})}debug(t,...e){this.config.debugLogger&&this.config.debugLogger(t,...e)}clone(){return new _(this._text,C({pos:this._pos},this.config))}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)}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}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(!m(this.currCodeUnitUnchecked()))throw new n({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),o=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==o)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new n({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&&I(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(x))return null;let t;if(this.tryConsumeCodeUnit(S)){let e,i;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(s=>s>=w&&s<=y||s>=P&&s<=v||s>=E&&s<=b),i=16):(e=this.consumeCodeUnitsWhile(s=>W(s)),i=10);const o=parseInt(e,i);isNaN(o)||!m(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(k)?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(!u(e))throw new n({type:"InvalidName"},this.genTextPosFrom(t))}else if(!g(e))break;this._pos+=1}}consumeQName(){var t,e,i,o;const s=this._pos;let a=null;for(this.debug("Starting consumeQName",{position:this._pos,nextChars:this._text.slice(this._pos,this._pos+10)});this._pos<this._end;){const l=this.currCodeUnitUnchecked();if(l===N)if(a==null)a=this._pos,this._pos+=1,this.debug("Found colon splitter",{position:this._pos});else throw this.debug("Multiple colons found - error",{position:this._pos}),new n({type:"InvalidName"},this.genTextPosFrom(s));else if(g(l))this._pos+=1;else break}let r,h;if(a!=null?(r=this._text.slice(s,a),h=this.sliceBack(a+1)):(r="",h=this.sliceBack(s)),this.debug("Finished consumeQName",{prefix:r,local:h,startPos:s,endPos:this._pos}),r.length>0&&!u((t=r[0])==null?void 0:t.codePointAt(0)))throw this.debug("Invalid prefix start character",{prefix:r,firstChar:r[0],codePoint:(e=r[0])==null?void 0:e.codePointAt(0)}),new n({type:"InvalidName"},this.genTextPosFrom(s));if(h.length>0){if(!u((i=h[0])==null?void 0:i.codePointAt(0)))throw this.debug("Invalid local name start character",{local:h,firstChar:h[0],codePoint:(o=h[0])==null?void 0:o.codePointAt(0)}),new n({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debug("Empty local name - error"),new n({type:"InvalidName"},this.genTextPosFrom(s));return[r,h]}consumeQuote(){const t=this.currCodeUnit();if(t===T||t===A)return this._pos+=1,t;throw new n({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),o=Math.max(0,t-i),s=Math.min(this._end,t+i+e%2);return this._text.slice(o,s)}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)===O?(i++,o=1):o++;return C({row:i,col:o},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}getTextSnippet(t,e){return this._text.slice(t,t+e)}}export{_ as XmlStream};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import{getQName as
|
|
2
|
-
`).slice(0,10):void 0}),o}}function d(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(h))E(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(g))k(t,n);else if(t.startsWith(A))S(t,n);else break}function at(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(N)&&!e.atEnd())throw new c({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(st))throw new c({type:"InvalidString",expected:"version"},t.genTextPos());m(t),n(t),t.startsWith(it)&&(m(t),n(t)),t.startsWith(rt)&&m(t),t.skipSpaces(),t.skipString(N)}function E(t,n){const e=t.getPos();t.advance(4);const o=t.consumeCodeUnitsWhile((i,r)=>!(i===B&&r.startsWith(D)));if(t.skipString(D),o.includes("--")||o.endsWith("-"))throw new c({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:o,range:t.rangeFrom(e)},t)}function S(t,n){if(t.startsWith(v))throw new c({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const i=t.consumeCodeUnitsWhile((r,s)=>!(r===W&&s.startsWith(I)));t.skipString(I),n({type:"ProcessingInstruction",target:o,content:i.length===0?void 0:i,range:t.rangeFrom(e)},t)}function k(t,n){if(!t.config.allowDtd)throw new c({type:"DtdDetected"});const e=t.getPos();if(pt(t),t.skipSpaces(),t.currCodeUnit()===a){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith($))ut(t,n);else if(t.startsWith(h))E(t,n);else if(t.startsWith(A))S(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===a){t.advance(1);break}else throw new c({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(j)||t.startsWith(Z)||t.startsWith(tt))try{dt(t)}catch(o){throw new c({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}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(),b(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==M&&n!==a)throw new c({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function b(t){if(t.startsWith(ot)||t.startsWith(L)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const o=t.consumeQuote();if(t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o),e===L){t.consumeSpaces();const i=t.consumeQuote();t.consumeCodeUnitsWhile(r=>r!==i),t.consumeCodeUnit(i)}return!0}return!1}function ut(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(H);e||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const i=lt(t,e);i!==null&&n({type:"EntityDeclaration",name:o,definition:i},t),t.skipSpaces(),t.consumeCodeUnit(a)}function lt(t,n){const e=t.currCodeUnit();if(e===P||e===x){const o=t.consumeQuote(),i=t.getPos();t.skipCodeUnitsWhile(s=>s!==o);const r=t.sliceBack(i);return t.consumeCodeUnit(o),r}else if(e===K||e===Y){if(b(t))return n&&(t.skipSpaces(),t.startsWith(et)&&(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:e},t.genTextPos())}function dt(t){t.skipCodeUnitsWhile(n=>n!==a),t.consumeCodeUnit(a)}function y(t,n){const e=t.getPos();if(t.config.debug){const s=t.currCodeUnit();t.debugLog("parseElement started",JSON.stringify({position:e,character:String.fromCharCode(s),charCode:s,charHex:"0x"+s.toString(16),context:t.getTextAround(e,30),textPos:t.genTextPosFrom(e),platform:typeof process!="undefined"?process.arch:"unknown"},null,2))}t.advance(1);let o,i;try{[o,i]=t.consumeQName(),t.debugLog("parseElement consumeQName succeeded",JSON.stringify({prefix:o,local:i,position:t.getPos()},null,2)),n({type:"ElementStart",prefix:o,local:i,start:e},t)}catch(s){throw t.debugLog("parseElement consumeQName FAILED",JSON.stringify({error:s instanceof Error?s.message:String(s),errorType:s instanceof c?s.variant.type:"unknown",position:t.getPos(),startPosition:e,context:t.getTextAround(t.getPos(),50),platform:typeof process!="undefined"?process.arch:"unknown",stackTrace:s instanceof Error?s.stack:void 0},null,2)),s}let r=!1;for(;!t.atEnd();){const s=t.startsWithSpace();t.skipSpaces();const p=t.getPos(),T=t.currCodeUnit();if(T===f){t.advance(1),t.consumeCodeUnit(a);const u=t.rangeFrom(p);n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else if(T===a){t.advance(1);const u=t.rangeFrom(p);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(U(i,o))){n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else{n({type:"ElementEnd",end:{type:"Open"},range:u},t),r=!0;break}}else{if(!s)throw new c({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[u,_,R]=m(t),X=t.getPos();n({type:"Attribute",range:{start:p,end:X},prefix:u,local:_,value:R},t)}}if(r){if(t.config.rawTextElements!=null){const s=U(i,o);if(t.config.rawTextElements.includes(s)){O(t,n,`</${s}>`);return}}O(t,n)}}function m(t){const[n,e]=t.consumeQName();let o;const i=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(w)){t.skipSpaces();const r=t.currCodeUnit();if(r===x||r===P){const s=t.consumeQuote();o=t.consumeCodeUnitsWhile(p=>p!==s&&p!==l),t.consumeCodeUnit(s)}else if(!t.config.strictDocument)o=t.consumeCodeUnitsWhile(s=>!q(s)&&s!==a&&s!==f&&s!==l);else throw new c({type:"InvalidChar",expected:"a quote",actual:r},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(i),o="true";else throw new c({type:"InvalidChar",expected:w,actual:t.currCodeUnit()},t.genTextPos());return[n,e,o]}function O(t,n,e=l){for(;!t.atEnd();)if(typeof e=="number"?t.currCodeUnit()===e:t.startsWith(e)){const o=t.nextCodeUnit();if(o===z)if(t.startsWith(h))E(t,n);else if(t.startsWith(nt))mt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(g))k(t,n);else throw new c({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(o===W)S(t,n);else if(o===f){ft(t,n);break}else y(t,n)}else Q(t,n,e)}function mt(t,n){const e=t.getPos();t.advance(9);const o=t.consumeCodeUnitsWhile((r,s)=>!(r===J&&s.startsWith(C)));t.skipString(C);const i=t.rangeFrom(e);n({type:"Cdata",text:o,range:i},t)}function ft(t,n){const e=t.getPos();t.advance(2);const[o,i]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(a);const r=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:i},range:r},t)}function Q(t,n,e=l){const o=t.getPos(),i=typeof e=="number"?t.consumeCodeUnitsWhile(r=>r!==e):t.consumeCodeUnitsWhile((r,s)=>!s.startsWith(e));if(t.config.strictDocument&&i.includes(C))throw new c({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:i,range:t.rangeFrom(o)},t)}export{ct as tokenize,F as tokenizeXmlStream};
|
|
1
|
+
import{getQName as f}from"../get-q-name.js";import{LESS_THAN as l,GREATER_THAN as r,SLASH as h,EQUALS as W,SINGLE_QUOTE as y,DOUBLE_QUOTE as b,HYPHEN as B,QUESTION_MARK as v,OPEN_SQUARE_BRACKET as X,PERCENT as Y,EXCLAMATION_MARK as z,UPPERCASE_S as H,UPPERCASE_P as K,CLOSE_SQUARE_BRACKET as q}from"./ascii-constants.js";import{isXmlSpaceByte as G}from"./utils.js";import{XmlError as c}from"./XmlError.js";import{XmlStream as $}from"./XmlStream.js";const j="\uFEFF",I="<?xml ",A="?>",J="<!ENTITY",V="<!ELEMENT",Z="<!ATTLIST",tt="<!NOTATION",C="<!DOCTYPE",F="<?",N="?>",S="<!--",D="-->",nt="<![CDATA[",E="]]>",et="NDATA",L="PUBLIC",st="SYSTEM",ot="version",it="encoding",at="standalone";function ct(t,n,e={}){const s=new $(t,e);s.debug("Starting tokenization",{stringLength:t.length}),O(s,n)}function O(t,n){if(t.debug("Starting XML stream tokenization"),t.startsWith(j)&&(t.debug("Skipping BOM"),t.advance(1)),t.startsWith(I)&&(t.debug("Found XML declaration"),rt(t)),g(t,n),t.config.strictDocument){if(t.debug("Parsing in strict document mode"),t.skipSpaces(),t.startsWithIgnoreCase(C)&&(t.debug("Found DOCTYPE declaration"),P(t,n),g(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===l&&(t.debug("Starting root element parsing"),T(t,n)),g(t,n),!t.atEnd())throw t.debug("Unexpected content after root element",{position:t.getPos(),nextChars:t.getTextSnippet(t.getPos(),10)}),new c({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(t.debug("Parsing in non-strict mode");!t.atEnd();)t.currCodeUnit()===l?T(t,n):R(t,n),g(t,n);t.debug("Finished XML stream tokenization")}function g(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(S))U(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(C))P(t,n);else if(t.startsWith(F))k(t,n);else break}function rt(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(A)&&!e.atEnd())throw new c({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(ot))throw new c({type:"InvalidString",expected:"version"},t.genTextPos());m(t),n(t),t.startsWith(it)&&(m(t),n(t)),t.startsWith(at)&&m(t),t.skipSpaces(),t.skipString(A)}function U(t,n){const e=t.getPos();t.advance(4);const s=t.consumeCodeUnitsWhile((o,a)=>!(o===B&&a.startsWith(D)));if(t.skipString(D),s.includes("--")||s.endsWith("-"))throw new c({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:s,range:t.rangeFrom(e)},t)}function k(t,n){if(t.startsWith(I))throw new c({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const s=t.consumeName();t.skipSpaces();const o=t.consumeCodeUnitsWhile((a,i)=>!(a===v&&i.startsWith(N)));t.skipString(N),n({type:"ProcessingInstruction",target:s,content:o.length===0?void 0:o,range:t.rangeFrom(e)},t)}function P(t,n){if(!t.config.allowDtd)throw new c({type:"DtdDetected"});const e=t.getPos();if(dt(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(J))ut(t,n);else if(t.startsWith(S))U(t,n);else if(t.startsWith(F))k(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(V)||t.startsWith(Z)||t.startsWith(tt))try{pt(t)}catch(s){throw new c({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}else throw new c({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function dt(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),Q(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==X&&n!==r)throw new c({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function Q(t){if(t.startsWith(st)||t.startsWith(L)){const n=t.getPos();t.advance(6);const e=t.sliceBack(n);t.consumeSpaces();const s=t.consumeQuote();if(t.consumeCodeUnitsWhile(o=>o!==s),t.consumeCodeUnit(s),e===L){t.consumeSpaces();const o=t.consumeQuote();t.consumeCodeUnitsWhile(a=>a!==o),t.consumeCodeUnit(o)}return!0}return!1}function ut(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(Y);e||t.consumeSpaces();const s=t.consumeName();t.consumeSpaces();const o=lt(t,e);o!==null&&n({type:"EntityDeclaration",name:s,definition:o},t),t.skipSpaces(),t.consumeCodeUnit(r)}function lt(t,n){const e=t.currCodeUnit();if(e===b||e===y){const s=t.consumeQuote(),o=t.getPos();t.skipCodeUnitsWhile(i=>i!==s);const a=t.sliceBack(o);return t.consumeCodeUnit(s),a}else if(e===H||e===K){if(Q(t))return n&&(t.skipSpaces(),t.startsWith(et)&&(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:e},t.genTextPos())}function pt(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function T(t,n){const e=t.getPos();t.advance(1),t.debug("Starting element parsing",{position:e,nextChars:t.getTextSnippet(e,10)});const[s,o]=t.consumeQName();t.debug("Element name parsed",{prefix:s,local:o}),n({type:"ElementStart",prefix:s,local:o,start:e},t);let a=!1;for(;!t.atEnd();){const i=t.startsWithSpace();t.skipSpaces();const d=t.getPos(),p=t.currCodeUnit();if(t.debug("Processing element content",{position:d,currCodeUnit:p,hasSpace:i}),p===h){t.debug("Found self-closing tag"),t.advance(1),t.consumeCodeUnit(r);const u=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else if(p===r){t.advance(1);const u=t.rangeFrom(d);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(f(o,s))){t.debug("Found implicit self-closing element",{element:f(o,s)}),n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else{t.debug("Found opening tag end"),n({type:"ElementEnd",end:{type:"Open"},range:u},t),a=!0;break}}else{if(!i)throw t.debug("Missing whitespace before attribute",{position:t.getPos(),currCodeUnit:String.fromCharCode(p)}),new c({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());t.debug("Parsing attribute");const[u,w,x]=m(t),M=t.getPos();t.debug("Attribute parsed",{prefix:u,local:w,value:x}),n({type:"Attribute",range:{start:d,end:M},prefix:u,local:w,value:x},t)}}if(a){if(t.config.rawTextElements!=null){const i=f(o,s);if(t.config.rawTextElements.includes(i)){t.debug("Processing raw text element content",{element:i}),_(t,n,`</${i}>`);return}}t.debug("Processing element content"),_(t,n)}t.debug("Finished element parsing",{prefix:s,local:o})}function m(t){const[n,e]=t.consumeQName();let s;const o=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(W)){t.skipSpaces();const a=t.currCodeUnit();if(a===y||a===b){const i=t.consumeQuote();s=t.consumeCodeUnitsWhile(d=>d!==i&&d!==l),t.consumeCodeUnit(i)}else if(!t.config.strictDocument)s=t.consumeCodeUnitsWhile(i=>!G(i)&&i!==r&&i!==h&&i!==l);else throw new c({type:"InvalidChar",expected:"a quote",actual:a},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(o),s="true";else throw new c({type:"InvalidChar",expected:W,actual:t.currCodeUnit()},t.genTextPos());return[n,e,s]}function _(t,n,e=l){for(;!t.atEnd();)if(typeof e=="number"?t.currCodeUnit()===e:t.startsWith(e)){const s=t.nextCodeUnit();if(s===z)if(t.startsWith(S))U(t,n);else if(t.startsWith(nt))gt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(C))P(t,n);else throw new c({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===v)k(t,n);else if(s===h){mt(t,n);break}else T(t,n)}else R(t,n,e)}function gt(t,n){const e=t.getPos();t.advance(9);const s=t.consumeCodeUnitsWhile((a,i)=>!(a===q&&i.startsWith(E)));t.skipString(E);const o=t.rangeFrom(e);n({type:"Cdata",text:s,range:o},t)}function mt(t,n){const e=t.getPos();t.advance(2);const[s,o]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const a=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:s,local:o},range:a},t)}function R(t,n,e=l){const s=t.getPos(),o=typeof e=="number"?t.consumeCodeUnitsWhile(a=>a!==e):t.consumeCodeUnitsWhile((a,i)=>!i.startsWith(e));if(t.config.strictDocument&&o.includes(E))throw new c({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)},t)}export{ct as tokenize,O as tokenizeXmlStream};
|
|
@@ -12,19 +12,16 @@ export declare class XmlStream {
|
|
|
12
12
|
private _end;
|
|
13
13
|
readonly config: TXmlStreamConfig;
|
|
14
14
|
constructor(text: string, options?: TXmlStreamOptions);
|
|
15
|
+
/**
|
|
16
|
+
* Public method to log debug information if a logger is configured
|
|
17
|
+
*/
|
|
18
|
+
debug(message: unknown, ...args: unknown[]): void;
|
|
15
19
|
/**
|
|
16
20
|
* Creates a clone of the current stream.
|
|
17
21
|
*
|
|
18
22
|
* @returns A new instance of XmlStream with the same state.
|
|
19
23
|
*/
|
|
20
24
|
clone(): XmlStream;
|
|
21
|
-
/**
|
|
22
|
-
* Logs debug information when debug logging is enabled.
|
|
23
|
-
*
|
|
24
|
-
* @param message - The debug message.
|
|
25
|
-
* @param data - Additional debug data.
|
|
26
|
-
*/
|
|
27
|
-
debugLog(message: string, data?: any): void;
|
|
28
25
|
/**
|
|
29
26
|
* Gets the current position in the stream.
|
|
30
27
|
*
|
|
@@ -218,6 +215,10 @@ export declare class XmlStream {
|
|
|
218
215
|
* @returns The calculated text position.
|
|
219
216
|
*/
|
|
220
217
|
genTextPosFrom(pos: number): TTextPos;
|
|
218
|
+
/**
|
|
219
|
+
* Gets a snippet of text from the current position
|
|
220
|
+
*/
|
|
221
|
+
getTextSnippet(start: number, length: number): string;
|
|
221
222
|
}
|
|
222
223
|
export interface TXmlStreamConfig {
|
|
223
224
|
/**
|
|
@@ -261,11 +262,11 @@ export interface TXmlStreamConfig {
|
|
|
261
262
|
*/
|
|
262
263
|
contextSliceSize: number | false;
|
|
263
264
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* @default
|
|
265
|
+
* Optional debug logger function that will be called with debug information.
|
|
266
|
+
* This allows users to capture and handle debug output in their own way.
|
|
267
|
+
* @default undefined
|
|
267
268
|
*/
|
|
268
|
-
|
|
269
|
+
debugLogger?: (message: unknown, ...args: unknown[]) => void;
|
|
269
270
|
}
|
|
270
271
|
export type TXmlStreamOptions = {
|
|
271
272
|
pos?: number;
|