@bbob/plugin-helper
Version:
Set of utils to help write plugins for @bbob/core
2 lines (1 loc) • 2.66 kB
JavaScript
!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).BbobPluginHelper={})}(this,function(t){"use strict";function e(t){return"object"==typeof t&&null!==t&&"tag"in t}function n(t){return"string"==typeof t}function r(t,e,n){return Object.keys(t).reduce((n,r)=>e(n,r,t),n)}function i(t){return e(t)&&Array.isArray(t.content)?t.content.reduce((t,e)=>t+i(e),0):n(t)?(t+"").length:0}function s(t,e){Array.isArray(t.content)&&t.content.push(e)}function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/(javascript|data|vbscript|file):/gi,"$1%3A")}function o(t,e){switch(typeof e){case"boolean":return e?""+t:"";case"number":return`${t}="${e}"`;case"string":return`${t}="${a(e)}"`;case"object":return`${t}="${a(JSON.stringify(e))}"`;default:return""}}function u(t){return null==t?"":r(t,(t,e,n)=>[...t,o(e,n[e])],[""]).join(" ")}function c(t){return r(t||{},(t,e,n)=>n[e]===e?n[e]:null,null)}let l=(t,e)=>{let n=c(e);if(n){let r=o(t,n),i={...e};delete i[n+""];let s=u(i);return`${r}${s}`}return`${t}${u(e)}`},h=(t,n,r)=>e(t)?t.toString({openTag:n,closeTag:r}):t+"",g=(t,e,n)=>Array.isArray(t)?t.reduce((t,r)=>null!==r?t+h(r,e,n):t,""):t?h(t,e,n):null;class d{get length(){return i(this)}attr(t,e){return void 0!==e&&(this.attrs[t]=e),this.attrs[t]}append(t){return s(this,t)}setStart(t){this.start=t}setEnd(t){this.end=t}toTagStart({openTag:t="[",closeTag:e="]"}={}){let n=l(this.tag+"",this.attrs);return`${t}${n}${e}`}toTagEnd({openTag:t="[",closeTag:e="]"}={}){return`${t}/${this.tag}${e}`}toTagNode(){return new d(this.tag,this.attrs,this.content,this.start,this.end)}toString({openTag:t="[",closeTag:e="]"}={}){let n=this.content?g(this.content,t,e):"",r=this.toTagStart({openTag:t,closeTag:e});return null===this.content||Array.isArray(this.content)&&0===this.content.length?r:`${r}${n}${this.toTagEnd({openTag:t,closeTag:e})}`}toJSON(){return{tag:this.tag,attrs:this.attrs,content:this.content,start:this.start,end:this.end}}static create(t,e={},n=null,r){return new d(t,e,n,r)}static isOf(t,e){return t.tag===e}constructor(t,e,n,r,i){this.tag=t,this.attrs=e,this.content=n,this.start=r,this.end=i}}t.BACKSLASH="\\",t.CLOSE_BRAKET="]",t.EQ="=",t.F="\f",t.N="\n",t.OPEN_BRAKET="[",t.QUOTEMARK='"',t.R="\r",t.SLASH="/",t.SPACE=" ",t.TAB=" ",t.TagNode=d,t.appendToNode=s,t.attrValue=o,t.attrsToString=u,t.escapeAttrValue=a,t.escapeHTML=a,t.getNodeLength=i,t.getUniqAttr=c,t.isEOL=function(t){return"\n"===t},t.isStringNode=n,t.isTagNode=e});