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