@bbob/parser
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -546,3 +546,3 @@ (function (global, factory) { | ||
* @public | ||
* @param {String} input | ||
* @param {string} input | ||
* @param {Object} opts | ||
@@ -553,5 +553,5 @@ * @param {Function} opts.createTokenizer | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @return {Array} | ||
* @param {string} opts.openTag | ||
* @param {string} opts.closeTag | ||
* @return {Array<string|TagNode>} | ||
*/ const parse = (input, opts = {})=>{ | ||
@@ -584,6 +584,6 @@ const options = opts; | ||
* Cache for nested tags checks | ||
* @type Set<string> | ||
*/ const nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @param {Token} token | ||
* @returns {boolean} | ||
@@ -599,3 +599,4 @@ */ const isTokenNested = (token)=>{ | ||
/** | ||
* @param tagName | ||
* @private | ||
* @param {string} tagName | ||
* @returns {boolean} | ||
@@ -605,3 +606,3 @@ */ const isTagNested = (tagName)=>Boolean(nestedTagsMap.has(tagName)); | ||
* @private | ||
* @param {String} value | ||
* @param {string} value | ||
* @return {boolean} | ||
@@ -636,2 +637,26 @@ */ const isAllowedTag = (value)=>{ | ||
* @param {string|TagNode} node | ||
* @param {boolean} isNested | ||
*/ const appendNodeAsString = (node, isNested = true)=>{ | ||
const items = getNodes(); | ||
if (Array.isArray(items)) { | ||
items.push(node.toTagStart({ | ||
openTag, | ||
closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach((item)=>{ | ||
items.push(item); | ||
}); | ||
if (isNested) { | ||
items.push(node.toTagEnd({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ const appendNodes = (node)=>{ | ||
@@ -644,15 +669,3 @@ const items = getNodes(); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag, | ||
closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach((item)=>{ | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
appendNodeAsString(node); | ||
} | ||
@@ -769,2 +782,9 @@ } else { | ||
tokenizer.tokenize(); | ||
// handles situations where we open tag, but forgot close them | ||
// for ex [q]test[/q][u]some[/u][q]some [u]some[/u] // forgot to close [/q] | ||
// so we need to flush nested content to nodes array | ||
const lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode && isTagNested(lastNestedNode.tag)) { | ||
appendNodeAsString(lastNestedNode, false); | ||
} | ||
return nodes.toArray(); | ||
@@ -771,0 +791,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@bbob/plugin-helper")):"function"==typeof define&&define.amd?define(["exports","@bbob/plugin-helper"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).BbobParser={},e.pluginHelper)}(this,function(e,t){"use strict";let r="type",n="value",s="line",i=e=>e&&void 0!==e[n]?e[n]:"",l=e=>e&&e[s]||0,u=e=>e&&e.row||0,a=e=>!!e&&void 0!==e[r]&&(5===e[r]||6===e[r]||1===e[r]),o=e=>!!e&&void 0!==e[r]&&2===e[r],g=e=>i(e).charCodeAt(0)===t.SLASH.charCodeAt(0),h=e=>!g(e),f=e=>!!e&&void 0!==e[r]&&3===e[r],p=e=>!!e&&void 0!==e[r]&&4===e[r],A=e=>{let t=i(e);return g(e)?t.slice(1):t},c=e=>(t.OPEN_BRAKET,i(e)+t.CLOSE_BRAKET);class d{isEmpty(){return isNaN(this[r])}isText(){return a(this)}isTag(){return o(this)}isAttrName(){return f(this)}isAttrValue(){return p(this)}isStart(){return h(this)}isEnd(){return g(this)}getName(){return A(this)}getValue(){return i(this)}getLine(){return l(this)}getColumn(){return u(this)}toString(){return c(this)}constructor(e,t,i,l){this[r]=Number(e),this[n]=String(t),this[s]=Number(i),this.row=Number(l)}}function T(e,t){let r={pos:0,len:e.length},n=t=>{let{pos:n}=r,s=e.indexOf(t,n);return s>=0?e.substring(n,s):""},s=t=>e.indexOf(t,r.pos)>=0,i=()=>r.len>r.pos,l=(e=1,n)=>{r.pos+=e,t&&t.onSkip&&!n&&t.onSkip()},u=()=>e.substring(r.pos),a=(t=0)=>e.substring(r.pos,r.pos+t),o=()=>e[r.pos],g=()=>{let t=r.pos-1;return void 0!==e[t]?e[t]:null},h=()=>{let t=r.pos+1;return t<=e.length-1?e[t]:null},f=(t,n)=>{let s=0;if(i())for(s=r.pos;i()&&t(o());)l(1,n);return e.substring(s,r.pos)};this.skip=l,this.hasNext=i,this.getCurr=o,this.getRest=u,this.getNext=h,this.getPrev=g,this.isLast=()=>r.pos===r.len,this.includes=s,this.grabWhile=f,this.grabN=a,this.substrUntilChar=n}let b=(e,t)=>new T(e,t),E=(e,t)=>{for(;e.charAt(0)===t;)e=e.substring(1);for(;e.charAt(e.length-1)===t;)e=e.substring(0,e.length-1);return e},S=e=>e.replace(t.BACKSLASH+t.QUOTEMARK,t.QUOTEMARK);function N(e=[]){let t=()=>Array.isArray(e)&&e.length>0&&void 0!==e[e.length-1]?e[e.length-1]:null,r=()=>!!e.length&&e.pop(),n=t=>e.push(t);this.push=n,this.toArray=()=>e,this.getLast=t,this.flushLast=r}let L=(e=[])=>new N(e),C=(e,t,r=0,n=0)=>new d(e,t,r,n);function x(e,r={}){let n=0,s=0,i=-1,l=0,u=0,a="",o=Array(Math.floor(e.length)),g=r.openTag||t.OPEN_BRAKET,h=r.closeTag||t.CLOSE_BRAKET,f=!!r.enableEscapeTags,p=r.contextFreeTags||[],A=r.onToken||(()=>{}),c=[h,g,t.QUOTEMARK,t.BACKSLASH,t.SPACE,t.TAB,t.EQ,t.N,"!"],d=[g,t.SPACE,t.TAB,t.N],T=[t.SPACE,t.TAB],N=[t.EQ,t.SPACE,t.TAB],L=e=>c.indexOf(e)>=0,x=e=>e===t.N,y=e=>T.indexOf(e)>=0,k=e=>-1===d.indexOf(e),O=e=>N.indexOf(e)>=0,m=e=>e===g||e===h||e===t.BACKSLASH,B=e=>e===t.BACKSLASH,K=()=>{s++},P=e=>S(E(e,t.QUOTEMARK)),Q=(e,t)=>{""!==a&&t&&(a=""),""===a&&p.includes(e)&&(a=e)},w=b(e,{onSkip:K});function R(e,t){let r=C(e,t,n,s);A(r),o[i+=1]=r}return{tokenize:function(){for(l=0;w.hasNext();)switch(l){case 1:l=function(){let e=w.getCurr(),r=w.getNext();w.skip();let n=w.substrUntilChar(h),s=0===n.length||n.indexOf(g)>=0;if(L(r)||s||w.isLast())return R(1,e),0;let i=-1===n.indexOf(t.EQ),l=n[0]===t.SLASH;if(i||l){let u=w.grabWhile(e=>e!==h);return w.skip(),R(2,u),Q(u,l),0}return 2}();break;case 2:l=function(){let e=w.grabWhile(e=>e!==h,!0),r=b(e,{onSkip:K}),n=r.includes(t.SPACE);for(u=0;r.hasNext();)u=function(e,r){if(1===u){let n=e=>!(e===t.EQ||y(e)),s=e.grabWhile(n),i=e.isLast(),l=e.getCurr()!==t.EQ;return(e.skip(),i||l?R(4,P(s)):R(3,s),i)?0:l?1:2}if(2===u){let a=!1,o=n=>{let s=n===t.QUOTEMARK,i=e.getPrev(),l=e.getNext(),u=i===t.BACKSLASH,o=l===t.EQ,g=y(n),h=y(l);return!!(a&&O(n))||(!s||!!u||!!(a=!a)||!!o||!!h)&&(!!r||!1===g)},g=e.grabWhile(o);return(e.skip(),R(4,P(g)),e.isLast())?0:1}let h=r=>!(r===t.EQ||y(r)||e.isLast()),f=e.grabWhile(h);if(R(2,f),Q(f),e.skip(),r)return 2;let p=e.includes(t.EQ);return p?1:2}(r,!n);return w.skip(),0}();break;default:l=function(){if(x(w.getCurr()))return R(6,w.getCurr()),w.skip(),s=0,n++,0;if(y(w.getCurr())){let e=w.grabWhile(y);return R(5,e),0}if(w.getCurr()===g){if(a){let r=g.length+t.SLASH.length+a.length,i=`${g}${t.SLASH}${a}`,l=w.grabN(r);if(l===i)return 1}else if(w.includes(h))return 1;return R(1,w.getCurr()),w.skip(),0}if(f){if(B(w.getCurr())){let u=w.getCurr(),o=w.getNext();return(w.skip(),m(o))?(w.skip(),R(1,o),0):(R(1,u),0)}let p=e=>k(e)&&!B(e),A=w.grabWhile(p);return R(1,A),0}let c=w.grabWhile(k);return R(1,c),0}()}return o.length=i+1,o},isTokenNested:function(r){let n=g+t.SLASH+r.getValue();return e.indexOf(n)>-1}}}let y=(e,r={})=>{let n=r.openTag||t.OPEN_BRAKET,s=r.closeTag||t.CLOSE_BRAKET,i=null,l=L(),u=L(),a=L(),o=L(),g=new Set,h=e=>{let t=e.getValue();return!g.has(t)&&i.isTokenNested&&i.isTokenNested(e)?(g.add(t),!0):g.has(t)},f=e=>Boolean(g.has(e)),p=e=>!r.onlyAllowTags||!r.onlyAllowTags.length||r.onlyAllowTags.indexOf(e)>=0,A=()=>{a.flushLast()&&o.flushLast()},c=()=>{let e=u.getLast();return e&&Array.isArray(e.content)?e.content:l.toArray()},d=e=>{let r=c();Array.isArray(r)&&(t.isTagNode(e)?p(e.tag)?r.push(e.toTagNode()):(r.push(e.toTagStart({openTag:n,closeTag:s})),e.content.length&&(e.content.forEach(e=>{r.push(e)}),r.push(e.toTagEnd({openTag:n,closeTag:s})))):r.push(e))},T=e=>{A();let r=t.TagNode.create(e.getValue()),n=h(e);a.push(r),n?u.push(r):d(r)},b=e=>{A();let t=u.flushLast();if(t)d(t);else if("function"==typeof r.onError){let n=e.getValue(),s=e.getLine(),i=e.getColumn();r.onError({message:`Inconsistent tag '${n}' on line ${s} and column ${i}`,tagName:n,lineNumber:s,columnNumber:i})}},E=e=>{e.isStart()&&T(e),e.isEnd()&&b(e)},S=e=>{let t=a.getLast(),r=e.getValue(),n=f(e);if(t){if(e.isAttrName())o.push(r),t.attr(o.getLast(),"");else if(e.isAttrValue()){let s=o.getLast();s?(t.attr(s,r),o.flushLast()):t.attr(r,r)}else e.isText()?n?t.append(r):d(r):e.isTag()&&d(e.toString())}else e.isText()?d(r):e.isTag()&&d(e.toString())},N=e=>{e.isTag()?E(e):S(e)};return(i=(r.createTokenizer?r.createTokenizer:x)(e,{onToken:N,openTag:n,closeTag:s,onlyAllowTags:r.onlyAllowTags,contextFreeTags:r.contextFreeTags,enableEscapeTags:r.enableEscapeTags})).tokenize(),l.toArray()};Object.defineProperty(e,"TagNode",{enumerable:!0,get:function(){return t.TagNode}}),e.default=y,e.parse=y,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@bbob/plugin-helper")):"function"==typeof define&&define.amd?define(["exports","@bbob/plugin-helper"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).BbobParser={},e.pluginHelper)}(this,function(e,t){"use strict";let r="type",n="value",s="line",i=e=>e&&void 0!==e[n]?e[n]:"",l=e=>e&&e[s]||0,a=e=>e&&e.row||0,u=e=>!!e&&void 0!==e[r]&&(5===e[r]||6===e[r]||1===e[r]),o=e=>!!e&&void 0!==e[r]&&2===e[r],g=e=>i(e).charCodeAt(0)===t.SLASH.charCodeAt(0),h=e=>!g(e),f=e=>!!e&&void 0!==e[r]&&3===e[r],p=e=>!!e&&void 0!==e[r]&&4===e[r],A=e=>{let t=i(e);return g(e)?t.slice(1):t},c=e=>(t.OPEN_BRAKET,i(e)+t.CLOSE_BRAKET);class d{isEmpty(){return isNaN(this[r])}isText(){return u(this)}isTag(){return o(this)}isAttrName(){return f(this)}isAttrValue(){return p(this)}isStart(){return h(this)}isEnd(){return g(this)}getName(){return A(this)}getValue(){return i(this)}getLine(){return l(this)}getColumn(){return a(this)}toString(){return c(this)}constructor(e,t,i,l){this[r]=Number(e),this[n]=String(t),this[s]=Number(i),this.row=Number(l)}}function T(e,t){let r={pos:0,len:e.length},n=t=>{let{pos:n}=r,s=e.indexOf(t,n);return s>=0?e.substring(n,s):""},s=t=>e.indexOf(t,r.pos)>=0,i=()=>r.len>r.pos,l=(e=1,n)=>{r.pos+=e,t&&t.onSkip&&!n&&t.onSkip()},a=()=>e.substring(r.pos),u=(t=0)=>e.substring(r.pos,r.pos+t),o=()=>e[r.pos],g=()=>{let t=r.pos-1;return void 0!==e[t]?e[t]:null},h=()=>{let t=r.pos+1;return t<=e.length-1?e[t]:null},f=(t,n)=>{let s=0;if(i())for(s=r.pos;i()&&t(o());)l(1,n);return e.substring(s,r.pos)};this.skip=l,this.hasNext=i,this.getCurr=o,this.getRest=a,this.getNext=h,this.getPrev=g,this.isLast=()=>r.pos===r.len,this.includes=s,this.grabWhile=f,this.grabN=u,this.substrUntilChar=n}let b=(e,t)=>new T(e,t),E=(e,t)=>{for(;e.charAt(0)===t;)e=e.substring(1);for(;e.charAt(e.length-1)===t;)e=e.substring(0,e.length-1);return e},S=e=>e.replace(t.BACKSLASH+t.QUOTEMARK,t.QUOTEMARK);function N(e=[]){let t=()=>Array.isArray(e)&&e.length>0&&void 0!==e[e.length-1]?e[e.length-1]:null,r=()=>!!e.length&&e.pop(),n=t=>e.push(t);this.push=n,this.toArray=()=>e,this.getLast=t,this.flushLast=r}let L=(e=[])=>new N(e),C=(e,t,r=0,n=0)=>new d(e,t,r,n);function x(e,r={}){let n=0,s=0,i=-1,l=0,a=0,u="",o=Array(Math.floor(e.length)),g=r.openTag||t.OPEN_BRAKET,h=r.closeTag||t.CLOSE_BRAKET,f=!!r.enableEscapeTags,p=r.contextFreeTags||[],A=r.onToken||(()=>{}),c=[h,g,t.QUOTEMARK,t.BACKSLASH,t.SPACE,t.TAB,t.EQ,t.N,"!"],d=[g,t.SPACE,t.TAB,t.N],T=[t.SPACE,t.TAB],N=[t.EQ,t.SPACE,t.TAB],L=e=>c.indexOf(e)>=0,x=e=>e===t.N,y=e=>T.indexOf(e)>=0,k=e=>-1===d.indexOf(e),O=e=>N.indexOf(e)>=0,m=e=>e===g||e===h||e===t.BACKSLASH,B=e=>e===t.BACKSLASH,K=()=>{s++},P=e=>S(E(e,t.QUOTEMARK)),Q=(e,t)=>{""!==u&&t&&(u=""),""===u&&p.includes(e)&&(u=e)},w=b(e,{onSkip:K});function R(e,t){let r=C(e,t,n,s);A(r),o[i+=1]=r}return{tokenize:function(){for(l=0;w.hasNext();)switch(l){case 1:l=function(){let e=w.getCurr(),r=w.getNext();w.skip();let n=w.substrUntilChar(h),s=0===n.length||n.indexOf(g)>=0;if(L(r)||s||w.isLast())return R(1,e),0;let i=-1===n.indexOf(t.EQ),l=n[0]===t.SLASH;if(i||l){let a=w.grabWhile(e=>e!==h);return w.skip(),R(2,a),Q(a,l),0}return 2}();break;case 2:l=function(){let e=w.grabWhile(e=>e!==h,!0),r=b(e,{onSkip:K}),n=r.includes(t.SPACE);for(a=0;r.hasNext();)a=function(e,r){if(1===a){let n=e=>!(e===t.EQ||y(e)),s=e.grabWhile(n),i=e.isLast(),l=e.getCurr()!==t.EQ;return(e.skip(),i||l?R(4,P(s)):R(3,s),i)?0:l?1:2}if(2===a){let u=!1,o=n=>{let s=n===t.QUOTEMARK,i=e.getPrev(),l=e.getNext(),a=i===t.BACKSLASH,o=l===t.EQ,g=y(n),h=y(l);return!!(u&&O(n))||(!s||!!a||!!(u=!u)||!!o||!!h)&&(!!r||!1===g)},g=e.grabWhile(o);return(e.skip(),R(4,P(g)),e.isLast())?0:1}let h=r=>!(r===t.EQ||y(r)||e.isLast()),f=e.grabWhile(h);if(R(2,f),Q(f),e.skip(),r)return 2;let p=e.includes(t.EQ);return p?1:2}(r,!n);return w.skip(),0}();break;default:l=function(){if(x(w.getCurr()))return R(6,w.getCurr()),w.skip(),s=0,n++,0;if(y(w.getCurr())){let e=w.grabWhile(y);return R(5,e),0}if(w.getCurr()===g){if(u){let r=g.length+t.SLASH.length+u.length,i=`${g}${t.SLASH}${u}`,l=w.grabN(r);if(l===i)return 1}else if(w.includes(h))return 1;return R(1,w.getCurr()),w.skip(),0}if(f){if(B(w.getCurr())){let a=w.getCurr(),o=w.getNext();return(w.skip(),m(o))?(w.skip(),R(1,o),0):(R(1,a),0)}let p=e=>k(e)&&!B(e),A=w.grabWhile(p);return R(1,A),0}let c=w.grabWhile(k);return R(1,c),0}()}return o.length=i+1,o},isTokenNested:function(r){let n=g+t.SLASH+r.getValue();return e.indexOf(n)>-1}}}let y=(e,r={})=>{let n=r.openTag||t.OPEN_BRAKET,s=r.closeTag||t.CLOSE_BRAKET,i=null,l=L(),a=L(),u=L(),o=L(),g=new Set,h=e=>{let t=e.getValue();return!g.has(t)&&i.isTokenNested&&i.isTokenNested(e)?(g.add(t),!0):g.has(t)},f=e=>Boolean(g.has(e)),p=e=>!r.onlyAllowTags||!r.onlyAllowTags.length||r.onlyAllowTags.indexOf(e)>=0,A=()=>{u.flushLast()&&o.flushLast()},c=()=>{let e=a.getLast();return e&&Array.isArray(e.content)?e.content:l.toArray()},d=(e,t=!0)=>{let r=c();Array.isArray(r)&&(r.push(e.toTagStart({openTag:n,closeTag:s})),e.content.length&&(e.content.forEach(e=>{r.push(e)}),t&&r.push(e.toTagEnd({openTag:n,closeTag:s}))))},T=e=>{let r=c();Array.isArray(r)&&(t.isTagNode(e)?p(e.tag)?r.push(e.toTagNode()):d(e):r.push(e))},b=e=>{A();let r=t.TagNode.create(e.getValue()),n=h(e);u.push(r),n?a.push(r):T(r)},E=e=>{A();let t=a.flushLast();if(t)T(t);else if("function"==typeof r.onError){let n=e.getValue(),s=e.getLine(),i=e.getColumn();r.onError({message:`Inconsistent tag '${n}' on line ${s} and column ${i}`,tagName:n,lineNumber:s,columnNumber:i})}},S=e=>{e.isStart()&&b(e),e.isEnd()&&E(e)},N=e=>{let t=u.getLast(),r=e.getValue(),n=f(e);if(t){if(e.isAttrName())o.push(r),t.attr(o.getLast(),"");else if(e.isAttrValue()){let s=o.getLast();s?(t.attr(s,r),o.flushLast()):t.attr(r,r)}else e.isText()?n?t.append(r):T(r):e.isTag()&&T(e.toString())}else e.isText()?T(r):e.isTag()&&T(e.toString())},C=e=>{e.isTag()?S(e):N(e)};(i=(r.createTokenizer?r.createTokenizer:x)(e,{onToken:C,openTag:n,closeTag:s,onlyAllowTags:r.onlyAllowTags,contextFreeTags:r.contextFreeTags,enableEscapeTags:r.enableEscapeTags})).tokenize();let y=a.flushLast();return y&&f(y.tag)&&d(y,!1),l.toArray()};Object.defineProperty(e,"TagNode",{enumerable:!0,get:function(){return t.TagNode}}),e.default=y,e.parse=y,Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -6,3 +6,3 @@ import { TagNode, CLOSE_BRAKET, OPEN_BRAKET, isTagNode } from '@bbob/plugin-helper'; | ||
* @public | ||
* @param {String} input | ||
* @param {string} input | ||
* @param {Object} opts | ||
@@ -13,5 +13,5 @@ * @param {Function} opts.createTokenizer | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @return {Array} | ||
* @param {string} opts.openTag | ||
* @param {string} opts.closeTag | ||
* @return {Array<string|TagNode>} | ||
*/ const parse = (input, opts = {})=>{ | ||
@@ -44,6 +44,6 @@ const options = opts; | ||
* Cache for nested tags checks | ||
* @type Set<string> | ||
*/ const nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @param {Token} token | ||
* @returns {boolean} | ||
@@ -59,3 +59,4 @@ */ const isTokenNested = (token)=>{ | ||
/** | ||
* @param tagName | ||
* @private | ||
* @param {string} tagName | ||
* @returns {boolean} | ||
@@ -65,3 +66,3 @@ */ const isTagNested = (tagName)=>Boolean(nestedTagsMap.has(tagName)); | ||
* @private | ||
* @param {String} value | ||
* @param {string} value | ||
* @return {boolean} | ||
@@ -96,2 +97,26 @@ */ const isAllowedTag = (value)=>{ | ||
* @param {string|TagNode} node | ||
* @param {boolean} isNested | ||
*/ const appendNodeAsString = (node, isNested = true)=>{ | ||
const items = getNodes(); | ||
if (Array.isArray(items)) { | ||
items.push(node.toTagStart({ | ||
openTag, | ||
closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach((item)=>{ | ||
items.push(item); | ||
}); | ||
if (isNested) { | ||
items.push(node.toTagEnd({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ const appendNodes = (node)=>{ | ||
@@ -104,15 +129,3 @@ const items = getNodes(); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag, | ||
closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach((item)=>{ | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
appendNodeAsString(node); | ||
} | ||
@@ -229,2 +242,9 @@ } else { | ||
const tokens = tokenizer.tokenize(); | ||
// handles situations where we open tag, but forgot close them | ||
// for ex [q]test[/q][u]some[/u][q]some [u]some[/u] // forgot to close [/q] | ||
// so we need to flush nested content to nodes array | ||
const lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode && isTagNested(lastNestedNode.tag)) { | ||
appendNodeAsString(lastNestedNode, false); | ||
} | ||
return nodes.toArray(); | ||
@@ -231,0 +251,0 @@ }; |
@@ -24,3 +24,3 @@ "use strict"; | ||
* @public | ||
* @param {String} input | ||
* @param {string} input | ||
* @param {Object} opts | ||
@@ -31,5 +31,5 @@ * @param {Function} opts.createTokenizer | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @return {Array} | ||
* @param {string} opts.openTag | ||
* @param {string} opts.closeTag | ||
* @return {Array<string|TagNode>} | ||
*/ var parse = function(input, opts) { | ||
@@ -63,6 +63,6 @@ if (opts === void 0) opts = {}; | ||
* Cache for nested tags checks | ||
* @type Set<string> | ||
*/ var nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @param {Token} token | ||
* @returns {boolean} | ||
@@ -78,3 +78,4 @@ */ var isTokenNested = function(token) { | ||
/** | ||
* @param tagName | ||
* @private | ||
* @param {string} tagName | ||
* @returns {boolean} | ||
@@ -86,3 +87,3 @@ */ var isTagNested = function(tagName) { | ||
* @private | ||
* @param {String} value | ||
* @param {string} value | ||
* @return {boolean} | ||
@@ -117,2 +118,27 @@ */ var isAllowedTag = function(value) { | ||
* @param {string|TagNode} node | ||
* @param {boolean} isNested | ||
*/ var appendNodeAsString = function(node, isNested) { | ||
if (isNested === void 0) isNested = true; | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function(item) { | ||
items.push(item); | ||
}); | ||
if (isNested) { | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ var appendNodes = function(node) { | ||
@@ -125,15 +151,3 @@ var items = getNodes(); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function(item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
appendNodeAsString(node); | ||
} | ||
@@ -250,4 +264,11 @@ } else { | ||
var tokens = tokenizer.tokenize(); | ||
// handles situations where we open tag, but forgot close them | ||
// for ex [q]test[/q][u]some[/u][q]some [u]some[/u] // forgot to close [/q] | ||
// so we need to flush nested content to nodes array | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode && isTagNested(lastNestedNode.tag)) { | ||
appendNodeAsString(lastNestedNode, false); | ||
} | ||
return nodes.toArray(); | ||
}; | ||
var _default = parse; |
{ | ||
"name": "@bbob/parser", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "A BBCode to AST Parser part of @bbob", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -9,3 +9,3 @@ import { | ||
* @public | ||
* @param {String} input | ||
* @param {string} input | ||
* @param {Object} opts | ||
@@ -16,5 +16,5 @@ * @param {Function} opts.createTokenizer | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @return {Array} | ||
* @param {string} opts.openTag | ||
* @param {string} opts.closeTag | ||
* @return {Array<string|TagNode>} | ||
*/ | ||
@@ -55,2 +55,3 @@ const parse = (input, opts = {}) => { | ||
* Cache for nested tags checks | ||
* @type Set<string> | ||
*/ | ||
@@ -60,4 +61,3 @@ const nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @param {Token} token | ||
* @returns {boolean} | ||
@@ -78,3 +78,4 @@ */ | ||
/** | ||
* @param tagName | ||
* @private | ||
* @param {string} tagName | ||
* @returns {boolean} | ||
@@ -86,3 +87,3 @@ */ | ||
* @private | ||
* @param {String} value | ||
* @param {string} value | ||
* @return {boolean} | ||
@@ -126,3 +127,26 @@ */ | ||
* @param {string|TagNode} node | ||
* @param {boolean} isNested | ||
*/ | ||
const appendNodeAsString = (node, isNested = true) => { | ||
const items = getNodes(); | ||
if (Array.isArray(items)) { | ||
items.push(node.toTagStart({ openTag, closeTag })); | ||
if (node.content.length) { | ||
node.content.forEach((item) => { | ||
items.push(item); | ||
}); | ||
if (isNested) { | ||
items.push(node.toTagEnd({ openTag, closeTag })); | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ | ||
const appendNodes = (node) => { | ||
@@ -136,11 +160,3 @@ const items = getNodes(); | ||
} else { | ||
items.push(node.toTagStart({ openTag, closeTag })); | ||
if (node.content.length) { | ||
node.content.forEach((item) => { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ openTag, closeTag })); | ||
} | ||
appendNodeAsString(node); | ||
} | ||
@@ -280,2 +296,10 @@ } else { | ||
// handles situations where we open tag, but forgot close them | ||
// for ex [q]test[/q][u]some[/u][q]some [u]some[/u] // forgot to close [/q] | ||
// so we need to flush nested content to nodes array | ||
const lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode && isTagNested(lastNestedNode.tag)) { | ||
appendNodeAsString(lastNestedNode, false); | ||
} | ||
return nodes.toArray(); | ||
@@ -282,0 +306,0 @@ }; |
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
141522
4100