xml-tokenizer 0.0.34 → 0.0.35

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 h=require("./ascii-constants.js"),c=require("./utils.js"),n=require("./XmlError.js"),S=Object.defineProperty,C=Object.getOwnPropertySymbols,E=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable,g=(d,t,e)=>t in d?S(d,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[t]=e,x=(d,t)=>{for(var e in t||(t={}))E.call(t,e)&&g(d,e,t[e]);if(C)for(var e of C(t))y.call(t,e)&&g(d,e,t[e]);return d},m=(d,t,e)=>g(d,typeof t!="symbol"?t+"":t,e);class f{constructor(t,e={}){m(this,"_text"),m(this,"_pos"),m(this,"_end"),m(this,"config");const{pos:i=0,strictDocument:s=!0,allowDtd:o=!0,rawTextElements:l=null,implicitSelfClosingElements:a=null,contextSliceSize:r=!1,debug:u=!1}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:s,allowDtd:o,rawTextElements:l,implicitSelfClosingElements:a,contextSliceSize:r,debug:u},this.config.debug&&this.debugLog("XmlStream initialized",{textLength:t.length,startPos:this._pos,platform:typeof process!="undefined"?process.arch:"unknown",nodeVersion:typeof process!="undefined"?process.version:"unknown",textSample:t.slice(0,100),charCodes:t.slice(0,20).split("").map((p,_)=>({char:p,code:p.charCodeAt(0),pos:_}))})}clone(){return new f(this._text,x({pos:this._pos},this.config))}debugLog(t,e){this.config.debug&&console.log(`[XML-TOKENIZER-DEBUG] ${t}`,e?JSON.stringify(e,null,2):"")}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new n.XmlError({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!c.isXmlChar(this.currCodeUnitUnchecked()))throw new n.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),s=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==s)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new n.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&c.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new n.XmlError({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),i=e.consumeReference();return i!=null?(this._pos+=e.getPos()-t,i):null}consumeReference(){if(!this.tryConsumeCodeUnit(h.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(h.HASH)){let e,i;this.tryConsumeCodeUnit(h.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(o=>o>=h.ZERO&&o<=h.NINE||o>=h.UPPERCASE_A&&o<=h.UPPERCASE_F||o>=h.LOWERCASE_A&&o<=h.LOWERCASE_F),i=16):(e=this.consumeCodeUnitsWhile(o=>c.isAsciiDigit(o)),i=10);const s=parseInt(e,i);isNaN(s)||!c.isXmlChar(s)?t=null:t={type:"Char",value:String.fromCodePoint(s)}}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(h.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new n.XmlError({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!c.isXmlNameStart(e))throw new n.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!c.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e,i;const s=this._pos;let o=null;if(this.debugLog("consumeQName started",{position:this._pos,totalLength:this._end,context:this.getTextAround(this._pos,30),textPos:this.genTextPosFrom(this._pos)}),this._pos<this._end){const r=this.currCodeUnitUnchecked(),u=c.isXmlNameStart(r);if(this.debugLog("consumeQName first character check",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),isXmlNameStart:u,isXmlName:c.isXmlName(r),position:this._pos,platform:typeof process!="undefined"?process.arch:"unknown"}),!u)throw this.debugLog("consumeQName failed: invalid first character",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),context:this.getTextAround(this._pos,50),surroundingBytes:Array.from(this._text.slice(Math.max(0,this._pos-5),this._pos+5)).map((p,_)=>({char:p,code:p.charCodeAt(0),hex:"0x"+p.charCodeAt(0).toString(16),relativePos:_-5}))}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}for(;this._pos<this._end;){const r=this.currCodeUnitUnchecked();if(r===h.COLON)if(o==null)o=this._pos,this._pos+=1,this.debugLog("consumeQName found colon separator",{position:this._pos-1});else throw this.debugLog("consumeQName failed: multiple colons",{position:this._pos}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));else if(c.isXmlName(r))this._pos+=1;else{this.debugLog("consumeQName loop break - invalid character",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),position:this._pos,reason:"not XML name character",isXmlName:c.isXmlName(r),consumedSoFar:this._text.slice(s,this._pos)});break}}let l,a;if(o!=null?(l=this._text.slice(s,o),a=this.sliceBack(o+1)):(l="",a=this.sliceBack(s)),l.length>0&&!c.isXmlNameStart((t=l[0])==null?void 0:t.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid prefix start",{prefix:l,local:a}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));if(a.length>0){if(!c.isXmlNameStart((e=a[0])==null?void 0:e.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid local name start",{local:a,prefix:l,char:a[0],charCode:(i=a[0])==null?void 0:i.codePointAt(0),platform:typeof process!="undefined"?process.arch:"unknown"}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debugLog("consumeQName failed: empty local name",{local:a,prefix:l}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));return this.debugLog("consumeQName succeeded",{prefix:l,local:a,startPos:s,endPos:this._pos,consumedText:this._text.slice(s,this._pos)}),[l,a]}consumeQuote(){const t=this.currCodeUnit();if(t===h.SINGLE_QUOTE||t===h.DOUBLE_QUOTE)return this._pos+=1,t;throw new n.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),s=Math.max(0,t-i),o=Math.min(this._end,t+i+e%2);return this._text.slice(s,o)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,s=1;for(let o=0;o<e;o++)this._text.charCodeAt(o)===h.LINE_FEED?(i++,s=1):s++;return x({row:i,col:s},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}exports.XmlStream=f;
1
+ "use strict";var h=require("./ascii-constants.js"),c=require("./utils.js"),n=require("./XmlError.js"),S=Object.defineProperty,C=Object.getOwnPropertySymbols,E=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable,f=(d,t,e)=>t in d?S(d,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[t]=e,x=(d,t)=>{for(var e in t||(t={}))E.call(t,e)&&f(d,e,t[e]);if(C)for(var e of C(t))y.call(t,e)&&f(d,e,t[e]);return d},m=(d,t,e)=>f(d,typeof t!="symbol"?t+"":t,e);class g{constructor(t,e={}){m(this,"_text"),m(this,"_pos"),m(this,"_end"),m(this,"config");const{pos:i=0,strictDocument:s=!0,allowDtd:o=!0,rawTextElements:l=null,implicitSelfClosingElements:a=null,contextSliceSize:r=!1,debug:u=!1}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:s,allowDtd:o,rawTextElements:l,implicitSelfClosingElements:a,contextSliceSize:r,debug:u},this.config.debug&&this.debugLog("XmlStream initialized",{textLength:t.length,startPos:this._pos,platform:typeof process!="undefined"?process.arch:"unknown",nodeVersion:typeof process!="undefined"?process.version:"unknown",textSample:t.slice(0,100),charCodes:t.slice(0,20).split("").map((p,_)=>({char:p,code:p.charCodeAt(0),pos:_}))})}clone(){return new g(this._text,x({pos:this._pos},this.config))}debugLog(t,e){this.config.debug&&console.info(`[XML-TOKENIZER-DEBUG] ${t}`,e?JSON.stringify(e,null,2):"")}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new n.XmlError({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!c.isXmlChar(this.currCodeUnitUnchecked()))throw new n.XmlError({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),s=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==s)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new n.XmlError({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&c.isXmlSpaceByte(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new n.XmlError({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new n.XmlError({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),i=e.consumeReference();return i!=null?(this._pos+=e.getPos()-t,i):null}consumeReference(){if(!this.tryConsumeCodeUnit(h.AMPERSAND))return null;let t;if(this.tryConsumeCodeUnit(h.HASH)){let e,i;this.tryConsumeCodeUnit(h.LOWERCASE_X)?(e=this.consumeCodeUnitsWhile(o=>o>=h.ZERO&&o<=h.NINE||o>=h.UPPERCASE_A&&o<=h.UPPERCASE_F||o>=h.LOWERCASE_A&&o<=h.LOWERCASE_F),i=16):(e=this.consumeCodeUnitsWhile(o=>c.isAsciiDigit(o)),i=10);const s=parseInt(e,i);isNaN(s)||!c.isXmlChar(s)?t=null:t={type:"Char",value:String.fromCodePoint(s)}}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(h.SEMICOLON)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new n.XmlError({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!c.isXmlNameStart(e))throw new n.XmlError({type:"InvalidName"},this.genTextPosFrom(t))}else if(!c.isXmlName(e))break;this._pos+=1}}consumeQName(){var t,e,i;const s=this._pos;let o=null;if(this.debugLog("consumeQName started",{position:this._pos,totalLength:this._end,context:this.getTextAround(this._pos,30),textPos:this.genTextPosFrom(this._pos)}),this._pos<this._end){const r=this.currCodeUnitUnchecked(),u=c.isXmlNameStart(r);if(this.debugLog("consumeQName first character check",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),isXmlNameStart:u,isXmlName:c.isXmlName(r),position:this._pos,platform:typeof process!="undefined"?process.arch:"unknown"}),!u)throw this.debugLog("consumeQName failed: invalid first character",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),context:this.getTextAround(this._pos,50),surroundingBytes:Array.from(this._text.slice(Math.max(0,this._pos-5),this._pos+5)).map((p,_)=>({char:p,code:p.charCodeAt(0),hex:"0x"+p.charCodeAt(0).toString(16),relativePos:_-5}))}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}for(;this._pos<this._end;){const r=this.currCodeUnitUnchecked();if(r===h.COLON)if(o==null)o=this._pos,this._pos+=1,this.debugLog("consumeQName found colon separator",{position:this._pos-1});else throw this.debugLog("consumeQName failed: multiple colons",{position:this._pos}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));else if(c.isXmlName(r))this._pos+=1;else{this.debugLog("consumeQName loop break - invalid character",{char:String.fromCharCode(r),charCode:r,charHex:"0x"+r.toString(16),position:this._pos,reason:"not XML name character",isXmlName:c.isXmlName(r),consumedSoFar:this._text.slice(s,this._pos)});break}}let l,a;if(o!=null?(l=this._text.slice(s,o),a=this.sliceBack(o+1)):(l="",a=this.sliceBack(s)),l.length>0&&!c.isXmlNameStart((t=l[0])==null?void 0:t.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid prefix start",{prefix:l,local:a}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));if(a.length>0){if(!c.isXmlNameStart((e=a[0])==null?void 0:e.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid local name start",{local:a,prefix:l,char:a[0],charCode:(i=a[0])==null?void 0:i.codePointAt(0),platform:typeof process!="undefined"?process.arch:"unknown"}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debugLog("consumeQName failed: empty local name",{local:a,prefix:l}),new n.XmlError({type:"InvalidName"},this.genTextPosFrom(s));return this.debugLog("consumeQName succeeded",{prefix:l,local:a,startPos:s,endPos:this._pos,consumedText:this._text.slice(s,this._pos)}),[l,a]}consumeQuote(){const t=this.currCodeUnit();if(t===h.SINGLE_QUOTE||t===h.DOUBLE_QUOTE)return this._pos+=1,t;throw new n.XmlError({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),s=Math.max(0,t-i),o=Math.min(this._end,t+i+e%2);return this._text.slice(s,o)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,s=1;for(let o=0;o<e;o++)this._text.charCodeAt(o)===h.LINE_FEED?(i++,s=1):s++;return x({row:i,col:s},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}exports.XmlStream=g;
@@ -1 +1 @@
1
- import{AMPERSAND as y,HASH as w,LOWERCASE_X as U,ZERO as N,NINE as v,UPPERCASE_A as P,UPPERCASE_F as E,LOWERCASE_A as b,LOWERCASE_F as k,SEMICOLON as T,COLON as A,SINGLE_QUOTE as L,DOUBLE_QUOTE as O,LINE_FEED as I}from"./ascii-constants.js";import{isXmlChar as x,isXmlSpaceByte as F,isAsciiDigit as W,isXmlNameStart as d,isXmlName as u}from"./utils.js";import{XmlError as r}from"./XmlError.js";var Q=Object.defineProperty,C=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable,f=(c,t,e)=>t in c?Q(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,S=(c,t)=>{for(var e in t||(t={}))X.call(t,e)&&f(c,e,t[e]);if(C)for(var e of C(t))R.call(t,e)&&f(c,e,t[e]);return c},m=(c,t,e)=>f(c,typeof t!="symbol"?t+"":t,e);class g{constructor(t,e={}){m(this,"_text"),m(this,"_pos"),m(this,"_end"),m(this,"config");const{pos:i=0,strictDocument:s=!0,allowDtd:o=!0,rawTextElements:a=null,implicitSelfClosingElements:h=null,contextSliceSize:n=!1,debug:p=!1}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:s,allowDtd:o,rawTextElements:a,implicitSelfClosingElements:h,contextSliceSize:n,debug:p},this.config.debug&&this.debugLog("XmlStream initialized",{textLength:t.length,startPos:this._pos,platform:typeof process!="undefined"?process.arch:"unknown",nodeVersion:typeof process!="undefined"?process.version:"unknown",textSample:t.slice(0,100),charCodes:t.slice(0,20).split("").map((l,_)=>({char:l,code:l.charCodeAt(0),pos:_}))})}clone(){return new g(this._text,S({pos:this._pos},this.config))}debugLog(t,e){this.config.debug&&console.log(`[XML-TOKENIZER-DEBUG] ${t}`,e?JSON.stringify(e,null,2):"")}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new r({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new r({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new r({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(!x(this.currCodeUnitUnchecked()))throw new r({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),s=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==s)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new r({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&&F(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new r({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new r({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(y))return null;let t;if(this.tryConsumeCodeUnit(w)){let e,i;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(o=>o>=N&&o<=v||o>=P&&o<=E||o>=b&&o<=k),i=16):(e=this.consumeCodeUnitsWhile(o=>W(o)),i=10);const s=parseInt(e,i);isNaN(s)||!x(s)?t=null:t={type:"Char",value:String.fromCodePoint(s)}}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(T)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new r({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(!d(e))throw new r({type:"InvalidName"},this.genTextPosFrom(t))}else if(!u(e))break;this._pos+=1}}consumeQName(){var t,e,i;const s=this._pos;let o=null;if(this.debugLog("consumeQName started",{position:this._pos,totalLength:this._end,context:this.getTextAround(this._pos,30),textPos:this.genTextPosFrom(this._pos)}),this._pos<this._end){const n=this.currCodeUnitUnchecked(),p=d(n);if(this.debugLog("consumeQName first character check",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),isXmlNameStart:p,isXmlName:u(n),position:this._pos,platform:typeof process!="undefined"?process.arch:"unknown"}),!p)throw this.debugLog("consumeQName failed: invalid first character",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),context:this.getTextAround(this._pos,50),surroundingBytes:Array.from(this._text.slice(Math.max(0,this._pos-5),this._pos+5)).map((l,_)=>({char:l,code:l.charCodeAt(0),hex:"0x"+l.charCodeAt(0).toString(16),relativePos:_-5}))}),new r({type:"InvalidName"},this.genTextPosFrom(s))}for(;this._pos<this._end;){const n=this.currCodeUnitUnchecked();if(n===A)if(o==null)o=this._pos,this._pos+=1,this.debugLog("consumeQName found colon separator",{position:this._pos-1});else throw this.debugLog("consumeQName failed: multiple colons",{position:this._pos}),new r({type:"InvalidName"},this.genTextPosFrom(s));else if(u(n))this._pos+=1;else{this.debugLog("consumeQName loop break - invalid character",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),position:this._pos,reason:"not XML name character",isXmlName:u(n),consumedSoFar:this._text.slice(s,this._pos)});break}}let a,h;if(o!=null?(a=this._text.slice(s,o),h=this.sliceBack(o+1)):(a="",h=this.sliceBack(s)),a.length>0&&!d((t=a[0])==null?void 0:t.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid prefix start",{prefix:a,local:h}),new r({type:"InvalidName"},this.genTextPosFrom(s));if(h.length>0){if(!d((e=h[0])==null?void 0:e.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid local name start",{local:h,prefix:a,char:h[0],charCode:(i=h[0])==null?void 0:i.codePointAt(0),platform:typeof process!="undefined"?process.arch:"unknown"}),new r({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debugLog("consumeQName failed: empty local name",{local:h,prefix:a}),new r({type:"InvalidName"},this.genTextPosFrom(s));return this.debugLog("consumeQName succeeded",{prefix:a,local:h,startPos:s,endPos:this._pos,consumedText:this._text.slice(s,this._pos)}),[a,h]}consumeQuote(){const t=this.currCodeUnit();if(t===L||t===O)return this._pos+=1,t;throw new r({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),s=Math.max(0,t-i),o=Math.min(this._end,t+i+e%2);return this._text.slice(s,o)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,s=1;for(let o=0;o<e;o++)this._text.charCodeAt(o)===I?(i++,s=1):s++;return S({row:i,col:s},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}export{g as XmlStream};
1
+ import{AMPERSAND as y,HASH as w,LOWERCASE_X as U,ZERO as N,NINE as v,UPPERCASE_A as P,UPPERCASE_F as E,LOWERCASE_A as b,LOWERCASE_F as k,SEMICOLON as T,COLON as A,SINGLE_QUOTE as L,DOUBLE_QUOTE as O,LINE_FEED as I}from"./ascii-constants.js";import{isXmlChar as x,isXmlSpaceByte as F,isAsciiDigit as W,isXmlNameStart as d,isXmlName as u}from"./utils.js";import{XmlError as r}from"./XmlError.js";var Q=Object.defineProperty,C=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable,f=(c,t,e)=>t in c?Q(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,S=(c,t)=>{for(var e in t||(t={}))X.call(t,e)&&f(c,e,t[e]);if(C)for(var e of C(t))R.call(t,e)&&f(c,e,t[e]);return c},m=(c,t,e)=>f(c,typeof t!="symbol"?t+"":t,e);class g{constructor(t,e={}){m(this,"_text"),m(this,"_pos"),m(this,"_end"),m(this,"config");const{pos:i=0,strictDocument:s=!0,allowDtd:o=!0,rawTextElements:a=null,implicitSelfClosingElements:h=null,contextSliceSize:n=!1,debug:p=!1}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:s,allowDtd:o,rawTextElements:a,implicitSelfClosingElements:h,contextSliceSize:n,debug:p},this.config.debug&&this.debugLog("XmlStream initialized",{textLength:t.length,startPos:this._pos,platform:typeof process!="undefined"?process.arch:"unknown",nodeVersion:typeof process!="undefined"?process.version:"unknown",textSample:t.slice(0,100),charCodes:t.slice(0,20).split("").map((l,_)=>({char:l,code:l.charCodeAt(0),pos:_}))})}clone(){return new g(this._text,S({pos:this._pos},this.config))}debugLog(t,e){this.config.debug&&console.info(`[XML-TOKENIZER-DEBUG] ${t}`,e?JSON.stringify(e,null,2):"")}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new r({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new r({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new r({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(!x(this.currCodeUnitUnchecked()))throw new r({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}startsWithIgnoreCase(t){if(this._pos+t.length>this._end)return!1;for(let e=0;e<t.length;e++){const i=this._text.charCodeAt(this._pos+e),s=t.charCodeAt(e);if((i>=97&&i<=122?i-32:i)!==s)return!1}return!0}skipString(t){if(!this.startsWith(t))throw new r({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&&F(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new r({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new r({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(y))return null;let t;if(this.tryConsumeCodeUnit(w)){let e,i;this.tryConsumeCodeUnit(U)?(e=this.consumeCodeUnitsWhile(o=>o>=N&&o<=v||o>=P&&o<=E||o>=b&&o<=k),i=16):(e=this.consumeCodeUnitsWhile(o=>W(o)),i=10);const s=parseInt(e,i);isNaN(s)||!x(s)?t=null:t={type:"Char",value:String.fromCodePoint(s)}}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(T)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new r({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(!d(e))throw new r({type:"InvalidName"},this.genTextPosFrom(t))}else if(!u(e))break;this._pos+=1}}consumeQName(){var t,e,i;const s=this._pos;let o=null;if(this.debugLog("consumeQName started",{position:this._pos,totalLength:this._end,context:this.getTextAround(this._pos,30),textPos:this.genTextPosFrom(this._pos)}),this._pos<this._end){const n=this.currCodeUnitUnchecked(),p=d(n);if(this.debugLog("consumeQName first character check",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),isXmlNameStart:p,isXmlName:u(n),position:this._pos,platform:typeof process!="undefined"?process.arch:"unknown"}),!p)throw this.debugLog("consumeQName failed: invalid first character",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),context:this.getTextAround(this._pos,50),surroundingBytes:Array.from(this._text.slice(Math.max(0,this._pos-5),this._pos+5)).map((l,_)=>({char:l,code:l.charCodeAt(0),hex:"0x"+l.charCodeAt(0).toString(16),relativePos:_-5}))}),new r({type:"InvalidName"},this.genTextPosFrom(s))}for(;this._pos<this._end;){const n=this.currCodeUnitUnchecked();if(n===A)if(o==null)o=this._pos,this._pos+=1,this.debugLog("consumeQName found colon separator",{position:this._pos-1});else throw this.debugLog("consumeQName failed: multiple colons",{position:this._pos}),new r({type:"InvalidName"},this.genTextPosFrom(s));else if(u(n))this._pos+=1;else{this.debugLog("consumeQName loop break - invalid character",{char:String.fromCharCode(n),charCode:n,charHex:"0x"+n.toString(16),position:this._pos,reason:"not XML name character",isXmlName:u(n),consumedSoFar:this._text.slice(s,this._pos)});break}}let a,h;if(o!=null?(a=this._text.slice(s,o),h=this.sliceBack(o+1)):(a="",h=this.sliceBack(s)),a.length>0&&!d((t=a[0])==null?void 0:t.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid prefix start",{prefix:a,local:h}),new r({type:"InvalidName"},this.genTextPosFrom(s));if(h.length>0){if(!d((e=h[0])==null?void 0:e.codePointAt(0)))throw this.debugLog("consumeQName failed: invalid local name start",{local:h,prefix:a,char:h[0],charCode:(i=h[0])==null?void 0:i.codePointAt(0),platform:typeof process!="undefined"?process.arch:"unknown"}),new r({type:"InvalidName"},this.genTextPosFrom(s))}else throw this.debugLog("consumeQName failed: empty local name",{local:h,prefix:a}),new r({type:"InvalidName"},this.genTextPosFrom(s));return this.debugLog("consumeQName succeeded",{prefix:a,local:h,startPos:s,endPos:this._pos,consumedText:this._text.slice(s,this._pos)}),[a,h]}consumeQuote(){const t=this.currCodeUnit();if(t===L||t===O)return this._pos+=1,t;throw new r({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),s=Math.max(0,t-i),o=Math.min(this._end,t+i+e%2);return this._text.slice(s,o)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,s=1;for(let o=0;o<e;o++)this._text.charCodeAt(o)===I?(i++,s=1):s++;return S({row:i,col:s},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}export{g as XmlStream};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-tokenizer",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "private": false,
5
5
  "description": "Straightforward and typesafe XML tokenizer that streams tokens through a callback mechanism",
6
6
  "keywords": [],