xml-tokenizer 0.0.39 → 0.0.41

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.
@@ -0,0 +1,8 @@
1
+ "use strict";var b=require("./config.js"),u=require("./xml-to-string.js"),C=Object.defineProperty,c=Object.getOwnPropertySymbols,m=Object.prototype.hasOwnProperty,d=Object.prototype.propertyIsEnumerable,f=(e,l,o)=>l in e?C(e,l,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[l]=o,$=(e,l)=>{for(var o in l||(l={}))m.call(l,o)&&f(e,o,l[o]);if(c)for(var o of c(l))d.call(l,o)&&f(e,o,l[o]);return e},X=(e,l)=>{var o={};for(var i in e)m.call(e,i)&&l.indexOf(i)<0&&(o[i]=e[i]);if(e!=null&&c)for(var i of c(e))l.indexOf(i)<0&&d.call(e,i)&&(o[i]=e[i]);return o};function v(e,l={}){const o=l,{xmlOptions:i=b.htmlConfig,transformers:h,skipNodes:p=y}=o,S=X(o,["xmlOptions","transformers","skipNodes"]);return u.xmlToString(e,$({xmlOptions:i,skipNodes:p,transformers:$({h1:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}# ${n}`},h2:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}## ${n}`},h3:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}### ${n}`},h4:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}#### ${n}`},h5:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}##### ${n}`},h6:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}###### ${n}`},p:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}${n}`},strong:r=>{const t=s(u.getXmlStringNodeContent(r));return t===""?"":`**${t}**`},b:r=>{const t=s(u.getXmlStringNodeContent(r));return t===""?"":`**${t}**`},em:r=>{const t=s(u.getXmlStringNodeContent(r));return t===""?"":`*${t}*`},i:r=>{const t=s(u.getXmlStringNodeContent(r));return t===""?"":`*${t}*`},a:r=>{var t;const n=(t=r.attributes.find(N=>N.local==="href"))==null?void 0:t.value,a=s(u.getXmlStringNodeContent(r));return a===""?"":n!=null?`[${a}](${n})`:a},ul:(r,t)=>{const n=u.getXmlStringNodeContent(r);return n===""?"":`${g(t)}${n}`},ol:(r,t)=>{const n=u.getXmlStringNodeContent(r);return n===""?"":`${g(t)}${n}`},li:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));if(n==="")return"";const a=O(t);return`${" ".repeat(Math.max(0,a-1))}- ${n}
2
+ `},blockquote:(r,t)=>{const n=s(u.getXmlStringNodeContent(r));return n===""?"":`${g(t)}> ${n}`},hr:(r,t)=>`${g(t)}---`,br:()=>`
3
+ `,code:r=>{const t=s(u.getXmlStringNodeContent(r));return t===""?"":`\`${t}\``},pre:(r,t)=>{const n=u.getXmlStringNodeContent(r);return n===""?"":`${g(t)}\`\`\`
4
+ ${n}
5
+ \`\`\``}},h)},S))}function s(e){return e.replace(/\s+/g," ").trim()}function g(e){if(e.length===0)return"";const l=e[e.length-1];if(l==null||l.content.length===0)return"";for(let o=l.content.length-1;o>=0;o--){const i=l.content[o];if(typeof i=="object"&&i.string!=null&&i.string!==""){if(k.has(i.local))return`
6
+
7
+ `;if(x.has(i.local))return`
8
+ `}}return""}function O(e){let l=0;for(let o=e.length-1;o>=0;o--){const i=e[o];i&&(i.local==="ul"||i.local==="ol")&&l++}return l}const k=new Set(["h1","h2","h3","h4","h5","h6","p","div","blockquote","hr","pre","ul","ol","li"]),x=new Set(["strong","b","em","i","a","code","br"]),y=["script","style","meta","link","noscript","iframe","object","embed","param","base","title","head"];exports.htmlToMarkdown=v;
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var E=require("./config.js"),a=require("./get-q-name.js"),A=require("./processor/process.js"),T=require("./processor/processors/path-tracker.js"),R=require("./processor/validate-processor-order.js"),l=require("./selector/select.js"),m=require("./selector/TokenSelector.js"),t=require("./selector/TokenSelectState.js"),O=require("./selector/TokenSelectStateMachine.js"),C=require("./token-to-xml.js"),e=require("./tokenizer/ascii-constants.js"),i=require("./tokenizer/tokenize.js"),r=require("./tokenizer/utils.js"),s=require("./tokenizer/XmlError.js"),c=require("./tokenizer/XmlStream.js"),N=require("./tokens-to-xml.js"),S=require("./xml-to-object.js"),o=require("./xml-to-simplified-object.js");exports.htmlConfig=E.htmlConfig,exports.svgConfig=E.svgConfig,exports.xmlConfig=E.xmlConfig,exports.getQName=a.getQName,exports.process=A.process,exports.pathTracker=T.pathTracker,exports.validateProcessorOrder=R.validateProcessorOrder,exports.select=l.select,exports.TokenSelector=m.TokenSelector,exports.EToCacheNodeProps=t.EToCacheNodeProps,exports.ETokenMatchCriteria=t.ETokenMatchCriteria,exports.TokenSelectState=t.TokenSelectState,exports.TokenSelectStateMachine=O.TokenSelectStateMachine,exports.tokenToXml=C.tokenToXml,exports.AMPERSAND=e.AMPERSAND,exports.CARRIAGE_RETURN=e.CARRIAGE_RETURN,exports.CLOSE_SQUARE_BRACKET=e.CLOSE_SQUARE_BRACKET,exports.COLON=e.COLON,exports.DOUBLE_QUOTE=e.DOUBLE_QUOTE,exports.EQUALS=e.EQUALS,exports.EXCLAMATION_MARK=e.EXCLAMATION_MARK,exports.GREATER_THAN=e.GREATER_THAN,exports.HASH=e.HASH,exports.HORIZONTAL_TAB=e.HORIZONTAL_TAB,exports.HYPHEN=e.HYPHEN,exports.LESS_THAN=e.LESS_THAN,exports.LINE_FEED=e.LINE_FEED,exports.LOWERCASE_A=e.LOWERCASE_A,exports.LOWERCASE_F=e.LOWERCASE_F,exports.LOWERCASE_X=e.LOWERCASE_X,exports.LOWERCASE_Z=e.LOWERCASE_Z,exports.NINE=e.NINE,exports.OPEN_SQUARE_BRACKET=e.OPEN_SQUARE_BRACKET,exports.PERCENT=e.PERCENT,exports.PERIOD=e.PERIOD,exports.QUESTION_MARK=e.QUESTION_MARK,exports.SEMICOLON=e.SEMICOLON,exports.SINGLE_QUOTE=e.SINGLE_QUOTE,exports.SLASH=e.SLASH,exports.SPACE=e.SPACE,exports.UNDERSCORE=e.UNDERSCORE,exports.UPPERCASE_A=e.UPPERCASE_A,exports.UPPERCASE_F=e.UPPERCASE_F,exports.UPPERCASE_P=e.UPPERCASE_P,exports.UPPERCASE_S=e.UPPERCASE_S,exports.UPPERCASE_Z=e.UPPERCASE_Z,exports.ZERO=e.ZERO,exports.tokenize=i.tokenize,exports.tokenizeXmlStream=i.tokenizeXmlStream,exports.isAsciiDigit=r.isAsciiDigit,exports.isXmlChar=r.isXmlChar,exports.isXmlName=r.isXmlName,exports.isXmlNameByte=r.isXmlNameByte,exports.isXmlNameStart=r.isXmlNameStart,exports.isXmlSpaceByte=r.isXmlSpaceByte,exports.XmlError=s.XmlError,exports.XmlStream=c.XmlStream,exports.tokensToXml=N.tokensToXml,exports.processTokenForObject=S.processTokenForObject,exports.xmlToObject=S.xmlToObject,exports.processTokenForSimplifiedObject=o.processTokenForSimplifiedObject,exports.xmlToSimplifiedObject=o.xmlToSimplifiedObject;
1
+ "use strict";var E=require("./config.js"),a=require("./get-q-name.js"),A=require("./html-to-md.js"),l=require("./processor/process.js"),T=require("./processor/processors/path-tracker.js"),m=require("./processor/validate-processor-order.js"),R=require("./selector/select.js"),C=require("./selector/TokenSelector.js"),t=require("./selector/TokenSelectState.js"),O=require("./selector/TokenSelectStateMachine.js"),n=require("./token-to-xml.js"),e=require("./tokenizer/ascii-constants.js"),i=require("./tokenizer/tokenize.js"),r=require("./tokenizer/utils.js"),N=require("./tokenizer/XmlError.js"),s=require("./tokenizer/XmlStream.js"),c=require("./tokens-to-xml.js"),_=require("./xml-to-object.js"),S=require("./xml-to-simplified-object.js"),o=require("./xml-to-string.js");exports.htmlConfig=E.htmlConfig,exports.svgConfig=E.svgConfig,exports.xmlConfig=E.xmlConfig,exports.getQName=a.getQName,exports.htmlToMarkdown=A.htmlToMarkdown,exports.process=l.process,exports.pathTracker=T.pathTracker,exports.validateProcessorOrder=m.validateProcessorOrder,exports.select=R.select,exports.TokenSelector=C.TokenSelector,exports.EToCacheNodeProps=t.EToCacheNodeProps,exports.ETokenMatchCriteria=t.ETokenMatchCriteria,exports.TokenSelectState=t.TokenSelectState,exports.TokenSelectStateMachine=O.TokenSelectStateMachine,exports.tokenToXml=n.tokenToXml,exports.AMPERSAND=e.AMPERSAND,exports.CARRIAGE_RETURN=e.CARRIAGE_RETURN,exports.CLOSE_SQUARE_BRACKET=e.CLOSE_SQUARE_BRACKET,exports.COLON=e.COLON,exports.DOUBLE_QUOTE=e.DOUBLE_QUOTE,exports.EQUALS=e.EQUALS,exports.EXCLAMATION_MARK=e.EXCLAMATION_MARK,exports.GREATER_THAN=e.GREATER_THAN,exports.HASH=e.HASH,exports.HORIZONTAL_TAB=e.HORIZONTAL_TAB,exports.HYPHEN=e.HYPHEN,exports.LESS_THAN=e.LESS_THAN,exports.LINE_FEED=e.LINE_FEED,exports.LOWERCASE_A=e.LOWERCASE_A,exports.LOWERCASE_F=e.LOWERCASE_F,exports.LOWERCASE_X=e.LOWERCASE_X,exports.LOWERCASE_Z=e.LOWERCASE_Z,exports.NINE=e.NINE,exports.OPEN_SQUARE_BRACKET=e.OPEN_SQUARE_BRACKET,exports.PERCENT=e.PERCENT,exports.PERIOD=e.PERIOD,exports.QUESTION_MARK=e.QUESTION_MARK,exports.SEMICOLON=e.SEMICOLON,exports.SINGLE_QUOTE=e.SINGLE_QUOTE,exports.SLASH=e.SLASH,exports.SPACE=e.SPACE,exports.UNDERSCORE=e.UNDERSCORE,exports.UPPERCASE_A=e.UPPERCASE_A,exports.UPPERCASE_F=e.UPPERCASE_F,exports.UPPERCASE_P=e.UPPERCASE_P,exports.UPPERCASE_S=e.UPPERCASE_S,exports.UPPERCASE_Z=e.UPPERCASE_Z,exports.ZERO=e.ZERO,exports.tokenize=i.tokenize,exports.tokenizeXmlStream=i.tokenizeXmlStream,exports.isAsciiDigit=r.isAsciiDigit,exports.isXmlChar=r.isXmlChar,exports.isXmlName=r.isXmlName,exports.isXmlNameByte=r.isXmlNameByte,exports.isXmlNameStart=r.isXmlNameStart,exports.isXmlSpaceByte=r.isXmlSpaceByte,exports.XmlError=N.XmlError,exports.XmlStream=s.XmlStream,exports.tokensToXml=c.tokensToXml,exports.xmlToObject=_.xmlToObject,exports.processTokenForSimplifiedObject=S.processTokenForSimplifiedObject,exports.xmlToSimplifiedObject=S.xmlToSimplifiedObject,exports.getXmlStringNodeContent=o.getXmlStringNodeContent,exports.xmlToString=o.xmlToString;
@@ -1 +1 @@
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:s=0,strictDocument:r=!0,allowDtd:i=!0,rawTextElements:p=null,implicitSelfClosingElements:h=null,contextSliceSize:l=!1,debugLogger:u}=e;this._text=t,this._pos=s,this._end=this._text.length,this.config={strictDocument:r,allowDtd:i,rawTextElements:p,implicitSelfClosingElements:h,contextSliceSize:l,debugLogger:u},this.debug("XmlStream constructor",{textLength:t.length,initialPos:s,platform:process.platform,nodeVersion:process.version,textSample:t.slice(0,Math.min(100,t.length))+(t.length>100?"...":"")}),this.debug("XmlStream initialized",{textLength:this._text.length,initialPos:s,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 s=this._text.charCodeAt(this._pos+e),r=t.charCodeAt(e);if((s>=97&&s<=122?s-32:s)!==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(),s=e.consumeReference();return s!=null?(this._pos+=e.getPos()-t,s):null}consumeReference(){if(!this.tryConsumeCodeUnit(o.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(o.HASH)){let e,s;this.tryConsumeCodeUnit(o.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(i=>i>=o.ZERO&&i<=o.NINE||i>=o.UPPERCASE_A&&i<=o.UPPERCASE_F||i>=o.LOWERCASE_A&&i<=o.LOWERCASE_F),s=16):(e=this.consumeCodeUnitsWhile(i=>c.isAsciiDigit(i)),s=10);const r=parseInt(e,s);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,s,r;const i=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(i));else if(c.isXmlName(u))this._pos+=1;else break}let h,l;if(p!=null?(h=this._text.slice(i,p),l=this.sliceBack(p+1)):(h="",l=this.sliceBack(i)),this.debug("Finished consumeQName",{prefix:h,local:l,startPos:i,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(i));if(l.length>0){if(!c.isXmlNameStart((s=l[0])==null?void 0:s.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(i))}else throw this.debug("Empty local name - error"),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(i));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 s=Math.floor(e/2),r=Math.max(0,t-s),i=Math.min(this._end,t+s+e%2);return this._text.slice(r,i)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let s=1,r=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===o.LINE_FEED?(s++,r=1):r++;return f({row:s,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
+ "use strict";var n=require("./ascii-constants.js"),a=require("./utils.js"),o=require("./XmlError.js"),f=Object.defineProperty,d=Object.getOwnPropertySymbols,E=Object.prototype.hasOwnProperty,g=Object.prototype.propertyIsEnumerable,p=(h,t,e)=>t in h?f(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,_=(h,t)=>{for(var e in t||(t={}))E.call(t,e)&&p(h,e,t[e]);if(d)for(var e of d(t))g.call(t,e)&&p(h,e,t[e]);return h},u=(h,t,e)=>p(h,typeof t!="symbol"?t+"":t,e);class m{constructor(t,e={}){u(this,"_text"),u(this,"_pos"),u(this,"_end"),u(this,"config");const{pos:s=0,strictDocument:r=!0,allowDtd:i=!0,rawTextElements:l=null,implicitSelfClosingElements:c=null,contextSliceSize:C=!1}=e;this._text=t,this._pos=s,this._end=this._text.length,this.config={strictDocument:r,allowDtd:i,rawTextElements:l,implicitSelfClosingElements:c,contextSliceSize:C}}clone(){return new m(this._text,_({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o.XmlError({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!a.isXmlChar(this.currCodeUnitUnchecked()))throw new o.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}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 s=this._text.charCodeAt(this._pos+e),r=t.charCodeAt(e);if((s>=97&&s<=122?s-32:s)!==r)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new o.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&a.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o.XmlError({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),s=e.consumeReference();return s!=null?(this._pos+=e.getPos()-t,s):null}consumeReference(){if(!this.tryConsumeCodeUnit(n.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(n.HASH)){let e,s;this.tryConsumeCodeUnit(n.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(i=>i>=n.ZERO&&i<=n.NINE||i>=n.UPPERCASE_A&&i<=n.UPPERCASE_F||i>=n.LOWERCASE_A&&i<=n.LOWERCASE_F),s=16):(e=this.consumeCodeUnitsWhile(i=>a.isAsciiDigit(i)),s=10);const r=parseInt(e,s);isNaN(r)||!a.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(n.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!a.isXmlNameStart(e))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!a.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e;const s=this._pos;let r=null;for(;this._pos<this._end;){const c=this.currCodeUnitUnchecked();if(c===n.COLON)if(r==null)r=this._pos,this._pos+=1;else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(s));else if(a.isXmlName(c))this._pos+=1;else break}let i,l;if(r!=null?(i=this._text.slice(s,r),l=this.sliceBack(r+1)):(i="",l=this.sliceBack(s)),i.length>0&&!a.isXmlNameStart((t=i[0])==null?void 0:t.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(s));if(l.length>0){if(!a.isXmlNameStart((e=l[0])==null?void 0:e.codePointAt(0)))throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}else throw new o.XmlError({type:"InvalidName"},this.genTextPosFrom(s));return[i,l]}consumeQuote(){const t=this.currCodeUnit();if(t===n.SINGLE_QUOTE||t===n.DOUBLE_QUOTE)return this._pos+=1,t;throw new o.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const s=Math.floor(e/2),r=Math.max(0,t-s),i=Math.min(this._end,t+s+e%2);return this._text.slice(r,i)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let s=1,r=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===n.LINE_FEED?(s++,r=1):r++;return _({row:s,col:r},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}exports.XmlStream=m;
@@ -1 +1 @@
1
- "use strict";var p=require("../get-q-name.js"),o=require("./ascii-constants.js"),_=require("./utils.js"),c=require("./XmlError.js"),I=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",z="version",q="encoding",Y="standalone";function K(t,n,e={}){var s,i;(s=e.debugLogger)==null||s.call(e,"Creating XmlStream",{stringLength:t.length});const r=new I.XmlStream(t,e);r.debug("Created XmlStream, starting tokenization",{stringLength:t.length}),(i=e.debugLogger)==null||i.call(e,"Created XmlStream, starting tokenization (2)",{stringLength:t.length}),v(r,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)),g(t,n),t.config.strictDocument){if(t.debug("Parsing in strict document mode"),t.skipSpaces(),t.startsWithIgnoreCase(E)&&(t.debug("Found DOCTYPE declaration"),T(t,n),g(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===o.LESS_THAN&&(t.debug("Starting root element parsing"),U(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.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):X(t,n),g(t,n);t.debug("Finished XML stream tokenization")}function g(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(f))S(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(E))T(t,n);else if(t.startsWith(x))C(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(z))throw new c.XmlError({type:"InvalidString",expected:"version"},t.genTextPos());m(t),n(t),t.startsWith(q)&&(m(t),n(t)),t.startsWith(Y)&&m(t),t.skipSpaces(),t.skipString(w)}function S(t,n){const e=t.getPos();t.advance(4);const s=t.consumeCodeUnitsWhile((i,r)=>!(i===o.HYPHEN&&r.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 C(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((r,a)=>!(r===o.QUESTION_MARK&&a.startsWith(W)));t.skipString(W),n({type:"ProcessingInstruction",target:s,content:i.length===0?void 0:i,range:t.rangeFrom(e)},t)}function T(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))C(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(r=>r!==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(a=>a!==s);const r=t.sliceBack(i);return t.consumeCodeUnit(s),r}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 r=!1;for(;!t.atEnd();){const a=t.startsWithSpace();t.skipSpaces();const l=t.getPos(),d=t.currCodeUnit();if(t.debug("Processing element content",{position:l,currCodeUnit:d,hasSpace:a}),d===o.SLASH){t.debug("Found self-closing tag"),t.advance(1),t.consumeCodeUnit(o.GREATER_THAN);const u=t.rangeFrom(l);n({type:"ElementEnd",end:{type:"Empty"},range:u},t);break}else if(d===o.GREATER_THAN){t.advance(1);const u=t.rangeFrom(l);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(p.getQName(i,s))){t.debug("Found implicit self-closing element",{element:p.getQName(i,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),r=!0;break}}else{if(!a)throw t.debug("Missing whitespace before attribute",{position:t.getPos(),currCodeUnit:String.fromCharCode(d)}),new c.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());t.debug("Parsing attribute");const[u,k,A]=m(t),L=t.getPos();t.debug("Attribute parsed",{prefix:u,local:k,value:A}),n({type:"Attribute",range:{start:l,end:L},prefix:u,local:k,value:A},t)}}if(r){if(t.config.rawTextElements!=null){const a=p.getQName(i,s);if(t.config.rawTextElements.includes(a)){t.debug("Processing raw text element content",{element:a}),R(t,n,`</${a}>`);return}}t.debug("Processing element content"),R(t,n)}t.debug("Finished element parsing",{prefix:s,local:i})}function m(t){const[n,e]=t.consumeQName();let s;const i=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(o.EQUALS)){t.skipSpaces();const r=t.currCodeUnit();if(r===o.SINGLE_QUOTE||r===o.DOUBLE_QUOTE){const a=t.consumeQuote();s=t.consumeCodeUnitsWhile(l=>l!==a&&l!==o.LESS_THAN),t.consumeCodeUnit(a)}else if(!t.config.strictDocument)s=t.consumeCodeUnitsWhile(a=>!_.isXmlSpaceByte(a)&&a!==o.GREATER_THAN&&a!==o.SLASH&&a!==o.LESS_THAN);else throw new c.XmlError({type:"InvalidChar",expected:"a quote",actual:r},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))T(t,n);else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(s===o.QUESTION_MARK)C(t,n);else if(s===o.SLASH){nt(t,n);break}else U(t,n)}else X(t,n,e)}function tt(t,n){const e=t.getPos();t.advance(9);const s=t.consumeCodeUnitsWhile((r,a)=>!(r===o.CLOSE_SQUARE_BRACKET&&a.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 r=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:s,local:i},range:r},t)}function X(t,n,e=o.LESS_THAN){const s=t.getPos(),i=typeof e=="number"?t.consumeCodeUnitsWhile(r=>r!==e):t.consumeCodeUnitsWhile((r,a)=>!a.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
+ "use strict";var U=require("../get-q-name.js"),s=require("./ascii-constants.js"),L=require("./utils.js"),c=require("./XmlError.js"),H=require("./XmlStream.js");const Q="\uFEFF",A="<?xml ",k="?>",D="<!ENTITY",F="<!ELEMENT",O="<!ATTLIST",G="<!NOTATION",p="<!DOCTYPE",w="<?",W="?>",E="<!--",y="-->",b="<![CDATA[",f="]]>",B="NDATA",N="PUBLIC",M="SYSTEM",q="version",K="encoding",Y="standalone";function z(t,n,e={}){P(new H.XmlStream(t,e),n)}function P(t,n){if(t.startsWith(Q)&&t.advance(1),t.startsWith(A)&&$(t),m(t,n),t.config.strictDocument){if(t.skipSpaces(),t.startsWithIgnoreCase(p)&&(S(t,n),m(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===s.LESS_THAN&&T(t,n),m(t,n),!t.atEnd())throw new c.XmlError({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(;!t.atEnd();)t.currCodeUnit()===s.LESS_THAN?T(t,n):R(t,n),m(t,n)}function m(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(E))g(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(p))S(t,n);else if(t.startsWith(w))h(t,n);else break}function $(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(k)&&!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());d(t),n(t),t.startsWith(K)&&(d(t),n(t)),t.startsWith(Y)&&d(t),t.skipSpaces(),t.skipString(k)}function g(t,n){const e=t.getPos();t.advance(4);const o=t.consumeCodeUnitsWhile((i,r)=>!(i===s.HYPHEN&&r.startsWith(y)));if(t.skipString(y),o.includes("--")||o.endsWith("-"))throw new c.XmlError({type:"InvalidComment"},t.genTextPosFrom(e));n({type:"Comment",text:o,range:t.rangeFrom(e)},t)}function h(t,n){if(t.startsWith(A))throw new c.XmlError({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const o=t.consumeName();t.skipSpaces();const i=t.consumeCodeUnitsWhile((r,a)=>!(r===s.QUESTION_MARK&&a.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 c.XmlError({type:"DtdDetected"});const e=t.getPos();if(j(t),t.skipSpaces(),t.currCodeUnit()===s.GREATER_THAN){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(D))J(t,n);else if(t.startsWith(E))g(t,n);else if(t.startsWith(w))h(t,n);else if(t.startsWith("]"))if(t.advance(1),t.skipSpaces(),t.currCodeUnit()===s.GREATER_THAN){t.advance(1);break}else throw new c.XmlError({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(F)||t.startsWith(O)||t.startsWith(G))try{Z(t)}catch(o){throw new c.XmlError({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function j(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),x(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==s.OPEN_SQUARE_BRACKET&&n!==s.GREATER_THAN)throw new c.XmlError({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function x(t){if(t.startsWith(M)||t.startsWith(N)){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===N){t.consumeSpaces();const i=t.consumeQuote();t.consumeCodeUnitsWhile(r=>r!==i),t.consumeCodeUnit(i)}return!0}return!1}function J(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(s.PERCENT);e||t.consumeSpaces();const o=t.consumeName();t.consumeSpaces();const i=V(t,e);i!==null&&n({type:"EntityDeclaration",name:o,definition:i},t),t.skipSpaces(),t.consumeCodeUnit(s.GREATER_THAN)}function V(t,n){const e=t.currCodeUnit();if(e===s.DOUBLE_QUOTE||e===s.SINGLE_QUOTE){const o=t.consumeQuote(),i=t.getPos();t.skipCodeUnitsWhile(a=>a!==o);const r=t.sliceBack(i);return t.consumeCodeUnit(o),r}else if(e===s.UPPERCASE_S||e===s.UPPERCASE_P){if(x(t))return n&&(t.skipSpaces(),t.startsWith(B)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new c.XmlError({type:"InvalidExternalID"},t.genTextPos())}else throw new c.XmlError({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:e},t.genTextPos())}function Z(t){t.skipCodeUnitsWhile(n=>n!==s.GREATER_THAN),t.consumeCodeUnit(s.GREATER_THAN)}function T(t,n){const e=t.getPos();t.advance(1);const[o,i]=t.consumeQName();n({type:"ElementStart",prefix:o,local:i,start:e},t);let r=!1;for(;!t.atEnd();){const a=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(U.getQName(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(!a)throw new c.XmlError({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[u,_,I]=d(t),X=t.getPos();n({type:"Attribute",range:{start:l,end:X},prefix:u,local:_,value:I},t)}}if(r){if(t.config.rawTextElements!=null){const a=U.getQName(i,o);if(t.config.rawTextElements.includes(a)){v(t,n,`</${a}>`);return}}v(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 r=t.currCodeUnit();if(r===s.SINGLE_QUOTE||r===s.DOUBLE_QUOTE){const a=t.consumeQuote();o=t.consumeCodeUnitsWhile(l=>l!==a&&l!==s.LESS_THAN),t.consumeCodeUnit(a)}else if(!t.config.strictDocument)o=t.consumeCodeUnitsWhile(a=>!L.isXmlSpaceByte(a)&&a!==s.GREATER_THAN&&a!==s.SLASH&&a!==s.LESS_THAN);else throw new c.XmlError({type:"InvalidChar",expected:"a quote",actual:r},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(i),o="true";else throw new c.XmlError({type:"InvalidChar",expected:s.EQUALS,actual:t.currCodeUnit()},t.genTextPos());return[n,e,o]}function v(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(E))g(t,n);else if(t.startsWith(b))tt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(p))S(t,n);else throw new c.XmlError({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(o===s.QUESTION_MARK)h(t,n);else if(o===s.SLASH){nt(t,n);break}else T(t,n)}else R(t,n,e)}function tt(t,n){const e=t.getPos();t.advance(9);const o=t.consumeCodeUnitsWhile((r,a)=>!(r===s.CLOSE_SQUARE_BRACKET&&a.startsWith(f)));t.skipString(f);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 r=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:o,local:i},range:r},t)}function R(t,n,e=s.LESS_THAN){const o=t.getPos(),i=typeof e=="number"?t.consumeCodeUnitsWhile(r=>r!==e):t.consumeCodeUnitsWhile((r,a)=>!a.startsWith(e));if(t.config.strictDocument&&i.includes(f))throw new c.XmlError({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:i,range:t.rangeFrom(o)},t)}exports.tokenize=z,exports.tokenizeXmlStream=P;
@@ -1 +1 @@
1
- "use strict";var r=require("./tokenizer/tokenize.js");function s(t,o={}){const e={local:"root",attributes:[],content:[]},n=[e];return r.tokenize(t,l=>{c(l,n)},o),e.content[0]}function c(t,o){switch(t.type){case"ElementStart":{const e={local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,attributes:[],content:[]},n=o[o.length-1];n!=null&&n.content.push(e),o.push(e);break}case"ElementEnd":{(t.end.type==="Close"||t.end.type==="Empty")&&o.pop();break}case"Attribute":{const e=o[o.length-1];e!=null&&e.attributes.push({local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,value:t.value});break}case"Text":case"Cdata":{const e=o[o.length-1];e!=null&&t.text.trim().length>0&&e.content.push(t.text);break}}}exports.processTokenForObject=c,exports.xmlToObject=s;
1
+ "use strict";var c=require("./tokenizer/tokenize.js");function r(t,l={}){const e={local:"root",attributes:[],content:[]},n=[e];return c.tokenize(t,o=>{a(o,n)},l),e.content[0]}function a(t,l){switch(t.type){case"ElementStart":{const e={local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,attributes:[],content:[]},n=l[l.length-1];n!=null&&n.content.push(e),l.push(e);break}case"ElementEnd":{(t.end.type==="Close"||t.end.type==="Empty")&&l.pop();break}case"Attribute":{const e=l[l.length-1];e!=null&&e.attributes.push({local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,value:t.value});break}case"Text":case"Cdata":{const e=l[l.length-1];e!=null&&t.text.trim().length>0&&e.content.push(t.text);break}}}exports.xmlToObject=r;
@@ -0,0 +1 @@
1
+ "use strict";var i=require("./get-q-name.js"),k=require("./tokenizer/tokenize.js"),g=Object.defineProperty,c=Object.getOwnPropertySymbols,m=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable,p=(n,e,t)=>e in n?g(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,d=(n,e)=>{for(var t in e||(e={}))m.call(e,t)&&p(n,t,e[t]);if(c)for(var t of c(e))b.call(e,t)&&p(n,t,e[t]);return n};function v(n,e={}){const{transformers:t={},xmlOptions:s={},skipNodes:r=[],preserveTree:a=!1}=e,o={local:"root",attributes:[],content:[]},u={stack:[o],transformers:d({__default:l},t),skipNodes:new Set(r),preserveTree:a};return k.tokenize(n,f=>{x(f,u)},s),o.string=l(o),o}function x(n,e){switch(n.type){case"ElementStart":{const t=i.getQName(n.local,n.prefix.length>0?n.prefix:void 0).toLowerCase(),s=e.skipNodes.has(t),r={local:n.local,prefix:n.prefix.length>0?n.prefix:void 0,attributes:[],content:[],skip:s},a=e.stack[e.stack.length-1];a!=null&&!a.skip&&a.content.push(r),e.stack.push(r);break}case"ElementEnd":{if(n.end.type==="Close"||n.end.type==="Empty"){const t=e.stack.pop();if(t==null||t.skip)return;const s=i.getQName(t.local,t.prefix).toLowerCase(),r=e.transformers[s];r!=null?t.string=r(t,e.stack):e.transformers.__default!=null&&(t.string=e.transformers.__default(t,e.stack)),e.preserveTree||(t.content=[])}break}case"Attribute":{const t=e.stack[e.stack.length-1];if(t==null||t.skip)return;t.attributes.push({local:n.local,prefix:n.prefix.length>0?n.prefix:void 0,value:n.value});break}case"Text":case"Cdata":{const t=e.stack[e.stack.length-1];if(t==null||t.skip)return;n.text.trim().length>0&&t.content.push(n.text);break}}}function l(n){return n.content.map(e=>typeof e=="string"?e:e.string).join("")}exports.getXmlStringNodeContent=l,exports.xmlToString=v;
@@ -0,0 +1,8 @@
1
+ import{htmlConfig as O}from"./config.js";import{xmlToString as x,getXmlStringNodeContent as u}from"./xml-to-string.js";var y=Object.defineProperty,f=Object.getOwnPropertySymbols,$=Object.prototype.hasOwnProperty,p=Object.prototype.propertyIsEnumerable,h=(e,l,o)=>l in e?y(e,l,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[l]=o,m=(e,l)=>{for(var o in l||(l={}))$.call(l,o)&&h(e,o,l[o]);if(f)for(var o of f(l))p.call(l,o)&&h(e,o,l[o]);return e},k=(e,l)=>{var o={};for(var s in e)$.call(e,s)&&l.indexOf(s)<0&&(o[s]=e[s]);if(e!=null&&f)for(var s of f(e))l.indexOf(s)<0&&p.call(e,s)&&(o[s]=e[s]);return o};function j(e,l={}){const o=l,{xmlOptions:s=O,transformers:b,skipNodes:g=P}=o,d=k(o,["xmlOptions","transformers","skipNodes"]);return x(e,m({xmlOptions:s,skipNodes:g,transformers:m({h1:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}# ${r}`},h2:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}## ${r}`},h3:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}### ${r}`},h4:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}#### ${r}`},h5:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}##### ${r}`},h6:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}###### ${r}`},p:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}${r}`},strong:n=>{const t=i(u(n));return t===""?"":`**${t}**`},b:n=>{const t=i(u(n));return t===""?"":`**${t}**`},em:n=>{const t=i(u(n));return t===""?"":`*${t}*`},i:n=>{const t=i(u(n));return t===""?"":`*${t}*`},a:n=>{var t;const r=(t=n.attributes.find(v=>v.local==="href"))==null?void 0:t.value,c=i(u(n));return c===""?"":r!=null?`[${c}](${r})`:c},ul:(n,t)=>{const r=u(n);return r===""?"":`${a(t)}${r}`},ol:(n,t)=>{const r=u(n);return r===""?"":`${a(t)}${r}`},li:(n,t)=>{const r=i(u(n));if(r==="")return"";const c=w(t);return`${" ".repeat(Math.max(0,c-1))}- ${r}
2
+ `},blockquote:(n,t)=>{const r=i(u(n));return r===""?"":`${a(t)}> ${r}`},hr:(n,t)=>`${a(t)}---`,br:()=>`
3
+ `,code:n=>{const t=i(u(n));return t===""?"":`\`${t}\``},pre:(n,t)=>{const r=u(n);return r===""?"":`${a(t)}\`\`\`
4
+ ${r}
5
+ \`\`\``}},b)},d))}function i(e){return e.replace(/\s+/g," ").trim()}function a(e){if(e.length===0)return"";const l=e[e.length-1];if(l==null||l.content.length===0)return"";for(let o=l.content.length-1;o>=0;o--){const s=l.content[o];if(typeof s=="object"&&s.string!=null&&s.string!==""){if(S.has(s.local))return`
6
+
7
+ `;if(N.has(s.local))return`
8
+ `}}return""}function w(e){let l=0;for(let o=e.length-1;o>=0;o--){const s=e[o];s&&(s.local==="ul"||s.local==="ol")&&l++}return l}const S=new Set(["h1","h2","h3","h4","h5","h6","p","div","blockquote","hr","pre","ul","ol","li"]),N=new Set(["strong","b","em","i","a","code","br"]),P=["script","style","meta","link","noscript","iframe","object","embed","param","base","title","head"];export{j as htmlToMarkdown};
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{htmlConfig as r,svgConfig as E,xmlConfig as t}from"./config.js";import{getQName as S}from"./get-q-name.js";import{process as R}from"./processor/process.js";import{pathTracker as i}from"./processor/processors/path-tracker.js";import{validateProcessorOrder as O}from"./processor/validate-processor-order.js";import{select as f}from"./selector/select.js";import{TokenSelector as _}from"./selector/TokenSelector.js";import{EToCacheNodeProps as x,ETokenMatchCriteria as a,TokenSelectState as s}from"./selector/TokenSelectState.js";import{TokenSelectStateMachine as c}from"./selector/TokenSelectStateMachine.js";import{tokenToXml as n}from"./token-to-xml.js";import{AMPERSAND as X,CARRIAGE_RETURN as k,CLOSE_SQUARE_BRACKET as I,COLON as H,DOUBLE_QUOTE as M,EQUALS as Q,EXCLAMATION_MARK as g,GREATER_THAN as h,HASH as B,HORIZONTAL_TAB as D,HYPHEN as d,LESS_THAN as F,LINE_FEED as b,LOWERCASE_A as j,LOWERCASE_F as K,LOWERCASE_X as W,LOWERCASE_Z as Z,NINE as G,OPEN_SQUARE_BRACKET as v,PERCENT as y,PERIOD as z,QUESTION_MARK as Y,SEMICOLON as q,SINGLE_QUOTE as u,SLASH as w,SPACE as J,UNDERSCORE as V,UPPERCASE_A as $,UPPERCASE_F as ee,UPPERCASE_P as oe,UPPERCASE_S as re,UPPERCASE_Z as Ee,ZERO as te}from"./tokenizer/ascii-constants.js";import{tokenize as Se,tokenizeXmlStream as Ae}from"./tokenizer/tokenize.js";import{isAsciiDigit as pe,isXmlChar as ie,isXmlName as Ce,isXmlNameByte as Oe,isXmlNameStart as Te,isXmlSpaceByte as fe}from"./tokenizer/utils.js";import{XmlError as _e}from"./tokenizer/XmlError.js";import{XmlStream as xe}from"./tokenizer/XmlStream.js";import{tokensToXml as se}from"./tokens-to-xml.js";import{processTokenForObject as ce,xmlToObject as Le}from"./xml-to-object.js";import{processTokenForSimplifiedObject as Ue,xmlToSimplifiedObject as Xe}from"./xml-to-simplified-object.js";export{X as AMPERSAND,k as CARRIAGE_RETURN,I as CLOSE_SQUARE_BRACKET,H as COLON,M as DOUBLE_QUOTE,Q as EQUALS,x as EToCacheNodeProps,a as ETokenMatchCriteria,g as EXCLAMATION_MARK,h as GREATER_THAN,B as HASH,D as HORIZONTAL_TAB,d as HYPHEN,F as LESS_THAN,b as LINE_FEED,j as LOWERCASE_A,K as LOWERCASE_F,W as LOWERCASE_X,Z as LOWERCASE_Z,G as NINE,v as OPEN_SQUARE_BRACKET,y as PERCENT,z as PERIOD,Y as QUESTION_MARK,q as SEMICOLON,u as SINGLE_QUOTE,w as SLASH,J as SPACE,s as TokenSelectState,c as TokenSelectStateMachine,_ as TokenSelector,V as UNDERSCORE,$ as UPPERCASE_A,ee as UPPERCASE_F,oe as UPPERCASE_P,re as UPPERCASE_S,Ee as UPPERCASE_Z,_e as XmlError,xe as XmlStream,te as ZERO,S as getQName,r as htmlConfig,pe as isAsciiDigit,ie as isXmlChar,Ce as isXmlName,Oe as isXmlNameByte,Te as isXmlNameStart,fe as isXmlSpaceByte,i as pathTracker,R as process,ce as processTokenForObject,Ue as processTokenForSimplifiedObject,f as select,E as svgConfig,n as tokenToXml,Se as tokenize,Ae as tokenizeXmlStream,se as tokensToXml,O as validateProcessorOrder,t as xmlConfig,Le as xmlToObject,Xe as xmlToSimplifiedObject};
1
+ import{htmlConfig as r,svgConfig as t,xmlConfig as E}from"./config.js";import{getQName as S}from"./get-q-name.js";import{htmlToMarkdown as R}from"./html-to-md.js";import{process as p}from"./processor/process.js";import{pathTracker as T}from"./processor/processors/path-tracker.js";import{validateProcessorOrder as l}from"./processor/validate-processor-order.js";import{select as O}from"./selector/select.js";import{TokenSelector as _}from"./selector/TokenSelector.js";import{EToCacheNodeProps as P,ETokenMatchCriteria as n,TokenSelectState as s}from"./selector/TokenSelectState.js";import{TokenSelectStateMachine as L}from"./selector/TokenSelectStateMachine.js";import{tokenToXml as X}from"./token-to-xml.js";import{AMPERSAND as g,CARRIAGE_RETURN as I,CLOSE_SQUARE_BRACKET as H,COLON as M,DOUBLE_QUOTE as d,EQUALS as h,EXCLAMATION_MARK as Q,GREATER_THAN as B,HASH as D,HORIZONTAL_TAB as F,HYPHEN as K,LESS_THAN as W,LINE_FEED as Z,LOWERCASE_A as b,LOWERCASE_F as j,LOWERCASE_X as G,LOWERCASE_Z as v,NINE as y,OPEN_SQUARE_BRACKET as z,PERCENT as w,PERIOD as Y,QUESTION_MARK as q,SEMICOLON as u,SINGLE_QUOTE as J,SLASH as V,SPACE as $,UNDERSCORE as oo,UPPERCASE_A as eo,UPPERCASE_F as ro,UPPERCASE_P as to,UPPERCASE_S as Eo,UPPERCASE_Z as mo,ZERO as So}from"./tokenizer/ascii-constants.js";import{tokenize as Ro,tokenizeXmlStream as io}from"./tokenizer/tokenize.js";import{isAsciiDigit as Co,isXmlChar as To,isXmlName as fo,isXmlNameByte as lo,isXmlNameStart as xo,isXmlSpaceByte as Oo}from"./tokenizer/utils.js";import{XmlError as _o}from"./tokenizer/XmlError.js";import{XmlStream as Po}from"./tokenizer/XmlStream.js";import{tokensToXml as so}from"./tokens-to-xml.js";import{xmlToObject as Lo}from"./xml-to-object.js";import{processTokenForSimplifiedObject as Xo,xmlToSimplifiedObject as ko}from"./xml-to-simplified-object.js";import{getXmlStringNodeContent as Io,xmlToString as Ho}from"./xml-to-string.js";export{g as AMPERSAND,I as CARRIAGE_RETURN,H as CLOSE_SQUARE_BRACKET,M as COLON,d as DOUBLE_QUOTE,h as EQUALS,P as EToCacheNodeProps,n as ETokenMatchCriteria,Q as EXCLAMATION_MARK,B as GREATER_THAN,D as HASH,F as HORIZONTAL_TAB,K as HYPHEN,W as LESS_THAN,Z as LINE_FEED,b as LOWERCASE_A,j as LOWERCASE_F,G as LOWERCASE_X,v as LOWERCASE_Z,y as NINE,z as OPEN_SQUARE_BRACKET,w as PERCENT,Y as PERIOD,q as QUESTION_MARK,u as SEMICOLON,J as SINGLE_QUOTE,V as SLASH,$ as SPACE,s as TokenSelectState,L as TokenSelectStateMachine,_ as TokenSelector,oo as UNDERSCORE,eo as UPPERCASE_A,ro as UPPERCASE_F,to as UPPERCASE_P,Eo as UPPERCASE_S,mo as UPPERCASE_Z,_o as XmlError,Po as XmlStream,So as ZERO,S as getQName,Io as getXmlStringNodeContent,r as htmlConfig,R as htmlToMarkdown,Co as isAsciiDigit,To as isXmlChar,fo as isXmlName,lo as isXmlNameByte,xo as isXmlNameStart,Oo as isXmlSpaceByte,T as pathTracker,p as process,Xo as processTokenForSimplifiedObject,O as select,t as svgConfig,X as tokenToXml,Ro as tokenize,io as tokenizeXmlStream,so as tokensToXml,l as validateProcessorOrder,E as xmlConfig,Lo as xmlToObject,ko as xmlToSimplifiedObject,Ho as xmlToString};
@@ -1 +1 @@
1
- import{AMPERSAND as C,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 _,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,x=(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 m{constructor(t,e={}){p(this,"_text"),p(this,"_pos"),p(this,"_end"),p(this,"config");const{pos:s=0,strictDocument:o=!0,allowDtd:i=!0,rawTextElements:a=null,implicitSelfClosingElements:r=null,contextSliceSize:h=!1,debugLogger:l}=e;this._text=t,this._pos=s,this._end=this._text.length,this.config={strictDocument:o,allowDtd:i,rawTextElements:a,implicitSelfClosingElements:r,contextSliceSize:h,debugLogger:l},this.debug("XmlStream constructor",{textLength:t.length,initialPos:s,platform:process.platform,nodeVersion:process.version,textSample:t.slice(0,Math.min(100,t.length))+(t.length>100?"...":"")}),this.debug("XmlStream initialized",{textLength:this._text.length,initialPos:s,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 m(this._text,x({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(!_(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 s=this._text.charCodeAt(this._pos+e),o=t.charCodeAt(e);if((s>=97&&s<=122?s-32:s)!==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(),s=e.consumeReference();return s!=null?(this._pos+=e.getPos()-t,s):null}consumeReference(){if(!this.tryConsumeCodeUnit(C))return null;let t;if(this.tryConsumeCodeUnit(S)){let e,s;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(i=>i>=w&&i<=y||i>=P&&i<=v||i>=E&&i<=b),s=16):(e=this.consumeCodeUnitsWhile(i=>W(i)),s=10);const o=parseInt(e,s);isNaN(o)||!_(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,s,o;const i=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(i));else if(g(l))this._pos+=1;else break}let r,h;if(a!=null?(r=this._text.slice(i,a),h=this.sliceBack(a+1)):(r="",h=this.sliceBack(i)),this.debug("Finished consumeQName",{prefix:r,local:h,startPos:i,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(i));if(h.length>0){if(!u((s=h[0])==null?void 0:s.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(i))}else throw this.debug("Empty local name - error"),new n({type:"InvalidName"},this.genTextPosFrom(i));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 s=Math.floor(e/2),o=Math.max(0,t-s),i=Math.min(this._end,t+s+e%2);return this._text.slice(o,i)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let s=1,o=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===O?(s++,o=1):o++;return x({row:s,col:o},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}getTextSnippet(t,e){return this._text.slice(t,t+e)}}export{m as XmlStream};
1
+ import{AMPERSAND as x,HASH as g,LOWERCASE_X as U,ZERO as w,NINE as y,UPPERCASE_A as S,UPPERCASE_F as E,LOWERCASE_A as v,LOWERCASE_F as P,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 d,isXmlSpaceByte as b,isAsciiDigit as I,isXmlNameStart as l,isXmlName as _}from"./utils.js";import{XmlError as o}from"./XmlError.js";var W=Object.defineProperty,m=Object.getOwnPropertySymbols,F=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable,p=(r,t,e)=>t in r?W(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,f=(r,t)=>{for(var e in t||(t={}))F.call(t,e)&&p(r,e,t[e]);if(m)for(var e of m(t))R.call(t,e)&&p(r,e,t[e]);return r},a=(r,t,e)=>p(r,typeof t!="symbol"?t+"":t,e);class u{constructor(t,e={}){a(this,"_text"),a(this,"_pos"),a(this,"_end"),a(this,"config");const{pos:s=0,strictDocument:n=!0,allowDtd:i=!0,rawTextElements:h=null,implicitSelfClosingElements:c=null,contextSliceSize:C=!1}=e;this._text=t,this._pos=s,this._end=this._text.length,this.config={strictDocument:n,allowDtd:i,rawTextElements:h,implicitSelfClosingElements:c,contextSliceSize:C}}clone(){return new u(this._text,f({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!d(this.currCodeUnitUnchecked()))throw new o({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}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 s=this._text.charCodeAt(this._pos+e),n=t.charCodeAt(e);if((s>=97&&s<=122?s-32:s)!==n)return!1}return!0}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(),s=e.consumeReference();return s!=null?(this._pos+=e.getPos()-t,s):null}consumeReference(){if(!this.tryConsumeCodeUnit(x))return null;let t;if(this.tryConsumeCodeUnit(g)){let e,s;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(i=>i>=w&&i<=y||i>=S&&i<=E||i>=v&&i<=P),s=16):(e=this.consumeCodeUnitsWhile(i=>I(i)),s=10);const n=parseInt(e,s);isNaN(n)||!d(n)?t=null:t={type:"Char",value:String.fromCodePoint(n)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(k)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!l(e))throw new o({type:"InvalidName"},this.genTextPosFrom(t))}else if(!_(e))break;this._pos+=1}}consumeQName(){var t,e;const s=this._pos;let n=null;for(;this._pos<this._end;){const c=this.currCodeUnitUnchecked();if(c===N)if(n==null)n=this._pos,this._pos+=1;else throw new o({type:"InvalidName"},this.genTextPosFrom(s));else if(_(c))this._pos+=1;else break}let i,h;if(n!=null?(i=this._text.slice(s,n),h=this.sliceBack(n+1)):(i="",h=this.sliceBack(s)),i.length>0&&!l((t=i[0])==null?void 0:t.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(s));if(h.length>0){if(!l((e=h[0])==null?void 0:e.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(s))}else throw new o({type:"InvalidName"},this.genTextPosFrom(s));return[i,h]}consumeQuote(){const t=this.currCodeUnit();if(t===T||t===A)return this._pos+=1,t;throw new o({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const s=Math.floor(e/2),n=Math.max(0,t-s),i=Math.min(this._end,t+s+e%2);return this._text.slice(n,i)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let s=1,n=1;for(let i=0;i<e;i++)this._text.charCodeAt(i)===O?(s++,n=1):n++;return f({row:s,col:n},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}export{u as XmlStream};
@@ -1 +1 @@
1
- import{getQName as f}from"../get-q-name.js";import{LESS_THAN as u,GREATER_THAN as c,SLASH as h,EQUALS as W,SINGLE_QUOTE as y,DOUBLE_QUOTE as b,HYPHEN as M,QUESTION_MARK as v,OPEN_SQUARE_BRACKET as B,PERCENT as z,EXCLAMATION_MARK as Y,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 r}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="<!--",L="-->",nt="<![CDATA[",E="]]>",et="NDATA",D="PUBLIC",st="SYSTEM",ot="version",it="encoding",at="standalone";function rt(t,n,e={}){var s,o;(s=e.debugLogger)==null||s.call(e,"Creating XmlStream",{stringLength:t.length});const i=new $(t,e);i.debug("Created XmlStream, starting tokenization",{stringLength:t.length}),(o=e.debugLogger)==null||o.call(e,"Created XmlStream, starting tokenization (2)",{stringLength:t.length}),O(i,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"),ct(t)),p(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),p(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===u&&(t.debug("Starting root element parsing"),T(t,n)),p(t,n),!t.atEnd())throw t.debug("Unexpected content after root element",{position:t.getPos(),nextChars:t.getTextSnippet(t.getPos(),10)}),new r({type:"UnknownToken",message:"Not at end"},t.genTextPos())}else for(t.debug("Parsing in non-strict mode");!t.atEnd();)t.currCodeUnit()===u?T(t,n):R(t,n),p(t,n);t.debug("Finished XML stream tokenization")}function p(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 ct(t){function n(e){if(e.startsWithSpace())e.skipSpaces();else if(!e.startsWith(A)&&!e.atEnd())throw new r({type:"InvalidChar",expected:"a whitespace",actual:e.currCodeUnitUnchecked()},e.genTextPos())}if(t.advance(5),n(t),!t.startsWith(ot))throw new r({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,i)=>!(o===M&&i.startsWith(L)));if(t.skipString(L),s.includes("--")||s.endsWith("-"))throw new r({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 r({type:"UnexpectedDeclaration"},t.genTextPos());const e=t.getPos();t.advance(2);const s=t.consumeName();t.skipSpaces();const o=t.consumeCodeUnitsWhile((i,a)=>!(i===v&&a.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 r({type:"DtdDetected"});const e=t.getPos();if(dt(t),t.skipSpaces(),t.currCodeUnit()===c){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(J))lt(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()===c){t.advance(1);break}else throw new r({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(V)||t.startsWith(Z)||t.startsWith(tt))try{gt(t)}catch(s){throw new r({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(e))}else throw new r({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!==B&&n!==c)throw new r({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function Q(t){if(t.startsWith(st)||t.startsWith(D)){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===D){t.consumeSpaces();const o=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o)}return!0}return!1}function lt(t,n){t.advance(8),t.consumeSpaces();const e=!t.tryConsumeCodeUnit(z);e||t.consumeSpaces();const s=t.consumeName();t.consumeSpaces();const o=ut(t,e);o!==null&&n({type:"EntityDeclaration",name:s,definition:o},t),t.skipSpaces(),t.consumeCodeUnit(c)}function ut(t,n){const e=t.currCodeUnit();if(e===b||e===y){const s=t.consumeQuote(),o=t.getPos();t.skipCodeUnitsWhile(a=>a!==s);const i=t.sliceBack(o);return t.consumeCodeUnit(s),i}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 r({type:"InvalidExternalID"},t.genTextPos())}else throw new r({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:e},t.genTextPos())}function gt(t){t.skipCodeUnitsWhile(n=>n!==c),t.consumeCodeUnit(c)}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 i=!1;for(;!t.atEnd();){const a=t.startsWithSpace();t.skipSpaces();const d=t.getPos(),g=t.currCodeUnit();if(t.debug("Processing element content",{position:d,currCodeUnit:g,hasSpace:a}),g===h){t.debug("Found self-closing tag"),t.advance(1),t.consumeCodeUnit(c);const l=t.rangeFrom(d);n({type:"ElementEnd",end:{type:"Empty"},range:l},t);break}else if(g===c){t.advance(1);const l=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:l},t);break}else{t.debug("Found opening tag end"),n({type:"ElementEnd",end:{type:"Open"},range:l},t),i=!0;break}}else{if(!a)throw t.debug("Missing whitespace before attribute",{position:t.getPos(),currCodeUnit:String.fromCharCode(g)}),new r({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());t.debug("Parsing attribute");const[l,w,x]=m(t),X=t.getPos();t.debug("Attribute parsed",{prefix:l,local:w,value:x}),n({type:"Attribute",range:{start:d,end:X},prefix:l,local:w,value:x},t)}}if(i){if(t.config.rawTextElements!=null){const a=f(o,s);if(t.config.rawTextElements.includes(a)){t.debug("Processing raw text element content",{element:a}),_(t,n,`</${a}>`);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 i=t.currCodeUnit();if(i===y||i===b){const a=t.consumeQuote();s=t.consumeCodeUnitsWhile(d=>d!==a&&d!==u),t.consumeCodeUnit(a)}else if(!t.config.strictDocument)s=t.consumeCodeUnitsWhile(a=>!G(a)&&a!==c&&a!==h&&a!==u);else throw new r({type:"InvalidChar",expected:"a quote",actual:i},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(o),s="true";else throw new r({type:"InvalidChar",expected:W,actual:t.currCodeUnit()},t.genTextPos());return[n,e,s]}function _(t,n,e=u){for(;!t.atEnd();)if(typeof e=="number"?t.currCodeUnit()===e:t.startsWith(e)){const s=t.nextCodeUnit();if(s===Y)if(t.startsWith(S))U(t,n);else if(t.startsWith(nt))pt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(C))P(t,n);else throw new r({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 pt(t,n){const e=t.getPos();t.advance(9);const s=t.consumeCodeUnitsWhile((i,a)=>!(i===q&&a.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(c);const i=t.rangeFrom(e);n({type:"ElementEnd",end:{type:"Close",prefix:s,local:o},range:i},t)}function R(t,n,e=u){const s=t.getPos(),o=typeof e=="number"?t.consumeCodeUnitsWhile(i=>i!==e):t.consumeCodeUnitsWhile((i,a)=>!a.startsWith(e));if(t.config.strictDocument&&o.includes(E))throw new r({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(s)},t)}export{rt as tokenize,O as tokenizeXmlStream};
1
+ import{getQName as w}from"../get-q-name.js";import{LESS_THAN as d,GREATER_THAN as r,SLASH as f,EQUALS as W,SINGLE_QUOTE as y,DOUBLE_QUOTE as P,HYPHEN as M,QUESTION_MARK as x,OPEN_SQUARE_BRACKET as X,PERCENT as H,EXCLAMATION_MARK as Y,UPPERCASE_S as K,UPPERCASE_P as q,CLOSE_SQUARE_BRACKET as z}from"./ascii-constants.js";import{isXmlSpaceByte as G}from"./utils.js";import{XmlError as a}from"./XmlError.js";import{XmlStream as $}from"./XmlStream.js";const j="\uFEFF",v="<?xml ",I="?>",J="<!ENTITY",V="<!ELEMENT",Z="<!ATTLIST",tt="<!NOTATION",g="<!DOCTYPE",A="<?",N="?>",h="<!--",D="-->",nt="<![CDATA[",C="]]>",st="NDATA",F="PUBLIC",et="SYSTEM",ot="version",it="encoding",ct="standalone";function at(t,n,s={}){Q(new $(t,s),n)}function Q(t,n){if(t.startsWith(j)&&t.advance(1),t.startsWith(v)&&rt(t),u(t,n),t.config.strictDocument){if(t.skipSpaces(),t.startsWithIgnoreCase(g)&&(U(t,n),u(t,n)),t.skipSpaces(),!t.atEnd()&&t.currCodeUnit()===d&&k(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?k(t,n):O(t,n),u(t,n)}function u(t,n){for(;!t.atEnd();)if(t.skipSpaces(),t.startsWith(h))E(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(g))U(t,n);else if(t.startsWith(A))S(t,n);else break}function rt(t){function n(s){if(s.startsWithSpace())s.skipSpaces();else if(!s.startsWith(I)&&!s.atEnd())throw new a({type:"InvalidChar",expected:"a whitespace",actual:s.currCodeUnitUnchecked()},s.genTextPos())}if(t.advance(5),n(t),!t.startsWith(ot))throw new a({type:"InvalidString",expected:"version"},t.genTextPos());m(t),n(t),t.startsWith(it)&&(m(t),n(t)),t.startsWith(ct)&&m(t),t.skipSpaces(),t.skipString(I)}function E(t,n){const s=t.getPos();t.advance(4);const e=t.consumeCodeUnitsWhile((o,i)=>!(o===M&&i.startsWith(D)));if(t.skipString(D),e.includes("--")||e.endsWith("-"))throw new a({type:"InvalidComment"},t.genTextPosFrom(s));n({type:"Comment",text:e,range:t.rangeFrom(s)},t)}function S(t,n){if(t.startsWith(v))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===x&&c.startsWith(N)));t.skipString(N),n({type:"ProcessingInstruction",target:e,content:o.length===0?void 0:o,range:t.rangeFrom(s)},t)}function U(t,n){if(!t.config.allowDtd)throw new a({type:"DtdDetected"});const s=t.getPos();if(pt(t),t.skipSpaces(),t.currCodeUnit()===r){t.advance(1);return}for(t.advance(1);!t.atEnd();)if(t.skipSpaces(),t.startsWith(J))lt(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()===r){t.advance(1);break}else throw new a({type:"InvalidChar",expected:"'>'",actual:t.currCodeUnitUnchecked()},t.genTextPos());else if(t.startsWith(V)||t.startsWith(Z)||t.startsWith(tt))try{ut(t)}catch(e){throw new a({type:"UnknownToken",message:"Failed to consume declaration"},t.genTextPosFrom(s))}else throw new a({type:"UnknownToken",message:"Failed to parse doctype"},t.genTextPos())}function pt(t){t.advance(9),t.consumeSpaces(),t.skipName(),t.skipSpaces(),_(t),t.skipSpaces();const n=t.currCodeUnit();if(n!==X&&n!==r)throw new a({type:"InvalidChar",expected:"'[' or '>'",actual:n},t.genTextPos())}function _(t){if(t.startsWith(et)||t.startsWith(F)){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===F){t.consumeSpaces();const o=t.consumeQuote();t.consumeCodeUnitsWhile(i=>i!==o),t.consumeCodeUnit(o)}return!0}return!1}function lt(t,n){t.advance(8),t.consumeSpaces();const s=!t.tryConsumeCodeUnit(H);s||t.consumeSpaces();const e=t.consumeName();t.consumeSpaces();const o=dt(t,s);o!==null&&n({type:"EntityDeclaration",name:e,definition:o},t),t.skipSpaces(),t.consumeCodeUnit(r)}function dt(t,n){const s=t.currCodeUnit();if(s===P||s===y){const e=t.consumeQuote(),o=t.getPos();t.skipCodeUnitsWhile(c=>c!==e);const i=t.sliceBack(o);return t.consumeCodeUnit(e),i}else if(s===K||s===q){if(_(t))return n&&(t.skipSpaces(),t.startsWith(st)&&(t.advance(5),t.consumeSpaces(),t.skipName())),null;throw new a({type:"InvalidExternalID"},t.genTextPos())}else throw new a({type:"InvalidChar",expected:"a quote, SYSTEM or PUBLIC",actual:s},t.genTextPos())}function ut(t){t.skipCodeUnitsWhile(n=>n!==r),t.consumeCodeUnit(r)}function k(t,n){const s=t.getPos();t.advance(1);const[e,o]=t.consumeQName();n({type:"ElementStart",prefix:e,local:o,start:s},t);let i=!1;for(;!t.atEnd();){const c=t.startsWithSpace();t.skipSpaces();const p=t.getPos(),T=t.currCodeUnit();if(T===f){t.advance(1),t.consumeCodeUnit(r);const l=t.rangeFrom(p);n({type:"ElementEnd",end:{type:"Empty"},range:l},t);break}else if(T===r){t.advance(1);const l=t.rangeFrom(p);if(t.config.implicitSelfClosingElements!=null&&t.config.implicitSelfClosingElements.includes(w(o,e))){n({type:"ElementEnd",end:{type:"Empty"},range:l},t);break}else{n({type:"ElementEnd",end:{type:"Open"},range:l},t),i=!0;break}}else{if(!c)throw new a({type:"InvalidChar",expected:"a whitespace",actual:t.currCodeUnitUnchecked()},t.genTextPos());const[l,R,b]=m(t),B=t.getPos();n({type:"Attribute",range:{start:p,end:B},prefix:l,local:R,value:b},t)}}if(i){if(t.config.rawTextElements!=null){const c=w(o,e);if(t.config.rawTextElements.includes(c)){L(t,n,`</${c}>`);return}}L(t,n)}}function m(t){const[n,s]=t.consumeQName();let e;const o=t.getPos();if(t.skipSpaces(),t.tryConsumeCodeUnit(W)){t.skipSpaces();const i=t.currCodeUnit();if(i===y||i===P){const c=t.consumeQuote();e=t.consumeCodeUnitsWhile(p=>p!==c&&p!==d),t.consumeCodeUnit(c)}else if(!t.config.strictDocument)e=t.consumeCodeUnitsWhile(c=>!G(c)&&c!==r&&c!==f&&c!==d);else throw new a({type:"InvalidChar",expected:"a quote",actual:i},t.genTextPos())}else if(!t.config.strictDocument)t.goTo(o),e="true";else throw new a({type:"InvalidChar",expected:W,actual:t.currCodeUnit()},t.genTextPos());return[n,s,e]}function L(t,n,s=d){for(;!t.atEnd();)if(typeof s=="number"?t.currCodeUnit()===s:t.startsWith(s)){const e=t.nextCodeUnit();if(e===Y)if(t.startsWith(h))E(t,n);else if(t.startsWith(nt))mt(t,n);else if(!t.config.strictDocument&&t.startsWithIgnoreCase(g))U(t,n);else throw new a({type:"UnknownToken",message:"Failed to parse content"},t.genTextPos());else if(e===x)S(t,n);else if(e===f){ft(t,n);break}else k(t,n)}else O(t,n,s)}function mt(t,n){const s=t.getPos();t.advance(9);const e=t.consumeCodeUnitsWhile((i,c)=>!(i===z&&c.startsWith(C)));t.skipString(C);const o=t.rangeFrom(s);n({type:"Cdata",text:e,range:o},t)}function ft(t,n){const s=t.getPos();t.advance(2);const[e,o]=t.consumeQName();t.skipSpaces(),t.consumeCodeUnit(r);const i=t.rangeFrom(s);n({type:"ElementEnd",end:{type:"Close",prefix:e,local:o},range:i},t)}function O(t,n,s=d){const e=t.getPos(),o=typeof s=="number"?t.consumeCodeUnitsWhile(i=>i!==s):t.consumeCodeUnitsWhile((i,c)=>!c.startsWith(s));if(t.config.strictDocument&&o.includes(C))throw new a({type:"InvalidCharacterData"},t.genTextPos());n({type:"Text",text:o,range:t.rangeFrom(e)},t)}export{at as tokenize,Q as tokenizeXmlStream};
@@ -1 +1 @@
1
- import{tokenize as c}from"./tokenizer/tokenize.js";function a(t,o={}){const e={local:"root",attributes:[],content:[]},l=[e];return c(t,r=>{n(r,l)},o),e.content[0]}function n(t,o){switch(t.type){case"ElementStart":{const e={local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,attributes:[],content:[]},l=o[o.length-1];l!=null&&l.content.push(e),o.push(e);break}case"ElementEnd":{(t.end.type==="Close"||t.end.type==="Empty")&&o.pop();break}case"Attribute":{const e=o[o.length-1];e!=null&&e.attributes.push({local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,value:t.value});break}case"Text":case"Cdata":{const e=o[o.length-1];e!=null&&t.text.trim().length>0&&e.content.push(t.text);break}}}export{n as processTokenForObject,a as xmlToObject};
1
+ import{tokenize as r}from"./tokenizer/tokenize.js";function c(t,l={}){const e={local:"root",attributes:[],content:[]},n=[e];return r(t,o=>{a(o,n)},l),e.content[0]}function a(t,l){switch(t.type){case"ElementStart":{const e={local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,attributes:[],content:[]},n=l[l.length-1];n!=null&&n.content.push(e),l.push(e);break}case"ElementEnd":{(t.end.type==="Close"||t.end.type==="Empty")&&l.pop();break}case"Attribute":{const e=l[l.length-1];e!=null&&e.attributes.push({local:t.local,prefix:t.prefix.length>0?t.prefix:void 0,value:t.value});break}case"Text":case"Cdata":{const e=l[l.length-1];e!=null&&t.text.trim().length>0&&e.content.push(t.text);break}}}export{c as xmlToObject};
@@ -0,0 +1 @@
1
+ import{getQName as i}from"./get-q-name.js";import{tokenize as k}from"./tokenizer/tokenize.js";var m=Object.defineProperty,c=Object.getOwnPropertySymbols,g=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable,p=(n,e,t)=>e in n?m(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,d=(n,e)=>{for(var t in e||(e={}))g.call(e,t)&&p(n,t,e[t]);if(c)for(var t of c(e))b.call(e,t)&&p(n,t,e[t]);return n};function v(n,e={}){const{transformers:t={},xmlOptions:s={},skipNodes:r=[],preserveTree:o=!1}=e,a={local:"root",attributes:[],content:[]},f={stack:[a],transformers:d({__default:l},t),skipNodes:new Set(r),preserveTree:o};return k(n,u=>{x(u,f)},s),a.string=l(a),a}function x(n,e){switch(n.type){case"ElementStart":{const t=i(n.local,n.prefix.length>0?n.prefix:void 0).toLowerCase(),s=e.skipNodes.has(t),r={local:n.local,prefix:n.prefix.length>0?n.prefix:void 0,attributes:[],content:[],skip:s},o=e.stack[e.stack.length-1];o!=null&&!o.skip&&o.content.push(r),e.stack.push(r);break}case"ElementEnd":{if(n.end.type==="Close"||n.end.type==="Empty"){const t=e.stack.pop();if(t==null||t.skip)return;const s=i(t.local,t.prefix).toLowerCase(),r=e.transformers[s];r!=null?t.string=r(t,e.stack):e.transformers.__default!=null&&(t.string=e.transformers.__default(t,e.stack)),e.preserveTree||(t.content=[])}break}case"Attribute":{const t=e.stack[e.stack.length-1];if(t==null||t.skip)return;t.attributes.push({local:n.local,prefix:n.prefix.length>0?n.prefix:void 0,value:n.value});break}case"Text":case"Cdata":{const t=e.stack[e.stack.length-1];if(t==null||t.skip)return;n.text.trim().length>0&&t.content.push(n.text);break}}}function l(n){return n.content.map(e=>typeof e=="string"?e:e.string).join("")}export{l as getXmlStringNodeContent,v as xmlToString};
@@ -0,0 +1,19 @@
1
+ import { TXmlStringNode, TXmlToStringOptions } from './xml-to-string';
2
+ /**
3
+ * Converts HTML to Markdown using a streaming XML tokenizer approach.
4
+ *
5
+ * @param html - The HTML string to convert
6
+ * @param options - Configuration options for the conversion
7
+ * @returns The converted Markdown string with node tree
8
+ *
9
+ * @remarks
10
+ * This implementation uses a streaming approach where transformers can only see
11
+ * previously processed nodes. Spacing is determined by looking at the previous
12
+ * sibling element type (block vs inline).
13
+ */
14
+ export declare function htmlToMarkdown(html: string, options?: THtmlToMarkdownOptions): TXmlStringNode & {
15
+ string: string;
16
+ };
17
+ interface THtmlToMarkdownOptions extends TXmlToStringOptions {
18
+ }
19
+ export {};
@@ -1,5 +1,6 @@
1
1
  export * from './config';
2
2
  export * from './get-q-name';
3
+ export * from './html-to-md';
3
4
  export * from './processor';
4
5
  export * from './selector';
5
6
  export * from './token-to-xml';
@@ -7,3 +8,4 @@ export * from './tokenizer';
7
8
  export * from './tokens-to-xml';
8
9
  export * from './xml-to-object';
9
10
  export * from './xml-to-simplified-object';
11
+ export * from './xml-to-string';
@@ -12,10 +12,6 @@ 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;
19
15
  /**
20
16
  * Creates a clone of the current stream.
21
17
  *
@@ -215,10 +211,6 @@ export declare class XmlStream {
215
211
  * @returns The calculated text position.
216
212
  */
217
213
  genTextPosFrom(pos: number): TTextPos;
218
- /**
219
- * Gets a snippet of text from the current position
220
- */
221
- getTextSnippet(start: number, length: number): string;
222
214
  }
223
215
  export interface TXmlStreamConfig {
224
216
  /**
@@ -261,12 +253,6 @@ export interface TXmlStreamConfig {
261
253
  * @default 30
262
254
  */
263
255
  contextSliceSize: number | false;
264
- /**
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
268
- */
269
- debugLogger?: (message: unknown, ...args: unknown[]) => void;
270
256
  }
271
257
  export type TXmlStreamOptions = {
272
258
  pos?: number;
@@ -1,6 +1,5 @@
1
- import { type TXmlStreamOptions, type TXmlToken } from './tokenizer';
1
+ import { type TXmlStreamOptions } from './tokenizer';
2
2
  export declare function xmlToObject(xmlString: string, options?: TXmlStreamOptions): TXmlNode;
3
- export declare function processTokenForObject(token: TXmlToken, stack: TXmlNode[]): void;
4
3
  export interface TXmlNode {
5
4
  local: string;
6
5
  prefix?: string;
@@ -0,0 +1,25 @@
1
+ import { type TXmlStreamOptions } from './tokenizer';
2
+ export declare function xmlToString(xmlString: string, options?: TXmlToStringOptions): TXmlStringNode & {
3
+ string: string;
4
+ };
5
+ export type TXmlToStringOptions = {
6
+ transformers?: TXmlStringTransformers;
7
+ xmlOptions?: TXmlStreamOptions;
8
+ skipNodes?: string[];
9
+ preserveTree?: boolean;
10
+ };
11
+ export type TXmlStringTransformer = (node: TXmlStringNode, stack: TXmlStringNode[]) => string;
12
+ export type TXmlStringTransformers = Record<string, TXmlStringTransformer>;
13
+ export interface TXmlStringNode {
14
+ local: string;
15
+ prefix?: string;
16
+ attributes: {
17
+ local: string;
18
+ prefix?: string;
19
+ value: string;
20
+ }[];
21
+ content: (TXmlStringNode | string)[];
22
+ skip?: boolean;
23
+ string?: string;
24
+ }
25
+ export declare function getXmlStringNodeContent(node: TXmlStringNode): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-tokenizer",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "private": false,
5
5
  "description": "Straightforward and typesafe XML tokenizer that streams tokens through a callback mechanism",
6
6
  "keywords": [],
@@ -23,17 +23,17 @@
23
23
  "README.md"
24
24
  ],
25
25
  "devDependencies": {
26
- "@types/node": "^22.15.29",
26
+ "@types/node": "^24.1.0",
27
27
  "@types/sax": "^1.2.7",
28
28
  "@types/xml2js": "^0.4.14",
29
29
  "camaro": "^6.2.3",
30
- "fast-xml-parser": "^5.2.3",
30
+ "fast-xml-parser": "^5.2.5",
31
31
  "sax": "^1.4.1",
32
32
  "saxen": "^10.0.0",
33
33
  "txml": "^5.1.1",
34
34
  "xml2js": "^0.6.2",
35
- "@blgc/config": "0.0.35",
36
- "rollup-presets": "0.0.21"
35
+ "@blgc/config": "0.0.36",
36
+ "rollup-presets": "0.0.22"
37
37
  },
38
38
  "size-limit": [
39
39
  {