ultrahtml
Version:
A 1.75kB library for enhancing `html`. `ultrahtml` has zero dependencies and is compatible with any JavaScript runtime.
32 lines (31 loc) • 11 kB
JavaScript
import{walkSync as vr,ELEMENT_NODE as nr,TEXT_NODE as Lr,render as zr}from"../index.js";import{matches as _r}from"../selector.js";var D="comm",j="rule",F="decl";var ar="@import";var ir="@keyframes";var cr="@layer";var X=Math.abs,U=String.fromCharCode;function K(r){return r.trim()}function N(r,e,t){return r.replace(e,t)}function ur(r,e,t){return r.indexOf(e,t)}function k(r,e){return r.charCodeAt(e)|0}function I(r,e,t){return r.slice(e,t)}function m(r){return r.length}function Y(r){return r.length}function R(r,e){return e.push(r),r}var V=1,M=1,fr=0,b=0,f=0,$="";function W(r,e,t,n,s,a,i,o){return{value:r,root:e,parent:t,type:n,props:s,children:a,line:V,column:M,length:i,return:"",siblings:o}}function pr(){return f}function lr(){return f=b>0?k($,--b):0,M--,f===10&&(M=1,V--),f}function w(){return f=b<fr?k($,b++):0,M++,f===10&&(M=1,V++),f}function T(){return k($,b)}function L(){return b}function B(r,e){return I($,r,e)}function J(r){switch(r){case 0:case 9:case 10:case 13:case 32:return 5;case 33:case 43:case 44:case 47:case 62:case 64:case 126:case 59:case 123:case 125:return 4;case 58:return 3;case 34:case 39:case 40:case 91:return 2;case 41:case 93:return 1}return 0}function hr(r){return V=M=1,fr=m($=r),b=0,[]}function gr(r){return $="",r}function G(r){return K(B(b-1,Q(r===91?r+2:r===40?r+1:r)))}function mr(r){for(;(f=T())&&f<33;)w();return J(r)>2||J(f)>3?"":" "}function xr(r,e){for(;--e&&w()&&!(f<48||f>102||f>57&&f<65||f>70&&f<97););return B(r,L()+(e<6&&T()==32&&w()==32))}function Q(r){for(;w();)switch(f){case r:return b;case 34:case 39:r!==34&&r!==39&&Q(f);break;case 40:r===41&&Q(r);break;case 92:w();break}return b}function br(r,e){for(;w()&&r+f!==47+10;)if(r+f===42+42&&T()===47)break;return"/*"+B(e,b-1)+"*"+U(r===47?r:w())}function wr(r){for(;!J(T());)w();return B(r,b)}function rr(r){return gr(Z("",null,null,null,[""],r=hr(r),0,[0],r))}function Z(r,e,t,n,s,a,i,o,c){for(var p=0,l=0,h=i,S=0,d=0,O=0,x=1,P=1,y=1,g=0,v="",C=s,A=a,E=n,u=v;P;)switch(O=g,g=w()){case 40:if(O!=108&&k(u,h-1)==58){ur(u+=N(G(g),"&","&\f"),"&\f",X(p?o[p-1]:0))!=-1&&(y=-1);break}case 34:case 39:case 91:u+=G(g);break;case 9:case 10:case 13:case 32:u+=mr(O);break;case 92:u+=xr(L()-1,7);continue;case 47:switch(T()){case 42:case 47:R(Ir(br(w(),L()),e,t,c),c);break;default:u+="/"}break;case 123*x:o[p++]=m(u)*y;case 125*x:case 59:case 0:switch(g){case 0:case 125:P=0;case 59+l:y==-1&&(u=N(u,/\f/g,"")),d>0&&m(u)-h&&R(d>32?Er(u+";",n,t,h-1,c):Er(N(u," ","")+";",n,t,h-2,c),c);break;case 59:u+=";";default:if(R(E=dr(u,e,t,p,l,s,o,v,C=[],A=[],h,a),a),g===123)if(l===0)Z(u,e,E,E,C,a,h,o,A);else switch(S===99&&k(u,3)===110?100:S){case 100:case 108:case 109:case 115:Z(r,E,E,n&&R(dr(r,E,E,0,0,s,o,v,s,C=[],h,A),A),s,A,h,o,n?C:A);break;default:Z(u,E,E,E,[""],A,0,o,A)}}p=l=d=0,x=y=1,v=u="",h=i;break;case 58:h=1+m(u),d=O;default:if(x<1){if(g==123)--x;else if(g==125&&x++==0&&lr()==125)continue}switch(u+=U(g),g*x){case 38:y=l>0?1:(u+="\f",-1);break;case 44:o[p++]=(m(u)-1)*y,y=1;break;case 64:T()===45&&(u+=G(w())),S=T(),l=h=m(v=u+=wr(L())),g++;break;case 45:O===45&&m(u)==2&&(x=0)}}return a}function dr(r,e,t,n,s,a,i,o,c,p,l,h){for(var S=s-1,d=s===0?a:[""],O=Y(d),x=0,P=0,y=0;x<n;++x)for(var g=0,v=I(r,S+1,S=X(P=i[x])),C=r;g<O;++g)(C=K(P>0?d[g]+" "+v:N(v,/&\f/g,d[g])))&&(c[y++]=C);return W(r,e,t,s===0?j:o,c,p,l,h)}function Ir(r,e,t,n){return W(r,e,t,D,U(pr()),I(r,2,-2),0,n)}function Er(r,e,t,n,s){return W(r,e,t,F,I(r,0,n),I(r,n+1,-1),n,s)}function z(r,e){for(var t="",n=0;n<r.length;n++)t+=e(r[n],n,r,e)||"";return t}function Sr(r,e,t,n){switch(r.type){case cr:if(r.children.length)break;case ar:case F:return r.return=r.return||r.value;case D:return"";case ir:return r.return=r.value+"{"+z(r.children,n)+"}";case j:if(!m(r.value=r.props.join(",")))return""}return m(t=z(r.children,n))?r.return=r.value+"{"+t+"}":""}function tr(r){var e=Y(r);return function(t,n,s,a){for(var i="",o=0;o<e;o++)i+=r[o](t,n,s,a)||"";return i}}var H={attribute:/\[\s*(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)\s*(?:(?<operator>\W?=)\s*(?<value>.+?)\s*(\s(?<caseSensitive>[iIsS]))?\s*)?\]/gu,id:/#(?<name>[-\w\P{ASCII}]+)/gu,class:/\.(?<name>[-\w\P{ASCII}]+)/gu,comma:/\s*,\s*/g,combinator:/\s*[\s>+~]\s*/g,"pseudo-element":/::(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu,"pseudo-class":/:(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu,universal:/(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?\*/gu,type:/(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)/gu},Tr=new Set(["combinator","comma"]),Or=new Set(["not","is","where","has","matches","-moz-any","-webkit-any","nth-child","nth-last-child"]),yr=/(?<index>[\dn+-]+)\s+of\s+(?<subtree>.+)/,Cr={"nth-child":yr,"nth-last-child":yr},Nr=r=>{switch(r){case"pseudo-element":case"pseudo-class":return new RegExp(H[r].source.replace("(?<argument>\xB6*)","(?<argument>.*)"),"gu");default:return H[r]}};function Rr(r,e){let t=0,n="";for(;e<r.length;e++){let s=r[e];switch(s){case"(":++t;break;case")":--t}if(n+=s,t===0)return n}return n}function Mr(r,e=H){if(!r)return[];let t=[r];for(let[s,a]of Object.entries(e))for(let i=0;i<t.length;i++){let o=t[i];if(typeof o!="string")continue;a.lastIndex=0;let c=a.exec(o);if(!c)continue;let p=c.index-1,l=[],h=c[0],S=o.slice(0,p+1);S&&l.push(S),l.push({...c.groups,type:s,content:h});let d=o.slice(p+h.length+1);d&&l.push(d),t.splice(i,1,...l)}let n=0;for(let s of t)switch(typeof s){case"string":throw new Error(`Unexpected sequence ${s} found at index ${n}`);case"object":n+=s.content.length,s.pos=[n-s.content.length,n],Tr.has(s.type)&&(s.content=s.content.trim()||" ")}return t}var $r=/(['"])([^\\\n]+?)\1/g,Pr=/\\./g;function Ur(r,e=H){if((r=r.trim())==="")return[];let t=[];r=(r=r.replace(Pr,(a,i)=>(t.push({value:a,offset:i}),"\uE000".repeat(a.length)))).replace($r,(a,i,o,c)=>(t.push({value:a,offset:c}),`${i}${"\uE001".repeat(o.length)}${i}`));{let a,i=0;for(;(a=r.indexOf("(",i))>-1;){let o=Rr(r,a);t.push({value:o,offset:a}),r=`${r.substring(0,a)}(${"\xB6".repeat(o.length-2)})${r.substring(a+o.length)}`,i=a+o.length}}let n=Mr(r,e),s=new Set;for(let a of t.reverse())for(let i of n){let{offset:o,value:c}=a;if(!(i.pos[0]<=o&&o+c.length<=i.pos[1]))continue;let{content:p}=i,l=o-i.pos[0];i.content=p.slice(0,l)+c+p.slice(l+c.length),i.content!==p&&s.add(i)}for(let a of s){let i=Nr(a.type);if(!i)throw new Error(`Unknown token type: ${a.type}`);i.lastIndex=0;let o=i.exec(a.content);if(!o)throw new Error(`Unable to parse content for ${a.type}: ${a.content}`);Object.assign(a,o.groups)}return n}function _(r,{list:e=!0}={}){if(e&&r.find(t=>t.type==="comma")){let t=[],n=[];for(let s=0;s<r.length;s++)if(r[s].type==="comma"){if(n.length===0)throw new Error("Incorrect comma at "+s);t.push(_(n,{list:!1})),n.length=0}else n.push(r[s]);if(n.length===0)throw new Error("Trailing comma");return t.push(_(n,{list:!1})),{type:"list",list:t}}for(let t=r.length-1;t>=0;t--){let n=r[t];if(n.type==="combinator"){let s=r.slice(0,t),a=r.slice(t+1);return{type:"complex",combinator:n.content,left:_(s),right:_(a)}}}switch(r.length){case 0:throw new Error("Could not build AST.");case 1:return r[0];default:return{type:"compound",list:[...r]}}}function*q(r,e){switch(r.type){case"list":for(let t of r.list)yield*q(t,r);break;case"complex":yield*q(r.left,r),yield*q(r.right,r);break;case"compound":yield*r.list.map(t=>[t,r]);break;default:yield[r,e]}}function er(r,{recursive:e=!0,list:t=!0}={}){let n=Ur(r);if(!n)return;let s=_(n,{list:t});if(!e)return s;for(let[a]of q(s)){if(a.type!=="pseudo-class"||!a.argument||!Or.has(a.name))continue;let i=a.argument,o=Cr[a.name];if(o){let c=o.exec(i);if(!c)continue;Object.assign(a,c.groups),i=c.groups.subtree}i&&Object.assign(a,{subtree:er(i,{recursive:!0,list:!0})})}return s}function Dr(r={}){return async e=>{let t=r.hash??Br(await zr(e)),n=[],s=!1,a=new Set,i=new Set;vr(e,o=>{if(o.type===nr&&o.name==="style"&&(!r.attribute||Fr(o,r.attribute))){s=!0,r.attribute&&delete o.attributes[r.attribute];for(let c of Vr(o.children[0].value))a.add(c)}o.type===nr&&i.add(o)}),s&&vr(e,o=>{o.type===nr&&(n.push(()=>Kr(o,t,a)),o.name==="style"&&n.push(()=>{o.children=o.children.map(c=>(c.type!==Lr||(c.value=Yr(c.value,t),c.value===""&&(o.parent.children=o.parent.children.filter(p=>p!==o))),c))}))});for(let o of n)o();return e}}var jr=new Set(["base","font","frame","frameset","head","link","meta","noframes","noscript","script","style","title"]);function Fr(r,e){return e in r.attributes?r.attributes[e]!=="false":!1}function Kr(r,e,t){let{name:n}=r;if(!!n&&!(n.length<1)&&!jr.has(n)&&!r.attributes["data-scope"]){for(let s of t)if(_r(r,s)){r.attributes["data-scope"]=e;return}}}function Ar(r,e){let t=er(r),n=s=>{switch(s.type){case"pseudo-class":return s.name==="root"?s.content:s.name==="global"?s.argument:`${s.content}:where([data-scope="${e}"])`;case"compound":return`${r}:where([data-scope="${e}"])`;case"complex":{let{left:a,right:i,combinator:o}=s;return`${n(a)}${o}${n(i)}`}case"list":return s.list.map(a=>n(a)).join(" ");default:return`${s.content}:where([data-scope="${e}"])`}};return n(t)}function Yr(r,e){return z(rr(r),tr([t=>{t.type==="rule"&&(Array.isArray(t.props)?t.props=t.props.map(n=>Ar(n,e)):t.props=Ar(t.props,e))},Sr]))}function Vr(r){let e=new Set;return z(rr(r),tr([t=>{if(t.type==="rule")if(Array.isArray(t.props))for(let n of t.props)e.add(n);else e.add(t.props)}])),Array.from(e)}var or="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY",sr=or.length;function Wr(r){let e=0;if(r.length===0)return e;for(let t=0;t<r.length;t++){let n=r.charCodeAt(t);e=(e<<5)-e+n,e=e&e}return e}function Br(r){let e,t="",n=Wr(r),s=n<0?"Z":"";for(n=Math.abs(n);n>=sr;)e=n%sr,n=Math.floor(n/sr),t=or[e]+t;return n>0&&(t=or[n]+t),s+t}export{Dr as default};
/**
* shorthash - https://github.com/bibig/node-shorthash
*
* @license
*
* (The MIT License)
*
* Copyright (c) 2013 Bibig <bibig@me.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/