@bbob/parser
Advanced tools
Comparing version 0.0.0-alpha.cad4675 to 0.0.0-alpha.ede7040
@@ -247,6 +247,6 @@ (function (global, factory) { | ||
}; | ||
const tokenToText = (token)=>{ | ||
let text = OPEN_BRAKET; | ||
const tokenToText = (token, openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET)=>{ | ||
let text = openTag; | ||
text += getTokenValue(token); | ||
text += CLOSE_BRAKET; | ||
text += closeTag; | ||
return text; | ||
@@ -300,4 +300,4 @@ }; | ||
} | ||
toString() { | ||
return tokenToText(this); | ||
toString({ openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET } = {}) { | ||
return tokenToText(this, openTag, closeTag); | ||
} | ||
@@ -452,2 +452,3 @@ constructor(type, value, row = 0, col = 0, start = 0, end = 0){ | ||
const contextFreeTags = (options.contextFreeTags || []).filter(Boolean).map((tag)=>tag.toLowerCase()); | ||
const caseFreeTags = options.caseFreeTags || false; | ||
const nestedMap = new Map(); | ||
@@ -491,4 +492,2 @@ const onToken = options.onToken || (()=>{}); | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ function emitToken(type, value, startPos, endPos) { | ||
@@ -686,8 +685,8 @@ const token = createTokenOfType(type, value, row, prevCol, startPos, endPos); | ||
} | ||
function isTokenNested(token) { | ||
const value = openTag + SLASH + token.getValue(); | ||
function isTokenNested(tokenValue) { | ||
const value = openTag + SLASH + tokenValue; | ||
if (nestedMap.has(value)) { | ||
return !!nestedMap.get(value); | ||
} else { | ||
const status = buffer.indexOf(value) > -1; | ||
const status = caseFreeTags ? buffer.toLowerCase().indexOf(value.toLowerCase()) > -1 : buffer.indexOf(value) > -1; | ||
nestedMap.set(value, status); | ||
@@ -729,3 +728,3 @@ return status; | ||
const onlyAllowTags = (options.onlyAllowTags || []).filter(Boolean).map((tag)=>tag.toLowerCase()); | ||
options.caseFreeTags || false; | ||
const caseFreeTags = options.caseFreeTags || false; | ||
let tokenizer = null; | ||
@@ -755,5 +754,6 @@ /** | ||
function isTokenNested(token) { | ||
const value = token.getValue(); | ||
const tokenValue = token.getValue(); | ||
const value = caseFreeTags ? tokenValue.toLowerCase() : tokenValue; | ||
const { isTokenNested } = tokenizer || {}; | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(token)) { | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(value)) { | ||
nestedTagsMap.add(value); | ||
@@ -767,3 +767,3 @@ return true; | ||
*/ function isTagNested(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
return Boolean(nestedTagsMap.has(caseFreeTags ? tagName.toLowerCase() : tagName)); | ||
} | ||
@@ -853,14 +853,20 @@ /** | ||
*/ function handleTagEnd(token) { | ||
const lastTagNode = nestedNodes.last(); | ||
if (isTagNode(lastTagNode)) { | ||
lastTagNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
const tagName = token.getValue().slice(1); | ||
const lastNestedNode = nestedNodes.flush(); | ||
flushTagNodes(); | ||
const lastNestedNode = nestedNodes.flush(); | ||
if (lastNestedNode) { | ||
const nodes = getNodes(); | ||
if (isTagNode(lastNestedNode)) { | ||
lastNestedNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
appendNodes(nodes, lastNestedNode); | ||
} else if (!isTagNested(tagName)) { | ||
const nodes = getNodes(); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} else if (typeof options.onError === "function") { | ||
@@ -923,3 +929,6 @@ const tag = token.getValue(); | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
@@ -930,3 +939,6 @@ } else if (token.isText()) { | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
@@ -951,2 +963,3 @@ } | ||
contextFreeTags: options.contextFreeTags, | ||
caseFreeTags: options.caseFreeTags, | ||
enableEscapeTags: options.enableEscapeTags | ||
@@ -953,0 +966,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).BbobParser={})}(this,function(t){"use strict";function e(t){return"object"==typeof t&&null!==t&&"tag"in t}function r(t,e,r){return Object.keys(t).reduce((r,n)=>e(r,n,t),r)}function n(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/(javascript|data|vbscript):/gi,"$1%3A")}function s(t,e){switch(typeof e){case"boolean":return e?""+t:"";case"number":return`${t}="${e}"`;case"string":return`${t}="${n(e)}"`;case"object":return`${t}="${n(JSON.stringify(e))}"`;default:return""}}function i(t){return null==t?"":r(t,(t,e,r)=>[...t,s(e,r[e])],[""]).join(" ")}let a=(t,e)=>{let n=r(e||{},(t,e,r)=>r[e]===e?r[e]:null,null);if(n){let r=s(t,n),a={...e};delete a[n+""];let u=i(a);return`${r}${u}`}return`${t}${i(e)}`},u=(t,r,n)=>{let s=t=>e(t)?t.toString({openTag:r,closeTag:n}):t+"";return Array.isArray(t)?t.reduce((t,e)=>null!==e?t+s(e):t,""):t?s(t):null};class o{attr(t,e){return void 0!==e&&(this.attrs[t]=e),this.attrs[t]}append(t){Array.isArray(this.content)&&this.content.push(t)}setStart(t){this.start=t}setEnd(t){this.end=t}get length(){return function t(r){return e(r)&&Array.isArray(r.content)?r.content.reduce((e,r)=>e+t(r),0):"string"==typeof r?(r+"").length:0}(this)}toTagStart({openTag:t="[",closeTag:e="]"}={}){let r=a(this.tag+"",this.attrs);return`${t}${r}${e}`}toTagEnd({openTag:t="[",closeTag:e="]"}={}){return`${t}/${this.tag}${e}`}toTagNode(){let t=new o((this.tag+"").toLowerCase(),this.attrs,this.content);return this.start&&t.setStart(this.start),this.end&&t.setEnd(this.end),t}toString({openTag:t="[",closeTag:e="]"}={}){let r=this.content?u(this.content,t,e):"",n=this.toTagStart({openTag:t,closeTag:e});return null===this.content||Array.isArray(this.content)&&0===this.content.length?n:`${n}${r}${this.toTagEnd({openTag:t,closeTag:e})}`}static create(t,e={},r=null,n){let s=new o(t,e,r);return n&&s.setStart(n),s}static isOf(t,e){return t.tag===e}constructor(t,e,r){this.tag=t,this.attrs=e,this.content=r}}let l=t=>t&&void 0!==t.v?t.v:"",h=t=>t&&t.l||0,g=t=>t&&t.r||0,c=t=>t&&t.s||0,f=t=>t&&t.e||0,p=t=>!!t&&void 0!==t.t&&(5===t.t||6===t.t||1===t.t),d=t=>!!t&&void 0!==t.t&&2===t.t,T=t=>47===l(t).charCodeAt(0),b=t=>!T(t),y=t=>!!t&&void 0!==t.t&&3===t.t,A=t=>!!t&&void 0!==t.t&&4===t.t,x=t=>{let e=l(t);return T(t)?e.slice(1):e},k=t=>"["+l(t)+"]";class N{get type(){return this.t}isEmpty(){return 0===this.t||isNaN(this.t)}isText(){return p(this)}isTag(){return d(this)}isAttrName(){return y(this)}isAttrValue(){return A(this)}isStart(){return b(this)}isEnd(){return T(this)}getName(){return x(this)}getValue(){return l(this)}getLine(){return h(this)}getColumn(){return g(this)}getStart(){return c(this)}getEnd(){return f(this)}toString(){return k(this)}constructor(t,e,r=0,n=0,s=0,i=0){this.l=r,this.r=n,this.t=t||0,this.v=e+"",this.s=s,this.e=i}}class v{skip(t=1,e){this.c.pos+=t,this.o&&this.o.onSkip&&!e&&this.o.onSkip()}hasNext(){return this.c.len>this.c.pos}getCurr(){return void 0===this.s[this.c.pos]?"":this.s[this.c.pos]}getPos(){return this.c.pos}getLength(){return this.c.len}getRest(){return this.s.substring(this.c.pos)}getNext(){let t=this.c.pos+1;return t<=this.s.length-1?this.s[t]:null}getPrev(){let t=this.c.pos-1;return void 0===this.s[t]?null:this.s[t]}isLast(){return this.c.pos===this.c.len}includes(t){return this.s.indexOf(t,this.c.pos)>=0}grabWhile(t,e){let r=0;if(this.hasNext())for(r=this.c.pos;this.hasNext()&&t(this.getCurr());)this.skip(1,e);return this.s.substring(r,this.c.pos)}grabN(t=0){return this.s.substring(this.c.pos,this.c.pos+t)}substrUntilChar(t){let{pos:e}=this.c,r=this.s.indexOf(t,e);return r>=0?this.s.substring(e,r):""}constructor(t,e={}){this.s=t,this.c={pos:0,len:t.length},this.o=e}}let $=(t,e)=>new v(t,e),C=(t,e)=>{for(;t.charAt(0)===e;)t=t.substring(1);for(;t.charAt(t.length-1)===e;)t=t.substring(0,t.length-1);return t},S=t=>t.replace('\\"','"');function m(t,e,r=0,n=0,s=0,i=0){return new N(t,e,r,n,s,i)}let E=[" "," "],w=["="," "," "],L=t=>E.indexOf(t)>=0,O=t=>"\\"===t,P=t=>w.indexOf(t)>=0,W=t=>"\n"===t,V=t=>S(C(t,'"'));function j(t,e={}){let r=0,n=0,s=0,i=-1,a=0,u=0,o="",l=Array(Math.floor(t.length)),h=e.openTag||"[",g=e.closeTag||"]",c=!!e.enableEscapeTags,f=(e.contextFreeTags||[]).filter(Boolean).map(t=>t.toLowerCase()),p=new Map,d=e.onToken||(()=>{}),T=[g,h,'"',"\\"," "," ","=","\n","!"],b=[h," "," ","\n"],y=t=>T.indexOf(t)>=0,A=t=>-1===b.indexOf(t),x=t=>t===h||t===g||"\\"===t,k=()=>{s++},N=(t,e)=>{""!==o&&e&&(o=""),""===o&&f.includes(t.toLowerCase())&&(o=t)},v=$(t,{onSkip:k});function C(t,e,a,u){let o=m(t,e,r,n,a,u);d(o),n=s,l[i+=1]=o}return{tokenize:function(){for(a=0;v.hasNext();)switch(a){case 1:a=function(){let t=v.getCurr(),e=v.getNext();v.skip();let r=v.substrUntilChar(g),n=0===r.length||r.indexOf(h)>=0;if(e&&y(e)||n||v.isLast())return C(1,t),0;let s=-1===r.indexOf("="),i="/"===r[0];if(s||i){let t=v.getPos()-1,e=v.grabWhile(t=>t!==g),r=t+e.length+2;return v.skip(),C(2,e,t,r),N(e,i),0}return 2}();break;case 2:a=function(){let t=v.getPos(),e=$(v.grabWhile(t=>t!==g,!0),{onSkip:k}),r=e.includes(" ");for(u=0;e.hasNext();)u=function(t,e,r){if(1===u){let e=t.grabWhile(t=>!("="===t||L(t))),r=t.isLast(),n="="!==t.getCurr();return(t.skip(),r||n?C(4,V(e)):C(3,e),r)?0:n?1:2}if(2===u){let r=!1,s=t.grabWhile(n=>{let s='"'===n,i=t.getPrev(),a=t.getNext(),u="="===a,o=L(n),l=a&&L(a);return!!(r&&P(n))||(!s||"\\"===i||!!(r=!r)||!!u||!!l)&&(!!e||!o)});return(t.skip(),C(4,V(s)),'"'===t.getPrev()&&n++,t.isLast())?0:1}let s=r+t.getPos()-1,i=t.grabWhile(e=>!("="===e||L(e)||t.isLast()));return(C(2,i,s,r+t.getLength()+1),N(i),t.skip(),n++,e)?2:t.includes("=")?1:2}(e,!r,t);return v.skip(),0}();break;default:a=function(){if(W(v.getCurr()))return C(6,v.getCurr()),v.skip(),s=0,n=0,r++,0;if(L(v.getCurr()))return C(5,v.grabWhile(L)),0;if(v.getCurr()===h){if(o){let t=h.length+1+o.length,e=`${h}/${o}`;if(v.grabN(t)===e)return 1}else if(v.includes(g))return 1;return C(1,v.getCurr()),v.skip(),n++,0}if(c){if(O(v.getCurr())){let t=v.getCurr(),e=v.getNext();return(v.skip(),e&&x(e))?(v.skip(),C(1,e)):C(1,t),0}return C(1,v.grabWhile(t=>A(t)&&!O(t))),0}return C(1,v.grabWhile(A)),0}()}return l.length=i+1,l},isTokenNested:function(e){let r=h+"/"+e.getValue();if(p.has(r))return!!p.get(r);{let e=t.indexOf(r)>-1;return p.set(r,e),e}}}}class z{last(){return Array.isArray(this.n)&&this.n.length>0&&void 0!==this.n[this.n.length-1]?this.n[this.n.length-1]:null}flush(){return!!this.n.length&&this.n.pop()}push(t){this.n.push(t)}toArray(){return this.n}constructor(){this.n=[]}}let F=()=>new z;function B(t,r={}){var n;let s=r.openTag||"[",i=r.closeTag||"]",a=(r.onlyAllowTags||[]).filter(Boolean).map(t=>t.toLowerCase());r.caseFreeTags;let u=null,l=F(),h=F(),g=F(),c=F(),f=new Set;function p(){g.flush()&&c.flush()}function d(){let t=h.last();return t&&e(t)?t.content:l.toArray()}function T(t,e,r=!0){Array.isArray(t)&&void 0!==e&&(t.push(e.toTagStart({openTag:s,closeTag:i})),Array.isArray(e.content)&&e.content.length&&(e.content.forEach(e=>{t.push(e)}),r&&t.push(e.toTagEnd({openTag:s,closeTag:i}))))}function b(t,r){if(Array.isArray(t)&&void 0!==r){if(e(r)){var n;(n=r.tag,!a.length||a.indexOf(n.toLowerCase())>=0)?t.push(r.toTagNode()):T(t,r)}else t.push(r)}}(u=(r.createTokenizer?r.createTokenizer:j)(t,{onToken:function(t){t.isTag()?(t.isStart()&&function(t){p();let e=o.create(t.getValue(),{},[],{from:t.getStart(),to:t.getEnd()}),r=function(t){let e=t.getValue(),{isTokenNested:r}=u||{};return!f.has(e)&&r&&r(t)?(f.add(e),!0):f.has(e)}(t);g.push(e),r?h.push(e):b(d(),e)}(t),t.isEnd()&&function(t){let n=h.last();e(n)&&n.setEnd({from:t.getStart(),to:t.getEnd()}),p();let s=h.flush();if(s)b(d(),s);else if("function"==typeof r.onError){let e=t.getValue(),n=t.getLine(),s=t.getColumn();r.onError({tagName:e,lineNumber:n,columnNumber:s})}}(t)):!function(t){var e;let r=g.last(),n=t.getValue(),s=(e=t.toString(),!!f.has(e)),i=d();if(null!==r){if(t.isAttrName()){c.push(n);let t=c.last();t&&r.attr(t,"")}else if(t.isAttrValue()){let t=c.last();t?(r.attr(t,n),c.flush()):r.attr(n,n)}else t.isText()?s?r.append(n):b(i,n):t.isTag()&&b(i,t.toString())}else t.isText()?b(i,n):t.isTag()&&b(i,t.toString())}(t)},openTag:s,closeTag:i,onlyAllowTags:r.onlyAllowTags,contextFreeTags:r.contextFreeTags,enableEscapeTags:r.enableEscapeTags})).tokenize();let y=h.flush();return null!==y&&y&&e(y)&&(n=y.tag,f.has(n))&&T(d(),y,!1),l.toArray()}t.TagNode=o,t.createLexer=j,t.createTokenOfType=m,t.default=B,t.parse=B,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).BbobParser={})}(this,function(t){"use strict";let e="[",r="]";function s(t){return"object"==typeof t&&null!==t&&"tag"in t}function n(t,e,r){return Object.keys(t).reduce((r,s)=>e(r,s,t),r)}function i(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/(javascript|data|vbscript):/gi,"$1%3A")}function a(t,e){switch(typeof e){case"boolean":return e?""+t:"";case"number":return`${t}="${e}"`;case"string":return`${t}="${i(e)}"`;case"object":return`${t}="${i(JSON.stringify(e))}"`;default:return""}}function o(t){return null==t?"":n(t,(t,e,r)=>[...t,a(e,r[e])],[""]).join(" ")}let u=(t,e)=>{let r=n(e||{},(t,e,r)=>r[e]===e?r[e]:null,null);if(r){let s=a(t,r),n={...e};delete n[r+""];let i=o(n);return`${s}${i}`}return`${t}${o(e)}`},l=(t,e,r)=>{let n=t=>s(t)?t.toString({openTag:e,closeTag:r}):t+"";return Array.isArray(t)?t.reduce((t,e)=>null!==e?t+n(e):t,""):t?n(t):null};class h{attr(t,e){return void 0!==e&&(this.attrs[t]=e),this.attrs[t]}append(t){Array.isArray(this.content)&&this.content.push(t)}setStart(t){this.start=t}setEnd(t){this.end=t}get length(){return function t(e){return s(e)&&Array.isArray(e.content)?e.content.reduce((e,r)=>e+t(r),0):"string"==typeof e?(e+"").length:0}(this)}toTagStart({openTag:t=e,closeTag:s=r}={}){let n=u(this.tag+"",this.attrs);return`${t}${n}${s}`}toTagEnd({openTag:t=e,closeTag:s=r}={}){return`${t}/${this.tag}${s}`}toTagNode(){let t=new h((this.tag+"").toLowerCase(),this.attrs,this.content);return this.start&&t.setStart(this.start),this.end&&t.setEnd(this.end),t}toString({openTag:t=e,closeTag:s=r}={}){let n=this.content?l(this.content,t,s):"",i=this.toTagStart({openTag:t,closeTag:s});return null===this.content||Array.isArray(this.content)&&0===this.content.length?i:`${i}${n}${this.toTagEnd({openTag:t,closeTag:s})}`}static create(t,e={},r=null,s){let n=new h(t,e,r);return s&&n.setStart(s),n}static isOf(t,e){return t.tag===e}constructor(t,e,r){this.tag=t,this.attrs=e,this.content=r}}let g=t=>t&&void 0!==t.v?t.v:"",c=t=>t&&t.l||0,f=t=>t&&t.r||0,p=t=>t&&t.s||0,d=t=>t&&t.e||0,T=t=>!!t&&void 0!==t.t&&(5===t.t||6===t.t||1===t.t),b=t=>!!t&&void 0!==t.t&&2===t.t,y=t=>47===g(t).charCodeAt(0),A=t=>!y(t),x=t=>!!t&&void 0!==t.t&&3===t.t,k=t=>!!t&&void 0!==t.t&&4===t.t,C=t=>{let e=g(t);return y(t)?e.slice(1):e},N=(t,s=e,n=r)=>s+(g(t)+n);class S{get type(){return this.t}isEmpty(){return 0===this.t||isNaN(this.t)}isText(){return T(this)}isTag(){return b(this)}isAttrName(){return x(this)}isAttrValue(){return k(this)}isStart(){return A(this)}isEnd(){return y(this)}getName(){return C(this)}getValue(){return g(this)}getLine(){return c(this)}getColumn(){return f(this)}getStart(){return p(this)}getEnd(){return d(this)}toString({openTag:t=e,closeTag:s=r}={}){return N(this,t,s)}constructor(t,e,r=0,s=0,n=0,i=0){this.l=r,this.r=s,this.t=t||0,this.v=e+"",this.s=n,this.e=i}}class ${skip(t=1,e){this.c.pos+=t,this.o&&this.o.onSkip&&!e&&this.o.onSkip()}hasNext(){return this.c.len>this.c.pos}getCurr(){return void 0===this.s[this.c.pos]?"":this.s[this.c.pos]}getPos(){return this.c.pos}getLength(){return this.c.len}getRest(){return this.s.substring(this.c.pos)}getNext(){let t=this.c.pos+1;return t<=this.s.length-1?this.s[t]:null}getPrev(){let t=this.c.pos-1;return void 0===this.s[t]?null:this.s[t]}isLast(){return this.c.pos===this.c.len}includes(t){return this.s.indexOf(t,this.c.pos)>=0}grabWhile(t,e){let r=0;if(this.hasNext())for(r=this.c.pos;this.hasNext()&&t(this.getCurr());)this.skip(1,e);return this.s.substring(r,this.c.pos)}grabN(t=0){return this.s.substring(this.c.pos,this.c.pos+t)}substrUntilChar(t){let{pos:e}=this.c,r=this.s.indexOf(t,e);return r>=0?this.s.substring(e,r):""}constructor(t,e={}){this.s=t,this.c={pos:0,len:t.length},this.o=e}}let v=(t,e)=>new $(t,e),w=(t,e)=>{for(;t.charAt(0)===e;)t=t.substring(1);for(;t.charAt(t.length-1)===e;)t=t.substring(0,t.length-1);return t},L=t=>t.replace('\\"','"');function m(t,e,r=0,s=0,n=0,i=0){return new S(t,e,r,s,n,i)}let E=[" "," "],O=["="," "," "],P=t=>E.indexOf(t)>=0,W=t=>"\\"===t,V=t=>O.indexOf(t)>=0,j=t=>"\n"===t,F=t=>L(w(t,'"'));function z(t,s={}){let n=0,i=0,a=0,o=-1,u=0,l=0,h="",g=Array(Math.floor(t.length)),c=s.openTag||e,f=s.closeTag||r,p=!!s.enableEscapeTags,d=(s.contextFreeTags||[]).filter(Boolean).map(t=>t.toLowerCase()),T=s.caseFreeTags||!1,b=new Map,y=s.onToken||(()=>{}),A=[f,c,'"',"\\"," "," ","=","\n","!"],x=[c," "," ","\n"],k=t=>A.indexOf(t)>=0,C=t=>-1===x.indexOf(t),N=t=>t===c||t===f||"\\"===t,S=()=>{a++},$=(t,e)=>{""!==h&&e&&(h=""),""===h&&d.includes(t.toLowerCase())&&(h=t)},w=v(t,{onSkip:S});function L(t,e,r,s){let u=m(t,e,n,i,r,s);y(u),i=a,g[o+=1]=u}return{tokenize:function(){for(u=0;w.hasNext();)switch(u){case 1:u=function(){let t=w.getCurr(),e=w.getNext();w.skip();let r=w.substrUntilChar(f),s=0===r.length||r.indexOf(c)>=0;if(e&&k(e)||s||w.isLast())return L(1,t),0;let n=-1===r.indexOf("="),i="/"===r[0];if(n||i){let t=w.getPos()-1,e=w.grabWhile(t=>t!==f),r=t+e.length+2;return w.skip(),L(2,e,t,r),$(e,i),0}return 2}();break;case 2:u=function(){let t=w.getPos(),e=v(w.grabWhile(t=>t!==f,!0),{onSkip:S}),r=e.includes(" ");for(l=0;e.hasNext();)l=function(t,e,r){if(1===l){let e=t.grabWhile(t=>!("="===t||P(t))),r=t.isLast(),s="="!==t.getCurr();return(t.skip(),r||s?L(4,F(e)):L(3,e),r)?0:s?1:2}if(2===l){let r=!1,s=t.grabWhile(s=>{let n='"'===s,i=t.getPrev(),a=t.getNext(),o="="===a,u=P(s),l=a&&P(a);return!!(r&&V(s))||(!n||"\\"===i||!!(r=!r)||!!o||!!l)&&(!!e||!u)});return(t.skip(),L(4,F(s)),'"'===t.getPrev()&&i++,t.isLast())?0:1}let s=r+t.getPos()-1,n=t.grabWhile(e=>!("="===e||P(e)||t.isLast()));return(L(2,n,s,r+t.getLength()+1),$(n),t.skip(),i++,e)?2:t.includes("=")?1:2}(e,!r,t);return w.skip(),0}();break;default:u=function(){if(j(w.getCurr()))return L(6,w.getCurr()),w.skip(),a=0,i=0,n++,0;if(P(w.getCurr()))return L(5,w.grabWhile(P)),0;if(w.getCurr()===c){if(h){let t=c.length+1+h.length,e=`${c}/${h}`;if(w.grabN(t)===e)return 1}else if(w.includes(f))return 1;return L(1,w.getCurr()),w.skip(),i++,0}if(p){if(W(w.getCurr())){let t=w.getCurr(),e=w.getNext();return(w.skip(),e&&N(e))?(w.skip(),L(1,e)):L(1,t),0}return L(1,w.grabWhile(t=>C(t)&&!W(t))),0}return L(1,w.grabWhile(C)),0}()}return g.length=o+1,g},isTokenNested:function(e){let r=c+"/"+e;if(b.has(r))return!!b.get(r);{let e=T?t.toLowerCase().indexOf(r.toLowerCase())>-1:t.indexOf(r)>-1;return b.set(r,e),e}}}}class B{last(){return Array.isArray(this.n)&&this.n.length>0&&void 0!==this.n[this.n.length-1]?this.n[this.n.length-1]:null}flush(){return!!this.n.length&&this.n.pop()}push(t){this.n.push(t)}toArray(){return this.n}constructor(){this.n=[]}}let M=()=>new B;function U(t,n={}){let i=n.openTag||e,a=n.closeTag||r,o=(n.onlyAllowTags||[]).filter(Boolean).map(t=>t.toLowerCase()),u=n.caseFreeTags||!1,l=null,g=M(),c=M(),f=M(),p=M(),d=new Set;function T(t){return!!d.has(u?t.toLowerCase():t)}function b(){f.flush()&&p.flush()}function y(){let t=c.last();return t&&s(t)?t.content:g.toArray()}function A(t,e,r=!0){Array.isArray(t)&&void 0!==e&&(t.push(e.toTagStart({openTag:i,closeTag:a})),Array.isArray(e.content)&&e.content.length&&(e.content.forEach(e=>{t.push(e)}),r&&t.push(e.toTagEnd({openTag:i,closeTag:a}))))}function x(t,e){if(Array.isArray(t)&&void 0!==e){if(s(e)){var r;(r=e.tag,!o.length||o.indexOf(r.toLowerCase())>=0)?t.push(e.toTagNode()):A(t,e)}else t.push(e)}}(l=(n.createTokenizer?n.createTokenizer:z)(t,{onToken:function(t){t.isTag()?(t.isStart()&&function(t){b();let e=h.create(t.getValue(),{},[],{from:t.getStart(),to:t.getEnd()}),r=function(t){let e=t.getValue(),r=u?e.toLowerCase():e,{isTokenNested:s}=l||{};return!d.has(r)&&s&&s(r)?(d.add(r),!0):d.has(r)}(t);f.push(e),r?c.push(e):x(y(),e)}(t),t.isEnd()&&function(t){let e=t.getValue().slice(1),r=c.flush();if(b(),r){let e=y();s(r)&&r.setEnd({from:t.getStart(),to:t.getEnd()}),x(e,r)}else if(T(e)){if("function"==typeof n.onError){let e=t.getValue(),r=t.getLine(),s=t.getColumn();n.onError({tagName:e,lineNumber:r,columnNumber:s})}}else x(y(),t.toString({openTag:i,closeTag:a}))}(t)):!function(t){let e=f.last(),r=t.getValue(),s=T(t.toString()),n=y();if(null!==e){if(t.isAttrName()){p.push(r);let t=p.last();t&&e.attr(t,"")}else if(t.isAttrValue()){let t=p.last();t?(e.attr(t,r),p.flush()):e.attr(r,r)}else t.isText()?s?e.append(r):x(n,r):t.isTag()&&x(n,t.toString({openTag:i,closeTag:a}))}else t.isText()?x(n,r):t.isTag()&&x(n,t.toString({openTag:i,closeTag:a}))}(t)},openTag:i,closeTag:a,onlyAllowTags:n.onlyAllowTags,contextFreeTags:n.contextFreeTags,caseFreeTags:n.caseFreeTags,enableEscapeTags:n.enableEscapeTags})).tokenize();let k=c.flush();return null!==k&&k&&s(k)&&T(k.tag)&&A(y(),k,!1),g.toArray()}t.TagNode=h,t.createLexer=z,t.createTokenOfType=m,t.default=U,t.parse=U,Object.defineProperty(t,"__esModule",{value:!0})}); |
@@ -43,2 +43,3 @@ /* eslint-disable no-plusplus,no-param-reassign */ import { OPEN_BRAKET, CLOSE_BRAKET, QUOTEMARK, BACKSLASH, SLASH, SPACE, TAB, EQ, N } from '@bbob/plugin-helper'; | ||
const contextFreeTags = (options.contextFreeTags || []).filter(Boolean).map((tag)=>tag.toLowerCase()); | ||
const caseFreeTags = options.caseFreeTags || false; | ||
const nestedMap = new Map(); | ||
@@ -82,4 +83,2 @@ const onToken = options.onToken || (()=>{}); | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ function emitToken(type, value, startPos, endPos) { | ||
@@ -277,8 +276,8 @@ const token = createTokenOfType(type, value, row, prevCol, startPos, endPos); | ||
} | ||
function isTokenNested(token) { | ||
const value = openTag + SLASH + token.getValue(); | ||
function isTokenNested(tokenValue) { | ||
const value = openTag + SLASH + tokenValue; | ||
if (nestedMap.has(value)) { | ||
return !!nestedMap.get(value); | ||
} else { | ||
const status = buffer.indexOf(value) > -1; | ||
const status = caseFreeTags ? buffer.toLowerCase().indexOf(value.toLowerCase()) > -1 : buffer.indexOf(value) > -1; | ||
nestedMap.set(value, status); | ||
@@ -285,0 +284,0 @@ return status; |
@@ -29,3 +29,3 @@ import { CLOSE_BRAKET, OPEN_BRAKET, TagNode, isTagNode } from "@bbob/plugin-helper"; | ||
const onlyAllowTags = (options.onlyAllowTags || []).filter(Boolean).map((tag)=>tag.toLowerCase()); | ||
const caseSensitiveTags = options.caseFreeTags || false; | ||
const caseFreeTags = options.caseFreeTags || false; | ||
let tokenizer = null; | ||
@@ -55,5 +55,6 @@ /** | ||
function isTokenNested(token) { | ||
const value = token.getValue(); | ||
const tokenValue = token.getValue(); | ||
const value = caseFreeTags ? tokenValue.toLowerCase() : tokenValue; | ||
const { isTokenNested } = tokenizer || {}; | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(token)) { | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(value)) { | ||
nestedTagsMap.add(value); | ||
@@ -67,3 +68,3 @@ return true; | ||
*/ function isTagNested(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
return Boolean(nestedTagsMap.has(caseFreeTags ? tagName.toLowerCase() : tagName)); | ||
} | ||
@@ -153,14 +154,20 @@ /** | ||
*/ function handleTagEnd(token) { | ||
const lastTagNode = nestedNodes.last(); | ||
if (isTagNode(lastTagNode)) { | ||
lastTagNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
const tagName = token.getValue().slice(1); | ||
const lastNestedNode = nestedNodes.flush(); | ||
flushTagNodes(); | ||
const lastNestedNode = nestedNodes.flush(); | ||
if (lastNestedNode) { | ||
const nodes = getNodes(); | ||
if (isTagNode(lastNestedNode)) { | ||
lastNestedNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
appendNodes(nodes, lastNestedNode); | ||
} else if (!isTagNested(tagName)) { | ||
const nodes = getNodes(); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} else if (typeof options.onError === "function") { | ||
@@ -223,3 +230,6 @@ const tag = token.getValue(); | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
@@ -230,3 +240,6 @@ } else if (token.isText()) { | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
@@ -251,2 +264,3 @@ } | ||
contextFreeTags: options.contextFreeTags, | ||
caseFreeTags: options.caseFreeTags, | ||
enableEscapeTags: options.enableEscapeTags | ||
@@ -253,0 +267,0 @@ }); |
@@ -55,6 +55,6 @@ import { OPEN_BRAKET, CLOSE_BRAKET, SLASH } from '@bbob/plugin-helper'; | ||
}; | ||
const tokenToText = (token)=>{ | ||
let text = OPEN_BRAKET; | ||
const tokenToText = (token, openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET)=>{ | ||
let text = openTag; | ||
text += getTokenValue(token); | ||
text += CLOSE_BRAKET; | ||
text += closeTag; | ||
return text; | ||
@@ -108,4 +108,4 @@ }; | ||
} | ||
toString() { | ||
return tokenToText(this); | ||
toString({ openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET } = {}) { | ||
return tokenToText(this, openTag, closeTag); | ||
} | ||
@@ -112,0 +112,0 @@ constructor(type, value, row = 0, col = 0, start = 0, end = 0){ |
@@ -78,2 +78,3 @@ /* eslint-disable no-plusplus,no-param-reassign */ "use strict"; | ||
}); | ||
var caseFreeTags = options.caseFreeTags || false; | ||
var nestedMap = new Map(); | ||
@@ -123,4 +124,2 @@ var onToken = options.onToken || function() {}; | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ function emitToken(type, value, startPos, endPos) { | ||
@@ -328,8 +327,8 @@ var token = createTokenOfType(type, value, row, prevCol, startPos, endPos); | ||
} | ||
function isTokenNested(token) { | ||
var value = openTag + _pluginhelper.SLASH + token.getValue(); | ||
function isTokenNested(tokenValue) { | ||
var value = openTag + _pluginhelper.SLASH + tokenValue; | ||
if (nestedMap.has(value)) { | ||
return !!nestedMap.get(value); | ||
} else { | ||
var status = buffer.indexOf(value) > -1; | ||
var status = caseFreeTags ? buffer.toLowerCase().indexOf(value.toLowerCase()) > -1 : buffer.indexOf(value) > -1; | ||
nestedMap.set(value, status); | ||
@@ -336,0 +335,0 @@ return status; |
@@ -55,3 +55,3 @@ "use strict"; | ||
}); | ||
var caseSensitiveTags = options.caseFreeTags || false; | ||
var caseFreeTags = options.caseFreeTags || false; | ||
var tokenizer = null; | ||
@@ -81,5 +81,6 @@ /** | ||
function isTokenNested(token) { | ||
var value = token.getValue(); | ||
var tokenValue = token.getValue(); | ||
var value = caseFreeTags ? tokenValue.toLowerCase() : tokenValue; | ||
var isTokenNested = (tokenizer || {}).isTokenNested; | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(token)) { | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(value)) { | ||
nestedTagsMap.add(value); | ||
@@ -93,3 +94,3 @@ return true; | ||
*/ function isTagNested(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
return Boolean(nestedTagsMap.has(caseFreeTags ? tagName.toLowerCase() : tagName)); | ||
} | ||
@@ -180,14 +181,20 @@ /** | ||
*/ function handleTagEnd(token) { | ||
var lastTagNode = nestedNodes.last(); | ||
if ((0, _pluginhelper.isTagNode)(lastTagNode)) { | ||
lastTagNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
var tagName = token.getValue().slice(1); | ||
var lastNestedNode = nestedNodes.flush(); | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flush(); | ||
if (lastNestedNode) { | ||
var nodes = getNodes(); | ||
if ((0, _pluginhelper.isTagNode)(lastNestedNode)) { | ||
lastNestedNode.setEnd({ | ||
from: token.getStart(), | ||
to: token.getEnd() | ||
}); | ||
} | ||
appendNodes(nodes, lastNestedNode); | ||
} else if (!isTagNested(tagName)) { | ||
var nodes1 = getNodes(); | ||
appendNodes(nodes1, token.toString({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} else if (typeof options.onError === "function") { | ||
@@ -250,3 +257,6 @@ var tag = token.getValue(); | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
@@ -257,3 +267,6 @@ } else if (token.isText()) { | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
@@ -278,2 +291,3 @@ } | ||
contextFreeTags: options.contextFreeTags, | ||
caseFreeTags: options.caseFreeTags, | ||
enableEscapeTags: options.enableEscapeTags | ||
@@ -280,0 +294,0 @@ }); |
@@ -135,6 +135,8 @@ "use strict"; | ||
}; | ||
var tokenToText = function(token) { | ||
var text = _pluginhelper.OPEN_BRAKET; | ||
var tokenToText = function(token, openTag, closeTag) { | ||
if (openTag === void 0) openTag = _pluginhelper.OPEN_BRAKET; | ||
if (closeTag === void 0) closeTag = _pluginhelper.CLOSE_BRAKET; | ||
var text = openTag; | ||
text += getTokenValue(token); | ||
text += _pluginhelper.CLOSE_BRAKET; | ||
text += closeTag; | ||
return text; | ||
@@ -199,4 +201,5 @@ }; | ||
}; | ||
_proto.toString = function toString() { | ||
return tokenToText(this); | ||
_proto.toString = function toString(param) { | ||
var _ref = param === void 0 ? {} : param, _ref_openTag = _ref.openTag, openTag = _ref_openTag === void 0 ? _pluginhelper.OPEN_BRAKET : _ref_openTag, _ref_closeTag = _ref.closeTag, closeTag = _ref_closeTag === void 0 ? _pluginhelper.CLOSE_BRAKET : _ref_closeTag; | ||
return tokenToText(this, openTag, closeTag); | ||
}; | ||
@@ -203,0 +206,0 @@ _create_class(Token, [ |
{ | ||
"name": "@bbob/parser", | ||
"version": "0.0.0-alpha.cad4675", | ||
"version": "0.0.0-alpha.ede7040", | ||
"description": "A BBCode to AST Parser part of @bbob", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -54,3 +54,3 @@ /* eslint-disable no-plusplus,no-param-reassign */ | ||
let contextFreeTag = ''; | ||
const tokens = new Array<Token<string>>(Math.floor(buffer.length)); | ||
const tokens = new Array<Token>(Math.floor(buffer.length)); | ||
const openTag = options.openTag || OPEN_BRAKET; | ||
@@ -62,2 +62,3 @@ const closeTag = options.closeTag || CLOSE_BRAKET; | ||
.map((tag) => tag.toLowerCase()); | ||
const caseFreeTags = options.caseFreeTags || false; | ||
const nestedMap = new Map<string, boolean>(); | ||
@@ -93,4 +94,2 @@ const onToken = options.onToken || (() => { | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ | ||
@@ -358,4 +357,4 @@ function emitToken(type: number, value: string, startPos?: number, endPos?: number) { | ||
function isTokenNested(token: Token) { | ||
const value = openTag + SLASH + token.getValue(); | ||
function isTokenNested(tokenValue: string) { | ||
const value = openTag + SLASH + tokenValue; | ||
@@ -365,3 +364,3 @@ if (nestedMap.has(value)) { | ||
} else { | ||
const status = (buffer.indexOf(value) > -1); | ||
const status = caseFreeTags ? (buffer.toLowerCase().indexOf(value.toLowerCase()) > -1) : (buffer.indexOf(value) > -1); | ||
@@ -368,0 +367,0 @@ nestedMap.set(value, status); |
@@ -55,5 +55,5 @@ import type { NodeContent, TagNodeTree, LexerTokenizer, ParseOptions } from "@bbob/types"; | ||
.map((tag) => tag.toLowerCase()); | ||
const caseSensitiveTags = options.caseFreeTags || false; | ||
const caseFreeTags = options.caseFreeTags || false; | ||
let tokenizer: LexerTokenizer | null = null; | ||
let tokenizer: ReturnType<typeof createLexer> | null = null; | ||
@@ -90,6 +90,7 @@ /** | ||
function isTokenNested(token: Token) { | ||
const value = token.getValue(); | ||
const tokenValue = token.getValue(); | ||
const value = caseFreeTags ? tokenValue.toLowerCase() : tokenValue; | ||
const { isTokenNested } = tokenizer || {}; | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(token)) { | ||
if (!nestedTagsMap.has(value) && isTokenNested && isTokenNested(value)) { | ||
nestedTagsMap.add(value); | ||
@@ -107,3 +108,3 @@ | ||
function isTagNested(tagName: string) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
return Boolean(nestedTagsMap.has(caseFreeTags ? tagName.toLowerCase() : tagName)); | ||
} | ||
@@ -210,13 +211,19 @@ | ||
function handleTagEnd(token: Token) { | ||
const lastTagNode = nestedNodes.last(); | ||
if (isTagNode(lastTagNode)) { | ||
lastTagNode.setEnd({ from: token.getStart(), to: token.getEnd() }); | ||
} | ||
const tagName = token.getValue().slice(1); | ||
const lastNestedNode = nestedNodes.flush(); | ||
flushTagNodes(); | ||
const lastNestedNode = nestedNodes.flush(); | ||
if (lastNestedNode) { | ||
const nodes = getNodes(); | ||
if (isTagNode(lastNestedNode)) { | ||
lastNestedNode.setEnd({ from: token.getStart(), to: token.getEnd() }); | ||
} | ||
appendNodes(nodes, lastNestedNode); | ||
} else if (!isTagNested(tagName)) { // when we have only close tag [/some] without any open tag | ||
const nodes = getNodes(); | ||
appendNodes(nodes, token.toString({ openTag, closeTag })); | ||
} else if (typeof options.onError === "function") { | ||
@@ -289,3 +296,3 @@ const tag = token.getValue(); | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ openTag, closeTag })); | ||
} | ||
@@ -296,3 +303,3 @@ } else if (token.isText()) { | ||
// if tag is not allowed, just pass it as is | ||
appendNodes(nodes, token.toString()); | ||
appendNodes(nodes, token.toString({ openTag, closeTag })); | ||
} | ||
@@ -321,2 +328,3 @@ } | ||
contextFreeTags: options.contextFreeTags, | ||
caseFreeTags: options.caseFreeTags, | ||
enableEscapeTags: options.enableEscapeTags, | ||
@@ -323,0 +331,0 @@ }); |
@@ -84,7 +84,7 @@ import { | ||
const tokenToText = (token: Token) => { | ||
let text = OPEN_BRAKET; | ||
const tokenToText = (token: Token, openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET) => { | ||
let text = openTag; | ||
text += getTokenValue(token); | ||
text += CLOSE_BRAKET; | ||
text += closeTag; | ||
@@ -171,4 +171,4 @@ return text; | ||
toString() { | ||
return tokenToText(this); | ||
toString({ openTag = OPEN_BRAKET, closeTag = CLOSE_BRAKET } = {}) { | ||
return tokenToText(this, openTag, closeTag); | ||
} | ||
@@ -175,0 +175,0 @@ } |
@@ -28,3 +28,6 @@ import type { Token as TokenInterface } from "@bbob/types"; | ||
getEnd(): number; | ||
toString(): string; | ||
toString({ openTag, closeTag }?: { | ||
openTag?: string | undefined; | ||
closeTag?: string | undefined; | ||
}): string; | ||
} | ||
@@ -31,0 +34,0 @@ export declare const TYPE_ID = "t"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184708
3740