-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqfill.min.js
2 lines (2 loc) · 7.19 KB
/
cqfill.min.js
1
2
var l;(function(r){r[r.LESS_THAN=0]="LESS_THAN",r[r.LESS_OR_EQUAL=1]="LESS_OR_EQUAL",r[r.GREATER_THAN=2]="GREATER_THAN",r[r.GREATER_OR_EQUAL=3]="GREATER_OR_EQUAL"})(l||(l={}));var d;(function(r){r[r.SizeQuery=0]="SizeQuery",r[r.ContainerConditionConjunction=1]="ContainerConditionConjunction",r[r.ContainerConditionDisjunction=2]="ContainerConditionDisjunction",r[r.ContainerConditionNegation=3]="ContainerConditionNegation"})(d||(d={}));function E(){return Array.from({length:16},()=>Math.floor(Math.random()*256).toString(16)).join("")}function b(e){switch(e.toLowerCase()){case"inlinesize":return"inlineSize";case"blocksize":return"blockSize";case"width":return"inlineSize";case"height":return"blockSize";default:throw Error(`Unknown feature name ${e} in container query`)}}function j(e,n){let t=n[b(e.feature)];switch(e.comparator){case 3:return t>=e.threshold;case 2:return t>e.threshold;case 1:return t<=e.threshold;case 0:return t<e.threshold}}function h(e,n){switch(e.type){case 1:return h(e.left,n)&&h(e.right,n);case 2:return h(e.left,n)||h(e.right,n);case 3:return!h(e.right,n);case 0:return j(e,n);default:throw Error("wtf?")}}function q(e,n){let t;if("borderBoxSize"in n)t=n.borderBoxSize?.[0]??n.borderBoxSize;else{let i=getComputedStyle(n.target);t={blockSize:n.contentRect.height,inlineSize:n.contentRect.width},t.blockSize+=parseInt(i.paddingBlockStart.slice(0,-2))+parseInt(i.paddingBlockEnd.slice(0,-2)),t.inlineSize+=parseInt(i.paddingInlineStart.slice(0,-2))+parseInt(i.paddingInlineEnd.slice(0,-2))}return h(e,t)}function D(e,n){for(;e;)if(e=e.parentElement,!!m.has(e)&&!(n&&!m.get(e).includes(n)))return e;return null}var m=new WeakMap;function g(e,n){w.observe(e),m.has(e)||m.set(e,[]),m.get(e).push(n)}var A=[];function $(e){A.push(e)}var w=new ResizeObserver(e=>{let n=new Map(e.map(t=>[t.target,t]));for(let t of A)for(let{selector:i}of t.rules){let r=document.querySelectorAll(i);for(let a of r){let u=D(a,t.name);if(!u||!n.has(u))continue;let f=n.get(u);a.classList.toggle(t.className,q(t.condition,f))}}}),R=[],G=new MutationObserver(e=>{for(let n of e){for(let t of n.removedNodes)t instanceof HTMLElement&&w.unobserve(t);for(let t of n.addedNodes)if(t instanceof HTMLElement)for(let i of R){t.matches(i.selector)&&g(t,i.name);for(let r of t.querySelectorAll(i.selector))g(r,i.name)}}});G.observe(document.documentElement,{childList:!0,subtree:!0});function C(e,n){let t={sheetSrc:e,index:0,name:n};for(;t.index<t.sheetSrc.length&&(o(t),!(t.index>=t.sheetSrc.length));){if(c("/*",t)){for(;c("/*",t);)B(t),o(t);continue}if(c("@container",t)){let{query:i,startIndex:r,endIndex:a}=Z(t),u=p(i);L(r,a,u,t),$(i)}else{let i=Q(t);if(!i)continue;W(i,t)}}return n&&(t.sheetSrc=t.sheetSrc.replace(/url\(["']*([^)"']+)["']*\)/g,(i,r)=>`url(${new URL(r,n)})`)),t.sheetSrc}function W(e,n){let t=e.block.contents.includes("container-"),i=e.block.contents.includes("container:");if(!t&&!i)return;let r,a;t&&(r=/container-name\s*:([^;}]+)/.exec(e.block.contents)?.[1].trim(),e.block.contents=e.block.contents.replace("container-type","contain")),i&&([a,r]=(/container\s*:([^;}]+)/.exec(e.block.contents)?.[1]).split("/").map(f=>f.trim()),e.block.contents=e.block.contents.replace(/container: ([^;}]+)/,`contain: ${a};`)),r||(r=E()),L(e.block.startIndex,e.block.endIndex,e.block.contents,n),R.push({name:r,selector:e.selector});for(let u of document.querySelectorAll(e.selector))g(u,r)}function L(e,n,t,i){if(i.sheetSrc=i.sheetSrc.slice(0,e)+t+i.sheetSrc.slice(n),i.index>=n){let r=i.index-n;i.index=e+t.length+r}}function B(e){s(e,"/*"),z("*/",e),s(e,"*/")}function k(e){if(e.index++,e.index>e.sheetSrc.length)throw S(e,"Advanced beyond the end")}function z(e,n){let t=n.index;for(;!c(e,n);)k(n);return n.sheetSrc.slice(t,n.index)}function c(e,n){return n.sheetSrc.substr(n.index,e.length)==e}function F(e){let n=e.index;if(z("{",e),n===e.index)throw Error("Empty selector");return e.sheetSrc.slice(n,e.index)}function Q(e){let n=e.index,t=F(e);if(!t)return;let i=V(e),r=e.index;return{selector:t,block:i,startIndex:n,endIndex:r}}function J(e){return e.name?e.name:"<anonymous file>"}function S(e,n){return Error(`(${J(e)}): ${n}`)}function s(e,n){if(e.sheetSrc.substr(e.index,n.length)!=n)throw S(e,`Did not find expected sequence ${n}`);e.index+=n.length}var P=/\s*/g;function o(e){P.lastIndex=e.index;let n=P.exec(e.sheetSrc);n&&(e.index+=n[0].length)}function N(e){return e.sheetSrc[e.index]}var I=/[\w\\\@_-]+/g;function y(e){I.lastIndex=e.index;let n=I.exec(e.sheetSrc);if(!n)throw S(e,"Expected an identifier");return e.index+=n[0].length,n[0]}function K(e){return y(e).toLowerCase()}var _=/[0-9.]*/g;function M(e){_.lastIndex=e.index;let n=_.exec(e.sheetSrc);if(!n)throw S(e,"Expected a number");e.index+=n[0].length,s(e,"px");let t=parseFloat(n[0]);if(Number.isNaN(t))throw S(e,`${n[0]} is not a valid number`);return t}function V(e){let n=e.index;s(e,"{");let t=1;for(;t!=0;)e.sheetSrc[e.index]==="{"?t++:e.sheetSrc[e.index]==="}"&&t--,k(e);let i=e.index,r=e.sheetSrc.slice(n,i);return{startIndex:n,endIndex:i,contents:r}}function X(e){let n=K(e);o(e),s(e,":"),o(e);let t=M(e);o(e),s(e,")"),o(e);let i;if(n.startsWith("min-"))i=3;else if(n.startsWith("max-"))i=1;else throw Error(`Unknown legacy container query ${n}`);return{type:0,feature:b(n.slice(4)),comparator:i,threshold:t}}function Y(e){if(c(">=",e))return s(e,">="),3;if(c(">",e))return s(e,">"),2;if(c("<=",e))return s(e,"<="),1;if(c("<",e))return s(e,"<"),0;throw Error("Unknown comparator")}function O(e){if(s(e,"("),c("(",e)){let r=v(e);return s(e,")"),r}if(o(e),c("min-",e)||c("max-",e))return X(e);let n=y(e).toLowerCase();o(e);let t=Y(e);o(e);let i=M(e);return o(e),s(e,")"),o(e),{type:0,feature:n,comparator:t,threshold:i}}function T(e){if(o(e),c("(",e))return O(e);if(c("size",e))return s(e,"size"),o(e),O(e);throw c("style",e)?Error("Style query not implement yet"):Error("Unknown container query type")}function x(e){return c("not",e)?(s(e,"not"),o(e),{type:3,right:T(e)}):T(e)}function v(e){let n=x(e);for(;;)if(c("and",e)){s(e,"and"),o(e);let t=x(e);o(e),n={type:1,left:n,right:t}}else if(c("or",e)){s(e,"or"),o(e);let t=x(e);o(e),n={type:2,left:n,right:t}}else break;return n}function Z(e){let n=e.index;s(e,"@container"),o(e);let t="";N(e)!=="("&&!c("size",e)&&!c("style",e)&&(t=y(e),o(e));let i=v(e);o(e),s(e,"{"),o(e);let r=[];for(;N(e)!=="}";)r.push(Q(e)),o(e);s(e,"}");let a=e.index;o(e);let u=`cq_${E()}`;return{query:{condition:i,className:u,name:t,rules:r},startIndex:n,endIndex:a}}function p(e){return e.rules.map(n=>`:is(${n.selector}).${e.className} ${n.block.contents}`).join(`
`)}function te(){if("container"in document.documentElement.style)return!1;new MutationObserver(r=>{for(let a of r)for(let u of a.addedNodes)u instanceof HTMLStyleElement&&t(u),u instanceof HTMLLinkElement&&i(u)}).observe(document.documentElement,{childList:!0,subtree:!0});function t(r){if(r.innerHTML.trim().length===0)return;let a=C(r.innerHTML);r.innerHTML=a}async function i(r){if(r.rel!=="stylesheet")return;let a=new URL(r.href,document.baseURI);if(a.origin!==location.origin)return;let u=await fetch(a.toString()).then(U=>U.text()),f=C(u,a.toString()),H=new Blob([f],{type:"text/css"});r.href=URL.createObjectURL(H)}document.querySelectorAll("style").forEach(r=>t(r)),document.querySelectorAll("link").forEach(r=>i(r))}export{te as init,C as transpileStyleSheet};