import{walkSync as Rt,ELEMENT_NODE as Ct,TEXT_NODE as At}from"../index.js";import{querySelectorAll as St,specificity as ht}from"../selector.js";var Te="comm",Pe="rule",Ie="decl";var Ne=Math.abs,q=String.fromCharCode;function $(e){return e.trim()}function Z(e,r,t){return e.replace(r,t)}function we(e,r){return e.indexOf(r)}function V(e,r){return e.charCodeAt(r)|0}function M(e,r,t){return e.slice(r,t)}function C(e){return e.length}function ke(e){return e.length}function F(e,r){return r.push(e),e}var J=1,D=1,Oe=0,w=0,E=0,B="";function ee(e,r,t,o,n,i,a){return{value:e,root:r,parent:t,type:o,props:n,children:i,line:J,column:D,length:a,return:""}}function Re(){return E}function Ce(){return E=w>0?V(B,--w):0,D--,E===10&&(D=1,J--),E}function k(){return E=w2||pe(E)>3?"":" "}function Me(e,r){for(;--r&&k()&&!(E<48||E>102||E>57&&E<65||E>70&&E<97););return te(e,U()+(r<6&&A()==32&&k()==32))}function ce(e){for(;k();)switch(E){case e:return w;case 34:case 39:e!==34&&e!==39&&ce(E);break;case 40:e===41&&ce(e);break;case 92:k();break}return w}function Le(e,r){for(;k()&&e+E!==47+10;)if(e+E===42+42&&A()===47)break;return"/*"+te(r,w-1)+"*"+q(e===47?e:k())}function Ve(e){for(;!pe(A());)k();return te(e,w)}function de(e){return Se(ne("",null,null,null,[""],e=Ae(e),0,[0],e))}function ne(e,r,t,o,n,i,a,s,l){for(var p=0,u=0,c=a,d=0,f=0,m=0,v=1,T=1,x=1,y=0,P="",N=n,b=i,g=o,h=P;T;)switch(m=y,y=k()){case 40:if(m!=108&&V(h,c-1)==58){we(h+=Z(re(y),"&","&\f"),"&\f")!=-1&&(x=-1);break}case 34:case 39:case 91:h+=re(y);break;case 9:case 10:case 13:case 32:h+=_e(m);break;case 92:h+=Me(U()-1,7);continue;case 47:switch(A()){case 42:case 47:F(vt(Le(k(),U()),r,t),l);break;default:h+="/"}break;case 123*v:s[p++]=C(h)*x;case 125*v:case 59:case 0:switch(y){case 0:case 125:T=0;case 59+u:f>0&&C(h)-c&&F(f>32?De(h+";",o,t,c-1):De(Z(h," ","")+";",o,t,c-2),l);break;case 59:h+=";";default:if(F(g=Fe(h,r,t,p,u,n,s,P,N=[],b=[],c),i),y===123)if(u===0)ne(h,r,g,g,N,i,c,s,b);else switch(d){case 100:case 109:case 115:ne(e,g,g,o&&F(Fe(e,g,g,0,0,n,s,P,n,N=[],c),b),n,b,c,s,o?N:b);break;default:ne(h,g,g,g,[""],b,0,s,b)}}p=u=f=0,v=x=1,P=h="",c=a;break;case 58:c=1+C(h),f=m;default:if(v<1){if(y==123)--v;else if(y==125&&v++==0&&Ce()==125)continue}switch(h+=q(y),y*v){case 38:x=u>0?1:(h+="\f",-1);break;case 44:s[p++]=(C(h)-1)*x,x=1;break;case 64:A()===45&&(h+=re(k())),d=A(),u=c=C(P=h+=Ve(U())),y++;break;case 45:m===45&&C(h)==2&&(v=0)}}return i}function Fe(e,r,t,o,n,i,a,s,l,p,u){for(var c=n-1,d=n===0?i:[""],f=ke(d),m=0,v=0,T=0;m0?d[x]+" "+y:Z(y,/&\f/g,d[x])))&&(l[T++]=P);return ee(e,r,t,n===0?Pe:s,l,p,u)}function vt(e,r,t){return ee(e,r,t,Te,q(Re()),M(e,2,-2),0)}function De(e,r,t,o){return ee(e,r,t,Ie,M(e,0,o),M(e,o+1,-1),o)}var R=e=>typeof e=="object"&&e!==null&&"errid"in e,yt=e=>{let r=[[]],t=[];for(let o of e)if(o.type==="comma"&&t.length===0)r.push([]);else{switch(o.type){case"function":case"(":t.push(")");break;case"[":t.push("]");break;case"{":t.push("}");break;case")":case"]":case"}":t.at(-1)===o.type&&t.pop();break}r[r.length-1].push(o)}return r},Be=e=>{let r=yt(e);if(r.length===1&&r[0].length===0)return{type:"query-list",mediaQueries:[{type:"query"}]};{let t=[];for(let o of r){let n=Ge(o);R(n)?t.push({type:"query",prefix:"not"}):t.push(n)}return{type:"query-list",mediaQueries:t}}},Ge=e=>{var r,t,o;let n=e.at(0);if(n){if(n.type==="("){let i=S(e,!0);if(R(i)){let{start:a,end:s}=(r=e.at(1))!==null&&r!==void 0?r:n;return{errid:"EXPECT_FEATURE_OR_CONDITION",start:a,end:s,child:i}}return{type:"query",mediaCondition:i}}if(n.type==="ident"){let i,a,{value:s,end:l}=n;s!=="only"&&s!=="not"||(i=s);let p=i===void 0?0:1,u=e.at(p);if(!u)return{errid:"EXPECT_LPAREN_OR_TYPE",start:l,end:l};if(u.type!=="ident"){if(i==="not"&&u.type==="("){let c=S(e.slice(p),!0);if(R(c)){let{start:d,end:f}=(t=e.at(p+1))!==null&&t!==void 0?t:u;return{errid:"EXPECT_CONDITION",start:d,end:f,child:c}}return{type:"query",mediaCondition:{type:"condition",operator:"not",children:[c]}}}{let{start:c,end:d}=u;return{errid:"EXPECT_TYPE",start:c,end:d}}}{let{value:c,start:d,end:f}=u;if(c==="all")a=void 0;else if(c==="print"||c==="screen")a=c;else{if(c!=="tty"&&c!=="tv"&&c!=="projection"&&c!=="handheld"&&c!=="braille"&&c!=="embossed"&&c!=="aural"&&c!=="speech")return{errid:"EXPECT_TYPE",start:d,end:f};i=i==="not"?void 0:"not",a=void 0}}if(p+1===e.length)return{type:"query",prefix:i,mediaType:a};{let c=e[p+1];if(c.type==="ident"&&c.value==="and"){let d=e.at(-1),f=e.at(p+2),m,v=d.end+1;if((f==null?void 0:f.type)==="ident"&&f.value==="not"){v+=1;let y=S(e.slice(p+3),!1);m=R(y)?y:{type:"condition",operator:"not",children:[y]}}else m=S(e.slice(p+2),!1);let{start:T,end:x}=(o=e.at(p+2))!==null&&o!==void 0?o:{start:v,end:v};return R(m)?{errid:"EXPECT_CONDITION",start:T,end:x,child:m}:{type:"query",prefix:i,mediaType:a,mediaCondition:m}}return{errid:"EXPECT_AND",start:c.start,end:c.end}}}return{errid:"EXPECT_LPAREN_OR_TYPE_OR_MODIFIER",start:n.start,end:n.end}}return{errid:"EMPTY_QUERY",start:0,end:0}},S=(e,r,t)=>{let o=e.at(0);if(o){if(o.type!=="(")return{errid:"EXPECT_LPAREN",start:o.start,end:o.end};let n,i=e.length-1,a=0,s=0;for(let[p,u]of e.entries())if(u.type==="("?(s+=1,a=Math.max(a,s)):u.type===")"&&(s-=1),s===0){i=p;break}if(s!==0)return{errid:"MISMATCH_PARENS",start:o.start,end:e[e.length-1].end};let l=e.slice(0,i+1);if(n=a===1?qe(l):l[1].type==="ident"&&l[1].value==="not"?S(l.slice(2,-1),!0,"not"):S(l.slice(1,-1),!0),R(n))return{errid:"EXPECT_FEATURE_OR_CONDITION",start:o.start,end:l[l.length-1].end,child:n};if(i===e.length-1)return{type:"condition",operator:t,children:[n]};{let p=e[i+1];if(p.type!=="ident"||p.value!=="and"&&p.value!=="or")return{errid:"EXPECT_AND_OR_OR",start:p.start,end:p.end};if(t!==void 0&&t!==p.value)return{errid:"MIX_AND_WITH_OR",start:p.start,end:p.end};if(p.value==="or"&&!r)return{errid:"MIX_AND_WITH_OR",start:p.start,end:p.end};let u=S(e.slice(i+2),r,p.value);return R(u)?u:{type:"condition",operator:p.value,children:[n,...u.children]}}}return{errid:"EMPTY_CONDITION",start:0,end:0}},qe=e=>{let r=e.at(0);if(r){if(r.type!=="(")return{errid:"EXPECT_LPAREN",start:r.start,end:r.end};let t=e[e.length-1];if(t.type!==")")return{errid:"EXPECT_RPAREN",start:t.end+1,end:t.end+1};let o=[e[0]];for(let i=1;i0&&s.type==="delim"&&s.value===47&&l.type==="number"&&l.value>0){o.push({type:"ratio",numerator:a.value,denominator:l.value,hasSpaceBefore:a.hasSpaceBefore,hasSpaceAfter:l.hasSpaceAfter,start:a.start,end:l.end}),i+=2;continue}}o.push(e[i])}let n=o[1];if(n.type==="ident"&&o.length===3)return{type:"feature",context:"boolean",feature:n.value};if(o.length===5&&o[1].type==="ident"&&o[2].type==="colon"){let i=o[3];if(i.type==="number"||i.type==="dimension"||i.type==="ratio"||i.type==="ident"){let a,s=o[1].value,l=s.slice(0,4);l==="min-"?(a="min",s=s.slice(4)):l==="max-"&&(a="max",s=s.slice(4));let{hasSpaceBefore:p,hasSpaceAfter:u,start:c,end:d,...f}=i;return{type:"feature",context:"value",prefix:a,feature:s,value:f}}return{errid:"EXPECT_VALUE",start:i.start,end:i.end}}if(o.length>=5){let i=gt(o);if(R(i))return{errid:"EXPECT_RANGE",start:r.start,end:o[o.length-1].end,child:i};{let{feature:a,...s}=i;return{type:"feature",context:"range",feature:a,range:s}}}return{errid:"INVALID_FEATURE",start:r.start,end:e[e.length-1].end}}return{errid:"EMPTY_FEATURE",start:0,end:0}},gt=e=>{var r,t,o,n,i,a,s,l;if(e.length<5)return{errid:"INVALID_RANGE",start:(t=(r=e.at(0))===null||r===void 0?void 0:r.start)!==null&&t!==void 0?t:0,end:(n=(o=e.at(-1))===null||o===void 0?void 0:o.end)!==null&&n!==void 0?n:0};if(e[0].type!=="(")return{errid:"EXPECT_LPAREN",start:e[0].start,end:e[0].end};let p=e[e.length-1];if(p.type!==")")return{errid:"EXPECT_RPAREN",start:p.start,end:p.end};let u={feature:""},c=e[1].type==="number"||e[1].type==="dimension"||e[1].type==="ratio"||e[1].type==="ident"&&e[1].value==="infinite";if(e[2].type==="delim"){if(e[2].value===60)e[3].type!=="delim"||e[3].value!==61||e[3].hasSpaceBefore?u[c?"leftOp":"rightOp"]="<":u[c?"leftOp":"rightOp"]="<=";else if(e[2].value===62)e[3].type!=="delim"||e[3].value!==61||e[3].hasSpaceBefore?u[c?"leftOp":"rightOp"]=">":u[c?"leftOp":"rightOp"]=">=";else{if(e[2].value!==61)return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};u[c?"leftOp":"rightOp"]="="}if(c)u.leftToken=e[1];else{if(e[1].type!=="ident")return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};u.feature=e[1].value}let d=2+((a=(i=u[c?"leftOp":"rightOp"])===null||i===void 0?void 0:i.length)!==null&&a!==void 0?a:0),f=e[d];if(c){if(f.type!=="ident")return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};if(u.feature=f.value,e.length>=7){let g=e[d+1],h=e[d+2];if(g.type!=="delim")return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};{let H=g.value;if(H===60)h.type!=="delim"||h.value!==61||h.hasSpaceBefore?u.rightOp="<":u.rightOp="<=";else{if(H!==62)return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};h.type!=="delim"||h.value!==61||h.hasSpaceBefore?u.rightOp=">":u.rightOp=">="}let W=d+1+((l=(s=u.rightOp)===null||s===void 0?void 0:s.length)!==null&&l!==void 0?l:0),G=e.at(W);if(W+2!==e.length)return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};u.rightToken=G}}else if(d+2!==e.length)return{errid:"INVALID_RANGE",start:e[0].start,end:p.end}}else u.rightToken=f;let m,{leftToken:v,leftOp:T,feature:x,rightOp:y,rightToken:P}=u,N,b;if(v!==void 0){if(v.type==="ident"){let{type:g,value:h}=v;h==="infinite"&&(N={type:g,value:h})}else if(v.type==="number"||v.type==="dimension"||v.type==="ratio"){let{hasSpaceBefore:g,hasSpaceAfter:h,start:H,end:W,...G}=v;N=G}}if(P!==void 0){if(P.type==="ident"){let{type:g,value:h}=P;h==="infinite"&&(b={type:g,value:h})}else if(P.type==="number"||P.type==="dimension"||P.type==="ratio"){let{hasSpaceBefore:g,hasSpaceAfter:h,start:H,end:W,...G}=P;b=G}}if(N!==void 0&&b!==void 0)if(T!=="<"&&T!=="<="||y!=="<"&&y!=="<="){if(T!==">"&&T!==">="||y!==">"&&y!==">=")return{errid:"INVALID_RANGE",start:e[0].start,end:p.end};m={leftToken:N,leftOp:T,feature:x,rightOp:y,rightToken:b}}else m={leftToken:N,leftOp:T,feature:x,rightOp:y,rightToken:b};else(N===void 0&&T===void 0&&y!==void 0&&b!==void 0||N!==void 0&&T!==void 0&&y===void 0&&b===void 0)&&(m={leftToken:N,leftOp:T,feature:x,rightOp:y,rightToken:b});return m??{errid:"INVALID_RANGE",start:e[0].start,end:p.end}}return{errid:"INVALID_RANGE",start:e[0].start,end:p.end}};var Ue=e=>({type:"query-list",mediaQueries:e.mediaQueries.map(r=>Qe(r))}),Qe=e=>e.mediaCondition?{type:"query",prefix:e.prefix,mediaType:e.mediaType,mediaCondition:fe(e.mediaCondition)}:e,fe=e=>{let r=[];for(let t of e.children)if(t.type==="condition"){let o=fe(t);o.operator===void 0&&o.children.length===1?r.push(o.children[0]):o.operator!==e.operator||o.operator!=="and"&&o.operator!=="or"?r.push(o):r.push(...o.children)}else r.push(t);if(r.length===1){let t=r[0];if(t.type==="condition"){if(e.operator===void 0)return t;if(e.operator==="not"&&t.operator==="not")return{type:"condition",children:t.children}}}return{type:"condition",operator:e.operator,children:r}};var Ye=e=>{let r=[e];for(let t=e.child;t!==void 0;t=t.child)r.push(t);for(let t=r.length-2;t>=0;t--)r[t+1].child=r.at(t);return delete r[0].child,r.at(-1)},_=e=>{switch(e.type){case"query-list":for(let r of e.mediaQueries)_(r);return e;case"query":return e.prefix===void 0&&delete e.prefix,e.mediaType===void 0&&delete e.mediaType,e.mediaCondition===void 0?delete e.mediaCondition:_(e.mediaCondition),e;case"condition":e.operator===void 0&&delete e.operator;for(let r of e.children)_(r);return e;case"feature":return e.context==="value"?(e.prefix===void 0&&delete e.prefix,_(e.value)):e.context==="range"&&(e.range.leftOp===void 0&&delete e.range.leftOp,e.range.rightOp===void 0&&delete e.range.rightOp,e.range.leftToken===void 0?delete e.range.leftToken:_(e.range.leftToken),e.range.rightToken===void 0?delete e.range.rightToken:_(e.range.rightToken)),e;default:return e}};var me,je=e=>{let r=(()=>{let n;return me?n=me:(n=new TextEncoder,me=n),n})().encode(e),t=[],o=r.length;for(let n=0;n>>53|r[++n]<<58>>>58):i<240?t.push(i<<60>>>48|r[++n]<<58>>>52|r[++n]<<58>>>58):t.push(i<<61>>>43|r[++n]<<58>>>46|r[++n]<<58>>>52|r[++n]<<58>>>58)}return t};var Ke=e=>{let r=[],t=!1;for(let o of e)switch(o.type){case"{":return{errid:"NO_LCURLY",start:o.start,end:o.end};case"semicolon":return{errid:"NO_SEMICOLON",start:o.start,end:o.end};case"whitespace":t=!0,r.length>0&&(r[r.length-1].hasSpaceAfter=!0);break;case"EOF":break;default:r.push({...o,hasSpaceBefore:t,hasSpaceAfter:!1}),t=!1}return r};var He=(e,r=0)=>{let t=[];for(;r=65&&i<=90||i>=97&&i<=122||i>=128||i>=48&&i<=57||i===92&&r+2=48&&o<=57){let i=oe(e,r),[a,s]=i;r=a,s[0]==="dimension"?t.push({type:"dimension",value:s[1],unit:s[2].toLowerCase(),flag:"number",start:n,end:r}):s[0]==="number"?t.push({type:s[0],value:s[1],flag:s[2],start:n,end:r}):t.push({type:s[0],value:s[1],flag:"number",start:n,end:r})}else if(o===95||o>=65&&o<=90||o>=97&&o<=122||o>=128||o===92){let i=Xe(e,r);if(i===null)t.push({type:"delim",value:o,start:n,end:r});else{let[a,s,l]=i;r=a,t.push({type:l,value:s,start:n,end:r})}}else t.push({type:"delim",value:o,start:n,end:r})}return t.push({type:"EOF",start:r,end:r}),t},ze=(e,r)=>{if(e.length<=r+1)return null;let t=e.at(r),o=[];for(let n=r+1;n{let t=e.at(r);if(t===void 0)return!1;if(t===45){let o=e.at(r+1);return o===void 0?!1:o===45||o===95||o>=65&&o<=90||o>=97&&o<=122||o>=128?!0:o===92?e.length<=r+2?!1:e.at(r+2)!==10:!1}return t===95||t>=65&&t<=90||t>=97&&t<=122||t>=128?!0:t===92?e.length<=r+1?!1:e.at(r+1)!==10:!1},he=(e,r)=>{if(e.length<=r+1||e.at(r)!==92)return null;let t=e.at(r+1);if(t===10)return null;if(t>=48&&t<=57||t>=65&&t<=70||t>=97&&t<=102){let o=[t],n=Math.min(r+7,e.length),i=r+2;for(;i=48&&a<=57||a>=65&&a<=70||a>=97&&a<=102))break;o.push(a)}if(i{let t=xt(e,r);if(t===null)return null;let[o,n,i]=t,a=ve(e,o+1);if(a!==null){let[s,l]=a;return[s,["dimension",n,l]]}return o+1{let t=e.at(r);if(t===void 0)return null;let o="integer",n=[];for(t!==43&&t!==45||(r+=1,t===45&&n.push(45));r=48&&s<=57))break;n.push(s),r+=1}if(r+1=48&&l<=57)for(n.push(s,l),o="number",r+=2;r=48&&p<=57))break;n.push(p),r+=1}}if(r+1=48&&l<=57?(n.push(69,l),r+=2,u=!0):(l===45||l===43)&&p!==void 0&&p>=48&&p<=57&&(n.push(69),l===45&&n.push(45),n.push(p),r+=3,u=!0),u)for(o="number";r=48&&c<=57))break;n.push(c),r+=1}}}let i=String.fromCodePoint(...n),a=o==="number"?Number.parseFloat(i):Number.parseInt(i);return a===0&&(a=0),Number.isNaN(a)?null:[r-1,a,o]},$e=(e,r)=>{if(e.length<=r)return null;let t=[];for(let o=e.at(r);r=65&&o<=90||o>=97&&o<=122||o>=128||o>=48&&o<=57)){{let n=he(e,r);if(n!==null){let[i,a]=n;t.push(a),r=i;continue}}break}t.push(o)}return r===0?null:[r-1,String.fromCodePoint(...t)]},ve=(e,r)=>We(e,r)?$e(e,r):null,bt=(e,r)=>{let t=e.at(r);for(;t===9||t===32||t===10;)t=e.at(++r);let o=[],n=!1;for(;r0&&(n=!0);else if(t===92){let i=he(e,r);if(i===null||n)return null;let[a,s]=i;o.push(s),r=a}else{if(n)return null;o.push(t)}t=e.at(++r)}return null},Xe=(e,r)=>{let t=ve(e,r);if(t===null)return null;let[o,n]=t;if(n.toLowerCase()==="url"){if(e.length>o+1&&e.at(o+1)===40){for(let i=2;o+io+1&&e.at(o+1)===40)return[o+1,n.toLowerCase(),"function"];return[o,n.toLowerCase(),"ident"]};var Ze=e=>{let r=He(je(e));return R(r)?r:Ke(r)};var ie=e=>typeof e=="object"&&e!==null&&"errid"in e;var Je=e=>{let r=Ze(e);return ie(r)?Ye(r):_(Ue(Be(r)))};var se={"any-hover":{none:1,hover:1},"any-pointer":{none:1,coarse:1,fine:1},"color-gamut":{srgb:1,p3:1,rec2020:1},grid:{0:1,1:1},hover:{none:1,hover:1},"overflow-block":{none:1,scroll:1,paged:1},"overflow-inline":{none:1,scroll:1},pointer:{none:1,coarse:1,fine:1},scan:{interlace:1,progressive:1},update:{none:1,slow:1,fast:1},"display-mode":{fullscreen:1,standalone:1,"minimal-ui":1,browser:1},"dynamic-range":{standard:1,high:1},"environment-blending":{opaque:1,additive:1,subtractive:1},"forced-colors":{none:1,active:1},"inverted-colors":{none:1,inverted:1},"nav-controls":{none:1,back:1},"prefers-color-scheme":{light:1,dark:1},"prefers-contrast":{"no-preference":1,less:1,more:1,custom:1},"prefers-reduced-data":{"no-preference":1,reduce:1},"prefers-reduced-motion":{"no-preference":1,reduce:1},"prefers-reduced-transparency":{"no-preference":1,reduce:1},scripting:{none:1,"initial-only":1,enabled:1},"video-color-gamut":{srgb:1,p3:1,rec2020:1},"video-dynamic-range":{standard:1,high:1}},Q={color:{feature:"color",type:"integer",bounds:[!0,0,1/0,!1]},"color-index":{feature:"color-index",type:"integer",bounds:[!0,0,1/0,!1]},monochrome:{feature:"monochrome",type:"integer",bounds:[!0,0,1/0,!1]},"device-height":{feature:"device-height",type:"length",bounds:[!0,0,1/0,!1]},"device-width":{feature:"device-width",type:"length",bounds:[!0,0,1/0,!1]},height:{feature:"height",type:"length",bounds:[!0,0,1/0,!1]},width:{feature:"width",type:"length",bounds:[!0,0,1/0,!1]},resolution:{feature:"resolution",type:"resolution",bounds:[!0,0,1/0,!1]},"horizontal-viewport-segments":{feature:"horizontal-viewport-segments",type:"integer",bounds:[!0,0,1/0,!1]},"vertical-viewport-segments":{feature:"vertical-viewport-segments",type:"integer",bounds:[!0,0,1/0,!1]}},ye={"aspect-ratio":{feature:"aspect-ratio",type:"ratio",bounds:[!1,[0,1],[1/0,1],!1]},"device-aspect-ratio":{feature:"device-aspect-ratio",type:"ratio",bounds:[!1,[0,1],[1/0,1],!1]}},Y=e=>Object.entries(e).filter(r=>r[1]!==void 0),et=new Set(Object.keys(se)),tt=e=>et.has(e[0]),le=e=>et.has(e),rt=new Set(Object.keys(ye)),j=e=>rt.has(e[0]),K=e=>rt.has(e),nt=new Set(Object.keys(Q)),ge=e=>nt.has(e[0]),ot=e=>nt.has(e),it=e=>ge(e)||j(e),xe=e=>ot(e)||K(e);var be=e=>ot(e)||K(e)||le(e),L=(e,r)=>{e[r[0]]=r[1]},ae=(...e)=>e.reduce((r,t)=>r==="{true}"?t:t==="{true}"?r:r==="{false}"||t==="{false}"?"{false}":((o,n)=>{let[i,a,s,l]=o,p=typeof a=="number"?a:a[0]/a[1],u=typeof s=="number"?s:s[0]/s[1],[c,d,f,m]=n,v=typeof d=="number"?d:d[0]/d[1],T=typeof f=="number"?f:f[0]/f[1],x=i!==c&&!i;p!==v&&(x=p>v);let y=l!==m&&!l;u!==T&&(y=ub||N===b&&(!P||!g)?"{false}":[P,N,b,g]})(r,t),"{true}"),ue=e=>{if(j(e)){let{bounds:r}=ye[e[0]],t=ae(e[1],r);if(typeof t=="string")return t;if(t[0]===r[0]&&t[1][0]===r[1][0]&&t[1][1]===r[1][1]&&t[2][0]===r[2][0]&&t[2][1]===r[2][1]&&t[3]===r[3])return"{true}";{let o=t[1][0]/t[1][1],n=t[2][0]/t[2][1];return o>n||o===n&&(!t[0]||!t[3])?"{false}":t}}{let{bounds:r}=Q[e[0]],t=ae(e[1],r);return typeof t=="string"?t:t[0]===r[0]&&t[1]===r[1]&&t[2]===r[2]&&t[3]===r[3]?"{true}":t[1]>t[2]||t[1]===t[2]&&(!t[0]||!t[3])?"{false}":t}};var at=e=>{if(typeof e[1]=="string")throw new Error("expected range");let{bounds:r}=ye[e[0]],[t,o,n,i]=e[1],a=o[0]/o[1],s=n[0]/n[1],l=r[1][0]/r[1][1],p=r[0],u=r[2][0]/r[2][1],c=r[3],d=s>u||s===u&&!(c&&!i);return al||n===l&&!(p&&!i);return o{if(e.type==="number")return{type:"number",value:e.value};if(e.type==="dimension"){let t;switch(e.unit){case"s":case"ms":t="time";break;case"hz":case"khz":t="frequency";break;case"dpi":case"dpcm":case"dppx":case"x":t="resolution";break;default:t="length"}if(e.unit==="px")return{type:"dimension",subtype:"length",px:e.value};if(t==="time")return{type:"dimension",subtype:"time",ms:e.unit==="s"?Math.round(1e3*e.value):e.value};if(t==="frequency")return{type:"dimension",subtype:"frequency",hz:e.unit==="khz"?Math.round(1e3*e.value):e.value};if(t==="resolution"){let o=e.value;return e.unit==="dpi"?o=Number.parseFloat((.0104166667*e.value).toFixed(3)):e.unit==="dpcm"&&(o=Number.parseFloat((.0264583333*e.value).toFixed(3))),{type:"dimension",subtype:"resolution",dppx:o}}if(e.unit in r){let o=r[e.unit];return{type:"dimension",subtype:"length",px:Number.parseFloat((e.value*o).toFixed(3))}}return{type:"ident",value:"{never}"}}return e.type==="ident"?e.value==="infinite"?{type:"infinite"}:{type:"ident",value:e.value}:{type:"ratio",numerator:e.numerator,denominator:e.denominator}},lt=e=>{let r={};typeof e.emPx=="number"&&(r={exPx:Math.round(.5*e.emPx),chPx:Math.round(.5*e.emPx),capPx:Math.round(.7*e.emPx),icPx:Math.round(e.emPx)});let t={...Et,...r,...e},{widthPx:o,heightPx:n,writingMode:i,emPx:a,lhPx:s,exPx:l,chPx:p,capPx:u,icPx:c}=t,d=o/100,f=n/100;return{em:a,rem:a,lh:s,rlh:s,ex:l,ch:p,cap:u,ic:c,vw:d,vh:f,vmin:Math.min(f,d),vmax:Math.max(f,d),vi:i==="horizontal-tb"?d:f,vb:i==="horizontal-tb"?f:d,cm:37.79527559,mm:.03779527559,in:96,q:.009448818898,pc:16,pt:16}},Tt={"<":">","<=":">=",">":"<",">=":"<="},ut=(e,r)=>{if(e.context==="range"){if(xe(e.feature)){let{range:t,feature:o}=e;return t.leftToken!==void 0&&t.rightToken!==void 0?t.leftOp==="<"||t.leftOp==="<="?{type:"double",name:o,minOp:t.leftOp,min:O(t.leftToken,r),maxOp:t.rightOp,max:O(t.rightToken,r)}:{type:"double",name:o,minOp:t.rightOp===">"?"<":"<=",min:O(t.rightToken,r),maxOp:t.leftOp?"<":"<=",max:O(t.leftToken,r)}:t.rightToken===void 0?t.leftOp==="="?{type:"equals",name:o,value:O(t.leftToken,r)}:{type:"single",name:o,op:Tt[t.leftOp],value:O(t.leftToken,r)}:t.rightOp==="="?{type:"equals",name:o,value:O(t.rightToken,r)}:{type:"single",name:o,op:t.rightOp,value:O(t.rightToken,r)}}}else if(e.context==="value"){if(e.feature==="orientation"){if(e.prefix===void 0&&e.value.type==="ident"){if(e.value.value==="portrait")return{type:"single",name:"aspect-ratio",op:"<=",value:{type:"ratio",numerator:1,denominator:1}};if(e.value.value==="landscape")return{type:"single",name:"aspect-ratio",op:">=",value:{type:"ratio",numerator:1,denominator:1}}}}else if(be(e.feature)){if(e.prefix===void 0)return{type:"equals",name:e.feature,value:O(e.value,r)};if(xe(e.feature))return e.prefix==="min"?{type:"single",name:e.feature,op:">=",value:O(e.value,r)}:{type:"single",name:e.feature,op:"<=",value:O(e.value,r)}}}else{if(e.feature==="orientation")return{type:"double",name:"aspect-ratio",min:{type:"ratio",numerator:0,denominator:1},minOp:"<",maxOp:"<",max:{type:"ratio",numerator:Number.POSITIVE_INFINITY,denominator:1}};if(be(e.feature))return{type:"boolean",name:e.feature}}return{type:"invalid",name:e.feature}},z=e=>e.type==="number"&&e.value>0?[e.value,1]:e.type==="ratio"?[e.numerator,e.denominator]:null,X=(e,r)=>{let t=Q[r];if(e.type==="infinite"){if(r==="resolution")return Number.POSITIVE_INFINITY}else if(t.type==="integer"){if(e.type==="number"&&Number.isInteger(e.value))return e.value}else if(t.type==="resolution"){if(e.type==="dimension"&&e.subtype==="resolution")return e.dppx}else if(t.type==="length"){if(e.type==="dimension"&&e.subtype==="length")return e.px;if(e.type==="number"&&e.value===0)return 0}return null};var pt=(e,r)=>{let t=[];for(let o of e)for(let n of r){let i=Pt(o,n);Object.keys(i).length>0&&t.push(i)}return t},Pt=(e,r)=>{let t={};for(let o of Y(e))o[1]!==void 0&&L(t,o);for(let o of Y(r))if(o[0]in t){if(t[o[0]]!==void 0){let n=t;if(o[0]!=="media-type"){if(o[0]==="invalid-features")n[o[0]].push(...o[1]);else if(n[o[0]]==="{false}"||o[1]==="{false}")n[o[0]]="{false}";else if(n[o[0]]==="{true}")L(n,o);else if(o[1]!=="{true}"){let i=t;ge(o)||j(o)?L(i,[o[0],ae(i[o[0]],o[1])]):o[0]==="color-gamut"||o[0]==="video-color-gamut"?i[o[0]]=[i[o[0]][0]&&o[1][0],i[o[0]][1]&&o[1][1],i[o[0]][2]&&o[1][2],i[o[0]][3]&&o[1][3]]:L(i,[o[0],i[o[0]]===o[1]?i[o[0]]:"{false}"])}}}}else L(t,o);return t},ct=e=>e.map(r=>dt(r)).reduce((r,t)=>pt(r,t)),dt=e=>{let r=Y(e),t=[];for(let n of r)if(n[1]!==void 0){let i,a;if(n[0]==="invalid-features")return[{[n[0]]:n[1]}];if(n[0]==="media-type")continue;if(i=n,n[1]==="{false}")a=[[n[0],"{true}"]];else if(n[1]==="{true}")a=[[n[0],"{false}"]];else if(tt(n))if(n[0]==="color-gamut"){let s=n[1];a=[["color-gamut",[!s[0],!s[1],!s[2],!s[3]]]]}else a=n[0]==="grid"?[["grid",n[1]===0?1:0]]:Object.keys(se[n[0]]).filter(s=>s!==n[1]).map(s=>[n[0],s]);else if(j(n)){let s=at(n);a=(s==="{false}"?["{false}"]:s).map(l=>[n[0],l])}else{let s=st(n);a=(s==="{false}"?["{false}"]:s).map(l=>[n[0],l])}t.push([i,a])}let o=[];for(let[,n]of t)for(let i of n)o.push({[i[0]]:i[1]});return o},It=(e,r)=>{let t=ut(e,r),o=[{"invalid-features":[e.feature]}];if(t.type==="invalid")return o;if(t.type==="boolean")return t.name==="color-gamut"?[{"color-gamut":[!1,!0,!0,!0]}]:t.name==="grid"?[{grid:1}]:le(t.name)?dt({[t.name]:"none"}):K(t.name)?[{[t.name]:[!1,[0,1],[Number.POSITIVE_INFINITY,1],!0]}]:[{[t.name]:[!1,0,Number.POSITIVE_INFINITY,!0]}];if(le(t.name)){if(t.type==="equals"){let n=t.value;if(t.name==="grid"){if(n.type==="number"&&(n.value===0||n.value===1))return[{grid:n.value}]}else if(n.type==="ident"&&n.value in se[t.name]){if(t.name!=="color-gamut")return[{[t.name]:n.value}];{let i=["srgb","p3","rec2020"].indexOf(n.value);if(i!==-1)return[{"color-gamut":[!1,i<=0,i<=1,i<=2]}]}}}return o}if(K(t.name)){let n=null;if(t.type==="equals"){let i=z(t.value);i!==null&&(n=[!0,i,i,!0])}else if(t.type==="single"){let i=z(t.value);i!==null&&(n=t.op==="<"?[!0,[Number.NEGATIVE_INFINITY,1],i,!1]:t.op==="<="?[!0,[Number.NEGATIVE_INFINITY,1],i,!0]:t.op===">"?[!1,i,[Number.POSITIVE_INFINITY,1],!0]:[!0,i,[Number.POSITIVE_INFINITY,1],!0])}else if(t.type==="double"){let i=z(t.min),a=z(t.max);i!==null&&a!==null&&(n=[t.minOp==="<=",i,a,t.maxOp==="<="])}return n===null?o:[{[t.name]:ue([t.name,n])}]}{let n=null;if(t.type==="equals"){let i=X(t.value,t.name);i!==null&&(n=[!0,i,i,!0])}else if(t.type==="single"){let i=X(t.value,t.name);i!==null&&(n=t.op==="<"?[!0,Number.NEGATIVE_INFINITY,i,!1]:t.op==="<="?[!0,Number.NEGATIVE_INFINITY,i,!0]:t.op===">"?[!1,i,Number.POSITIVE_INFINITY,!0]:[!0,i,Number.POSITIVE_INFINITY,!0])}else if(t.type==="double"){let i=X(t.min,t.name),a=X(t.max,t.name);i!==null&&a!==null&&(n=[t.minOp==="<=",i,a,t.maxOp==="<="])}return n===null?o:[{[t.name]:ue([t.name,n])}]}},Ee=(e,r)=>{let t=[];for(let o of e.children)"context"in o?t.push(It(o,r)):t.push(Ee(o,r));return e.operator==="or"||e.operator===void 0?t.flat():e.operator==="and"?t.reduce((o,n)=>pt(o,n)):ct(t[0])},Nt=e=>{let r=[],t=new Set,o=new Set;for(let n of e){let i=!1;if(Array.isArray(n["invalid-features"])&&n["invalid-features"].length>0){for(let s of n["invalid-features"])t.add(s);i=!0}let a={};for(let s of Y(n))if(s[0]!=="invalid-features"){if(s[0]==="color-gamut"){let l=s[1].toString();l==="false,false,false,false"?s[1]="{false}":l==="true,true,true,true"&&(s[1]="{true}")}else it(s)&&(s[1]=ue(s));s[1]==="{false}"?(o.add(s[0]),i=!0):s[1]==="{true}"||s[0]==="media-type"&&s[1]==="all"||L(a,s)}i||r.push(a)}return{simplePerms:r,invalidFeatures:[...t].sort(),falseFeatures:[...o].sort()}},wt=(e,r={})=>{let t=lt(r),o=[];for(let n of e.mediaQueries){let i=[];n.prefix==="not"?(n.mediaType==="print"?i.push({"media-type":"not-print"}):n.mediaType==="screen"&&i.push({"media-type":"not-screen"}),n.mediaCondition!==void 0&&i.push(...ct(Ee(n.mediaCondition,t)))):n.mediaCondition===void 0?i.push({"media-type":n.mediaType}):i.push(...Ee(n.mediaCondition,t).map(a=>({...a,"media-type":n.mediaType}))),o.push(...i)}return Nt(o)},ft=(e,r={})=>{let t=Je(e);if(ie(t))throw new Error(`Error parsing media query list: ${t.errid} at chars ${t.start}:${t.end}`);return wt(t,r)};var kt={mediaType:"screen",anyHover:"hover",anyPointer:"fine",colorGamut:"srgb-but-not-p3",grid:"bitmap",hover:"hover",overflowBlock:"scroll",overflowInline:"scroll",pointer:"fine",scan:"progressive",update:"fast",colorIndex:"none",colorBits:8,monochromeBits:"not-monochrome",displayMode:"browser",dynamicRange:"not-hdr",environmentBlending:"opaque",forcedColors:"none",invertedColors:"none",navControls:"back",prefersColorScheme:"no-preference",prefersContrast:"no-preference",prefersReducedData:"no-preference",prefersReducedMotion:"no-preference",prefersReducedTransparency:"no-preference",scripting:"enabled",videoColorGamut:"srgb-but-not-p3",videoDynamicRange:"not-hdr",horizontalViewportSegments:1,verticalViewportSegments:1},I=e=>new Error(`Invalid option: ${e}`),Ot=e=>{if(e.mediaType!=="screen"&&e.mediaType!=="print"&&e.mediaType!=="not-screen-or-print")throw I("mediaType");if(e.anyHover!=="none"&&e.anyHover!=="hover")throw I("anyHover");if(e.anyPointer!=="none"&&e.anyPointer!=="coarse"&&e.anyPointer!=="fine")throw I("anyPointer");if(e.colorGamut!=="not-srgb"&&e.colorGamut!=="srgb-but-not-p3"&&e.colorGamut!=="p3-but-not-rec2020"&&e.colorGamut!=="rec2020")throw I("colorGamut");if(e.grid!=="bitmap"&&e.grid!=="grid")throw I("grid");if(e.hover!=="none"&&e.hover!=="hover")throw I("hover");if(e.overflowBlock!=="none"&&e.overflowBlock!=="scroll"&&e.overflowBlock!=="paged")throw I("overflowBlock");if(e.overflowInline!=="none"&&e.overflowInline!=="scroll")throw I("overflowInline");if(e.pointer!=="none"&&e.pointer!=="coarse"&&e.pointer!=="fine")throw I("pointer");if(e.scan!=="interlace"&&e.scan!=="progressive")throw I("scan");if(e.update!=="none"&&e.update!=="slow"&&e.update!=="fast")throw I("update");if(!(Number.isInteger(e.widthPx)&&e.widthPx>=0))throw I("widthPx");if(!(Number.isInteger(e.heightPx)&&e.heightPx>=0))throw I("heightPx");if(!(Number.isInteger(e.deviceWidthPx)&&e.deviceWidthPx>=0))throw I("deviceWidthPx");if(!(Number.isInteger(e.deviceHeightPx)&&e.deviceHeightPx>=0))throw I("deviceHeightPx");if(!(Number.isInteger(e.colorBits)&&e.colorBits>=0))throw I("colorBits");if(e.dppx<=0)throw I("dppx");if(e.monochromeBits!=="not-monochrome"&&!(Number.isInteger(e.monochromeBits)&&e.monochromeBits>=0))throw I("monochromeBits");if(e.colorIndex!=="none"&&!(Number.isInteger(e.colorIndex)&&e.colorIndex>=0))throw I("colorIndex")},mt=(e,r)=>{let t={...kt,...r};Ot(t);for(let o of e.simplePerms){let n=!0;for(let i in o){let a=i,s=o;if(a==="media-type"){let l=s[a];if(l==="print"){if(t.mediaType==="screen"||t.mediaType==="not-screen-or-print"){n=!1;break}}else if(l==="screen"){if(t.mediaType==="print"||t.mediaType==="not-screen-or-print"){n=!1;break}}else if(l==="not-screen"){if(t.mediaType==="screen"){n=!1;break}}else if(t.mediaType==="print"){n=!1;break}}else if(a==="any-hover"){if(s[a]!==t.anyHover){n=!1;break}}else if(a==="hover"){if(s[a]!==t.hover){n=!1;break}}else if(a==="any-pointer"){if(s[a]!==t.anyPointer){n=!1;break}}else if(a==="pointer"){if(s[a]!==t.pointer){n=!1;break}}else if(a==="grid"){let l=s[a];if(l===0&&t.grid==="grid"||l===1&&t.grid==="bitmap"){n=!1;break}}else if(a==="color-gamut"){let[l,p,u,c]=s[a];if(t.colorGamut==="not-srgb"&&!l||t.colorGamut==="srgb-but-not-p3"&&!p||t.colorGamut==="p3-but-not-rec2020"&&!u||t.colorGamut==="rec2020"&&!c){n=!1;break}}else if(a==="video-color-gamut"){let[l,p,u,c]=s[a];if(t.videoColorGamut==="not-srgb"&&!l||t.videoColorGamut==="srgb-but-not-p3"&&!p||t.videoColorGamut==="p3-but-not-rec2020"&&!u||t.videoColorGamut==="rec2020"&&!c){n=!1;break}}else if(a==="overflow-block"){if(s[a]!==t.overflowBlock){n=!1;break}}else if(a==="overflow-inline"){if(s[a]!==t.overflowInline){n=!1;break}}else if(a==="scan"){if(s[a]!==t.scan){n=!1;break}}else if(a==="update"){if(s[a]!==t.update){n=!1;break}}else if(a==="scripting"){if(s[a]!==t.scripting){n=!1;break}}else if(a==="display-mode"){if(s[a]!==t.displayMode){n=!1;break}}else if(a==="environment-blending"){if(s[a]!==t.environmentBlending){n=!1;break}}else if(a==="forced-colors"){if(s[a]!==t.forcedColors){n=!1;break}}else if(a==="inverted-colors"){if(s[a]!==t.invertedColors){n=!1;break}}else if(a==="nav-controls"){if(s[a]!==t.navControls){n=!1;break}}else if(a==="prefers-color-scheme"){if(s[a]!==t.prefersColorScheme){n=!1;break}}else if(a==="prefers-contrast"){if(s[a]!==t.prefersContrast){n=!1;break}}else if(a==="prefers-reduced-data"){if(s[a]!==t.prefersReducedData){n=!1;break}}else if(a==="prefers-reduced-motion"){if(s[a]!==t.prefersReducedMotion){n=!1;break}}else if(a==="prefers-reduced-transparency"){if(s[a]!==t.prefersReducedTransparency){n=!1;break}}else if(a==="dynamic-range"){if(s[a]==="high"&&t.dynamicRange==="not-hdr"){n=!1;break}}else if(a==="video-dynamic-range"){if(s[a]==="high"&&t.videoDynamicRange==="not-hdr"){n=!1;break}}else if(a==="vertical-viewport-segments"){let[l,p,u,c]=s[a];if(t.verticalViewportSegmentsu||p===t.verticalViewportSegments&&!l||u===t.verticalViewportSegments&&!c){n=!1;break}}else if(a==="horizontal-viewport-segments"){let[l,p,u,c]=s[a];if(t.horizontalViewportSegmentsu||p===t.horizontalViewportSegments&&!l||u===t.horizontalViewportSegments&&!c){n=!1;break}}else if(a==="width"){let[l,p,u,c]=s[a];if(t.widthPxu||p===t.widthPx&&!l||u===t.widthPx&&!c){n=!1;break}}else if(a==="device-width"){let[l,p,u,c]=s[a];if(t.deviceWidthPxu||p===t.deviceWidthPx&&!l||u===t.deviceWidthPx&&!c){n=!1;break}}else if(a==="height"){let[l,p,u,c]=s[a];if(t.heightPxu||p===t.heightPx&&!l||u===t.heightPx&&!c){n=!1;break}}else if(a==="device-height"){let[l,p,u,c]=s[a];if(t.deviceHeightPxu||p===t.deviceHeightPx&&!l||u===t.deviceHeightPx&&!c){n=!1;break}}else if(a==="color"){let[l,p,u,c]=s[a];if(t.colorBitsu||p===t.colorBits&&!l||u===t.colorBits&&!c){n=!1;break}}else if(a==="monochrome"){let[l,p,u,c]=s[a];if(t.monochromeBits==="not-monochrome")(p>0||p===0&&!l||u===0&&!c)&&(n=!1);else if(t.monochromeBitsu||p===t.monochromeBits&&!l||u===t.monochromeBits&&!c){n=!1;break}}else if(a==="resolution"){let[l,p,u,c]=s[a];if(t.dppxu||p===t.dppx&&!l||u===t.dppx&&!c){n=!1;break}}else if(a==="color-index"){let[l,p,u,c]=s[a];if(t.colorIndex==="none"){if(p>0||p===0&&!l||u===0&&!c){n=!1;break}}else if(t.colorIndexu||p===t.colorIndex&&!l||u===t.colorIndex&&!c){n=!1;break}}else if(a==="aspect-ratio"){let[l,p,u,c]=s[a],d=p[0]/p[1],f=u[0]/u[1],m=t.widthPx/t.heightPx;if(mf||d===m&&!l||f===m&&!c){n=!1;break}}else{let[l,p,u,c]=s[a],d=p[0]/p[1],f=u[0]/u[1],m=t.deviceWidthPx/t.deviceHeightPx;if(mf||d===m&&!l||f===m&&!c){n=!1;break}}}if(n)return!0}return!1};function _t(e){let{useObjectSyntax:r=!1}=e??{};return t=>{let o=r?[":where([style]) {}"]:[],n=[];Rt(t,(u,c)=>{u.type===Ct&&u.name==="style"&&(o.push(u.children.map(d=>d.type===At?d.value:"").join("")),n.push(()=>{c.children=c.children.filter(d=>d!==u)}))});for(let u of n)u();let i=o.join(` `),a=de(i),s=new Map;function l(u){if(u.type==="rule"){let c=Object.fromEntries(u.children.map(d=>[d.props,d.children]));for(let d of u.props){let f=Object.assign(s.get(d)??{},c);s.set(d,f)}}else if(u.type==="@media"&&(e==null?void 0:e.env)){let c=Mt(e.env),f=(Array.isArray(u.props)?u.props:[u.props]).map(m=>ft(m));for(let m of f)if(mt(m,c)){for(let v of u.children)l(v);return}}}for(let u of a)l(u);let p=new Map;for(let[u,c]of Array.from(s).sort(([d],[f])=>{let m=ht(d),v=ht(f);return m>v?1:v>m?-1:0})){let d=St(t,u);for(let f of d){let m=p.get(f)??{};p.set(f,Object.assign(m,c))}}for(let[u,c]of p){let d=u.attributes.style??"",f={};for(let m of de(d))m.type==="decl"&&typeof m.props=="string"&&typeof m.children=="string"&&(f[m.props]=m.children);f=Object.assign({},c,f),r?u.attributes.style=f:u.attributes.style=`${Object.entries(f).map(([m,v])=>`${m}:${v.replace("!important","")};`).join("")}`}return t}}function Mt(e){let{width:r,height:t,dppx:o=1,widthPx:n=r,heightPx:i=t,deviceWidthPx:a=r*o,deviceHeightPx:s=t*o,...l}=e;return{widthPx:n,heightPx:i,deviceWidthPx:a,deviceHeightPx:s,dppx:o,...l}}export{_t as default}; /**! media-query-parser | Tom Golden (https://tom.bio) | @license MIT */