@favware/syntax-highlighter-core
Version:
A lightweight, easy-to-use and framework agnostic syntax highlighter for your code snippets.
1 lines • 9.65 kB
JavaScript
let e,t,l=!1,n=!1,o=!1,s=!1;const r="undefined"!=typeof window?window:{},c=r.document||{head:{}},i={t:0,l:"",jmp:e=>e(),raf:e=>requestAnimationFrame(e),ael:(e,t,l,n)=>e.addEventListener(t,l,n),rel:(e,t,l,n)=>e.removeEventListener(t,l,n),ce:(e,t)=>new CustomEvent(e,t)},a=e=>Promise.resolve(e),f=(()=>{try{return new CSSStyleSheet,"function"==typeof(new CSSStyleSheet).replace}catch(e){}return!1})(),u=new WeakMap,$=e=>"sc-"+e.o,d={},p=e=>"object"==(e=typeof e)||"function"===e,y=(e,t,...l)=>{let n=null,o=null,s=!1,r=!1,c=[];const i=t=>{for(let l=0;l<t.length;l++)n=t[l],Array.isArray(n)?i(n):null!=n&&"boolean"!=typeof n&&((s="function"!=typeof e&&!p(n))&&(n+=""),s&&r?c[c.length-1].i+=n:c.push(s?m(null,n):n),r=s)};if(i(l),t){t.name&&(o=t.name);{const e=t.className||t.class;e&&(t.class="object"!=typeof e?e:Object.keys(e).filter((t=>e[t])).join(" "))}}const a=m(e,null);return a.u=t,c.length>0&&(a.$=c),a.p=o,a},m=(e,t)=>({t:0,m:e,i:t,h:null,$:null,u:null,p:null}),h={},b=(e,t,l,n,o,s)=>{if(l!==n){let r=Z(e,t);if(t.toLowerCase(),"class"===t){const t=e.classList,o=S(l),s=S(n);t.remove(...o.filter((e=>e&&!s.includes(e)))),t.add(...s.filter((e=>e&&!o.includes(e))))}else if("ref"===t)n&&n(e);else{const c=p(n);if((r||c&&null!==n)&&!o)try{if(e.tagName.includes("-"))e[t]=n;else{let o=null==n?"":n;"list"===t?r=!1:null!=l&&e[t]==o||(e[t]=o)}}catch(e){}null==n||!1===n?!1===n&&""!==e.getAttribute(t)||e.removeAttribute(t):(!r||4&s||o)&&!c&&e.setAttribute(t,n=!0===n?"":n)}}},w=/\s/,S=e=>e?e.split(w):[],g=(e,t,l,n)=>{const o=11===t.h.nodeType&&t.h.host?t.h.host:t.h,s=e&&e.u||d,r=t.u||d;for(n in s)n in r||b(o,n,s[n],void 0,l,t.t);for(n in r)b(o,n,s[n],r[n],l,t.t)},j=(n,s,r)=>{let i,a,f,u=s.$[r],$=0;if(l||(o=!0,"slot"===u.m&&(u.t|=u.$?2:1)),null!==u.i)i=u.h=c.createTextNode(u.i);else if(1&u.t)i=u.h=c.createTextNode("");else if(i=u.h=c.createElement(2&u.t?"slot-fb":u.m),g(null,u,!1),u.$)for($=0;$<u.$.length;++$)a=j(n,u,$),a&&i.appendChild(a);return i["s-hn"]=t,3&u.t&&(i["s-sr"]=!0,i["s-cr"]=e,i["s-sn"]=u.p||"",f=n&&n.$&&n.$[r],f&&f.m===u.m&&n.h&&k(n.h,!1)),i},k=(e,l)=>{i.t|=1;const n=e.childNodes;for(let e=n.length-1;e>=0;e--){const s=n[e];s["s-hn"]!==t&&s["s-ol"]&&(R(s).insertBefore(s,O(s)),s["s-ol"].remove(),s["s-ol"]=void 0,o=!0),l&&k(s,l)}i.t&=-2},v=(e,t,l,n,o,s)=>{let r,c=e["s-cr"]&&e["s-cr"].parentNode||e;for(;o<=s;++o)n[o]&&(r=j(null,l,o),r&&(n[o].h=r,c.insertBefore(r,O(t))))},M=(e,t,l,o,s)=>{for(;t<=l;++t)(o=e[t])&&(s=o.h,U(o),n=!0,s["s-ol"]?s["s-ol"].remove():k(s,!0),s.remove())},C=(e,t)=>e.m===t.m&&("slot"!==e.m||e.p===t.p),O=e=>e&&e["s-ol"]||e,R=e=>(e["s-ol"]?e["s-ol"]:e).parentNode,T=(e,t)=>{const l=t.h=e.h,n=e.$,o=t.$,s=t.i;let r;null===s?("slot"===t.m||g(e,t,!1),null!==n&&null!==o?((e,t,l,n)=>{let o,s=0,r=0,c=t.length-1,i=t[0],a=t[c],f=n.length-1,u=n[0],$=n[f];for(;s<=c&&r<=f;)null==i?i=t[++s]:null==a?a=t[--c]:null==u?u=n[++r]:null==$?$=n[--f]:C(i,u)?(T(i,u),i=t[++s],u=n[++r]):C(a,$)?(T(a,$),a=t[--c],$=n[--f]):C(i,$)?("slot"!==i.m&&"slot"!==$.m||k(i.h.parentNode,!1),T(i,$),e.insertBefore(i.h,a.h.nextSibling),i=t[++s],$=n[--f]):C(a,u)?("slot"!==i.m&&"slot"!==$.m||k(a.h.parentNode,!1),T(a,u),e.insertBefore(a.h,i.h),a=t[--c],u=n[++r]):(o=j(t&&t[r],l,r),u=n[++r],o&&R(i.h).insertBefore(o,O(i.h)));s>c?v(e,null==n[f+1]?null:n[f+1].h,l,n,r,f):r>f&&M(t,s,c)})(l,n,t,o):null!==o?(null!==e.i&&(l.textContent=""),v(l,null,t,o,0,o.length-1)):null!==n&&M(n,0,n.length-1)):(r=l["s-cr"])?r.parentNode.textContent=s:e.i!==s&&(l.data=s)},x=e=>{let t,l,n,o,s,r,c=e.childNodes;for(l=0,n=c.length;l<n;l++)if(t=c[l],1===t.nodeType){if(t["s-sr"])for(s=t["s-sn"],t.hidden=!1,o=0;o<n;o++)if(r=c[o].nodeType,c[o]["s-hn"]!==t["s-hn"]||""!==s){if(1===r&&s===c[o].getAttribute("slot")){t.hidden=!0;break}}else if(1===r||3===r&&""!==c[o].textContent.trim()){t.hidden=!0;break}x(t)}},P=[],E=e=>{let t,l,o,s,r,c,i=0,a=e.childNodes,f=a.length;for(;i<f;i++){if(t=a[i],t["s-sr"]&&(l=t["s-cr"])&&l.parentNode)for(o=l.parentNode.childNodes,s=t["s-sn"],c=o.length-1;c>=0;c--)l=o[c],l["s-cn"]||l["s-nr"]||l["s-hn"]===t["s-hn"]||(L(l,s)?(r=P.find((e=>e.S===l)),n=!0,l["s-sn"]=l["s-sn"]||s,r?r.g=t:P.push({g:t,S:l}),l["s-sr"]&&P.map((e=>{L(e.S,l["s-sn"])&&(r=P.find((e=>e.S===l)),r&&!e.g&&(e.g=r.g))}))):P.some((e=>e.S===l))||P.push({S:l}));1===t.nodeType&&E(t)}},L=(e,t)=>1===e.nodeType?null===e.getAttribute("slot")&&""===t||e.getAttribute("slot")===t:e["s-sn"]===t||""===t,U=e=>{e.u&&e.u.ref&&e.u.ref(null),e.$&&e.$.map(U)},W=(e,t,l)=>{const n=(e=>Q(e).j)(e);return{emit:e=>A(n,t,{bubbles:!!(4&l),composed:!!(2&l),cancelable:!!(1&l),detail:e})}},A=(e,t,l)=>{const n=i.ce(t,l);return e.dispatchEvent(n),n},D=(e,t)=>{t&&!e.k&&t["s-p"]&&t["s-p"].push(new Promise((t=>e.k=t)))},H=(e,t)=>{if(e.t|=16,!(4&e.t))return D(e,e.v),fe((()=>N(e,t)));e.t|=512},N=(e,t)=>{const l=e.M;let n;return t&&(n=z(l,"componentWillLoad")),B(n,(()=>q(e,l,t)))},q=async(e,t,l)=>{const n=e.j,o=n["s-rc"];l&&(e=>{const t=e.C;((e,t)=>{let l=$(t),n=ne.get(l);if(e=11===e.nodeType?e:c,n)if("string"==typeof n){let t,o=u.get(e=e.head||e);o||u.set(e,o=new Set),o.has(l)||(t=c.createElement("style"),t.innerHTML=n,e.insertBefore(t,e.querySelector("link")),o&&o.add(l))}else e.adoptedStyleSheets.includes(n)||(e.adoptedStyleSheets=[...e.adoptedStyleSheets,n])})(e.j.getRootNode(),t)})(e);F(e,t),o&&(o.map((e=>e())),n["s-rc"]=void 0);{const t=n["s-p"],l=()=>V(e);0===t.length?l():(Promise.all(t).then(l),e.t|=4,t.length=0)}},F=(s,r)=>{try{r=r.render(),s.t&=-17,s.t|=2,((s,r)=>{const a=s.j,f=s.C,u=s.O||m(null,null),$=(e=>e&&e.m===h)(r)?r:y(null,null,r);if(t=a.tagName,f.R&&($.u=$.u||{},f.R.map((([e,t])=>$.u[t]=a[e]))),$.m=null,$.t|=4,s.O=$,$.h=u.h=a,e=a["s-cr"],l=0!=(1&f.t),n=!1,T(u,$),i.t|=1,o){let e,t,l,n,o,s;E($.h);let r=0;for(;r<P.length;r++)e=P[r],t=e.S,t["s-ol"]||(l=c.createTextNode(""),l["s-nr"]=t,t.parentNode.insertBefore(t["s-ol"]=l,t));for(r=0;r<P.length;r++)if(e=P[r],t=e.S,e.g){for(n=e.g.parentNode,o=e.g.nextSibling,l=t["s-ol"];l=l.previousSibling;)if(s=l["s-nr"],s&&s["s-sn"]===t["s-sn"]&&n===s.parentNode&&(s=s.nextSibling,!s||!s["s-nr"])){o=s;break}(!o&&n!==t.parentNode||t.nextSibling!==o)&&t!==o&&(!t["s-hn"]&&t["s-ol"]&&(t["s-hn"]=t["s-ol"].parentNode.nodeName),n.insertBefore(t,o))}else 1===t.nodeType&&(t.hidden=!0)}n&&x($.h),i.t&=-2,P.length=0})(s,r)}catch(e){ee(e,s.j)}return null},V=e=>{const t=e.j,l=e.M,n=e.v;z(l,"componentDidRender"),64&e.t?z(l,"componentDidUpdate"):(e.t|=64,G(t),e.T(t),n||_()),e.k&&(e.k(),e.k=void 0),512&e.t&&ae((()=>H(e,!1))),e.t&=-517},_=()=>{G(c.documentElement),ae((()=>A(r,"appload",{detail:{namespace:"favware-syntax-highlighter-core"}})))},z=(e,t,l)=>{if(e&&e[t])try{return e[t](l)}catch(e){ee(e)}},B=(e,t)=>e&&e.then?e.then(t):t(),G=e=>e.classList.add("hydrated"),I=(e,t,l)=>{if(t.P){const n=Object.entries(t.P),o=e.prototype;if(n.map((([e,[n]])=>{(31&n||2&l&&32&n)&&Object.defineProperty(o,e,{get(){return((e,t)=>Q(this).L.get(t))(0,e)},set(l){((e,t,l,n)=>{const o=Q(e),s=o.L.get(t),r=o.t,c=o.M;l=((e,t)=>null==e||p(e)?e:1&t?e+"":e)(l,n.P[t][0]),8&r&&void 0!==s||l===s||(o.L.set(t,l),c&&2==(18&r)&&H(o,!1))})(this,e,l,t)},configurable:!0,enumerable:!0})})),1&l){const l=new Map;o.attributeChangedCallback=function(e,t,n){i.jmp((()=>{const t=l.get(e);this[t]=(null!==n||"boolean"!=typeof this[t])&&n}))},e.observedAttributes=n.filter((([e,t])=>15&t[0])).map((([e,n])=>{const o=n[1]||e;return l.set(o,e),512&n[0]&&t.R.push([e,o]),o}))}}return e},J=(e,t={})=>{const l=[],n=t.exclude||[],o=r.customElements,s=c.head,a=s.querySelector("meta[charset]"),u=c.createElement("style"),d=[];let p,y=!0;Object.assign(i,t),i.l=new URL(t.resourcesUrl||"./",c.baseURI).href,e.map((e=>e[1].map((t=>{const s={t:t[0],o:t[1],P:t[2],U:t[3]};s.P=t[2],s.R=[];const r=s.o,a=class extends HTMLElement{constructor(e){super(e),Y(e=this,s)}connectedCallback(){p&&(clearTimeout(p),p=null),y?d.push(this):i.jmp((()=>(e=>{if(0==(1&i.t)){const t=Q(e),l=t.C,n=()=>{};if(!(1&t.t)){t.t|=1,12&l.t&&(e=>{const t=e["s-cr"]=c.createComment("");t["s-cn"]=!0,e.insertBefore(t,e.firstChild)})(e);{let l=e;for(;l=l.parentNode||l.host;)if(l["s-p"]){D(t,t.v=l);break}}l.P&&Object.entries(l.P).map((([t,[l]])=>{if(31&l&&e.hasOwnProperty(t)){const l=e[t];delete e[t],e[t]=l}})),(async(e,t,l,n,o)=>{if(0==(32&t.t)){{if(t.t|=32,(o=le(l)).then){const e=()=>{};o=await o,e()}o.isProxied||(I(o,l,2),o.isProxied=!0);const e=()=>{};t.t|=8;try{new o(t)}catch(e){ee(e)}t.t&=-9,e()}if(o.style){let e=o.style;const t=$(l);if(!ne.has(t)){const n=()=>{};((e,t,l)=>{let n=ne.get(e);f&&l?(n=n||new CSSStyleSheet,n.replace(t)):n=t,ne.set(e,n)})(t,e,!!(1&l.t)),n()}}}const s=t.v,r=()=>H(t,!0);s&&s["s-rc"]?s["s-rc"].push(r):r()})(0,t,l)}n()}})(this)))}disconnectedCallback(){i.jmp((()=>{}))}componentOnReady(){return Q(this).W}};s.A=e[0],n.includes(r)||o.get(r)||(l.push(r),o.define(r,I(a,s,1)))})))),u.innerHTML=l+"{visibility:hidden}.hydrated{visibility:inherit}",u.setAttribute("data-styles",""),s.insertBefore(u,a?a.nextSibling:s.firstChild),y=!1,d.length?d.map((e=>e.connectedCallback())):i.jmp((()=>p=setTimeout(_,30)))},K=new WeakMap,Q=e=>K.get(e),X=(e,t)=>K.set(t.M=e,t),Y=(e,t)=>{const l={t:0,j:e,C:t,L:new Map};return l.W=new Promise((e=>l.T=e)),e["s-p"]=[],e["s-rc"]=[],K.set(e,l)},Z=(e,t)=>t in e,ee=(e,t)=>(0,console.error)(e,t),te=new Map,le=e=>{const t=e.o.replace(/-/g,"_"),l=e.A,n=te.get(l);return n?n[t]:import(`./${l}.entry.js`).then((e=>(te.set(l,e),e[t])),ee)},ne=new Map,oe=[],se=[],re=(e,t)=>l=>{e.push(l),s||(s=!0,t&&4&i.t?ae(ie):i.raf(ie))},ce=e=>{for(let t=0;t<e.length;t++)try{e[t](performance.now())}catch(e){ee(e)}e.length=0},ie=()=>{ce(oe),ce(se),(s=oe.length>0)&&i.raf(ie)},ae=e=>a().then(e),fe=re(se,!0);export{h as H,J as b,W as c,y as h,a as p,X as r}