2 lines
8.2 KiB
JavaScript
2 lines
8.2 KiB
JavaScript
|
import{parse as e,addLayerToModel as r}from"@csstools/cascade-layer-name-parser";import{tokenizer as t,TokenType as n,NumberType as a,cloneTokens as o,stringify as s}from"@csstools/css-tokenizer";import{parseFromTokens as i,parse as l,isMediaFeatureBoolean as u,isMediaFeature as c,newMediaFeaturePlain as p,isMediaQueryInvalid as f,isMediaQueryWithType as m,isMediaAnd as d,isMediaOr as h,isMediaNot as y,isMediaConditionList as g,isGeneralEnclosed as v}from"@csstools/media-query-list-parser";const w=e("csstools-implicit-layer")[0];function collectCascadeLayerOrder(t){const n=new Map,a=new Map,o=[];t.walkAtRules((t=>{var s;if("layer"!==t.name.toLowerCase())return;{let e=t.parent;for(;e;){if("atrule"!==e.type||"layer"!==e.name.toLowerCase()){if(e===t.root())break;return}e=e.parent}}let i;if(t.nodes)i=normalizeLayerName(t.params,1);else{if(!t.params.trim())return;i=t.params}let l=e(i);if(null!=(s=l)&&s.length){{let e=t.parent;for(;e&&"atrule"===e.type&&"layer"===e.name.toLowerCase();){const r=a.get(e);r?(l=l.map((e=>r.concat(e))),e=e.parent):e=e.parent}}if(r(o,l),t.nodes){const e=l[0].concat(w);n.set(t,e),a.set(t,l[0])}}}));for(const e of n.values())r(o,[e]);const s=new WeakMap;for(const[e,r]of n)s.set(e,o.findIndex((e=>r.equal(e))));return s}function normalizeLayerName(e,r){return e.trim()?e:"csstools-anon-layer--"+r++}const C=new Set(["scope","container","layer"]);function isProcessableCustomMediaRule(e){if("custom-media"!==e.name.toLowerCase())return!1;if(!e.params||!e.params.includes("--"))return!1;if(e.nodes&&e.nodes.length>0)return!1;let r=e.parent;for(;r;){if("atrule"===r.type&&!C.has(r.name.toLowerCase()))return!1;r=r.parent}return!0}function removeCyclicReferences(e,r){const t=new Set;let n=r;for(;e.size>0;)try{toposort(Array.from(e.keys()),n);break}catch(r){if(!r._graphNode)throw r;e.delete(r._graphNode),t.add(r._graphNode),n=n.filter((e=>-1===e.indexOf(r._graphNode)))}return t}function toposort(e,r){let t=e.length;const n=new Array(t),a={};let o=t;const s=makeOutgoingEdges(r),i=makeNodesHash(e);for(;o--;)a[o]||visit(e[o],o,new Set);return n;function visit(e,r,o){if(o.has(e)){const r=new Error("Cyclic dependency"+JSON.stringify(e));throw r._graphNode=e,r}if(!i.has(e))return;if(a[r])return;a[r]=!0;let l=s.get(e)||new Set;if(l=Array.from(l),r=l.length){o.add(e);do{const e=l[--r];visit(e,i.get(e),o)}while(r);o.delete(e)}n[--t]=e}}function makeOutgoingEdges(e){const r=new Map;for(let t=0,n=e.length;t<n;t++){const n=e[t];r.has(n[0])||r.set(n[0],new Set),r.has(n[1])||r.set(n[1],new Set),r.get(n[0]).add(n[1])}return r}function makeNodesHash(e){const r=new Map;for(let t=0,n=e.length;t<n;t++)r.set(e[t],t);return r}function atMediaParamsTokens(e){const r=t({css:e},{onParseError:()=>{throw new Error(`Unable to parse media query "${e}"`)}}),n=[];for(;!r.endOfFile();)n.push(r.nextToken());return n}const W=[[n.Ident,"max-color",0,0,{value:"max-color"}],[n.Colon,":",0,0,void 0],[n.Number,"2147477350",0,0,{value:2147477350,type:a.Integer}]],k=[[n.Ident,"color",0,0,{value:"color"}],[n.Colon,":",0,0,void 0],[n.Number,"2147477350",0,0,{value:2147477350,type:a.Integer}]];function replaceTrueAndFalseTokens(e){let r,t=[];for(let a=0;a<e.length;a++)if(e[a][0]!==n.Comment&&e[a][0]!==n.Whitespace){if(e[a][0]===n.Ident){const n=e[a];if("true"===n[4].value.toLowerCase()){r="true",t=e.slice(a+1);break}if("false"===n[4].value.toLowerCase()){r="false",t=e.slice(a+1);break}}return e}if(!r)return e;for(let r=0;r<t.length;r++)if(t[r][0]!==n.Comment&&t[r][0]!==n.Whitespace)return e;return"true"===r?[[n.Whitespace," ",0,0,void 0],[n.OpenParen,"(",0,0,void 0],...W,[n.CloseParen,")",0,0,void 0]]:[[n.Whitespace," ",0,0,void 0],[n.OpenParen,"(",0,0,void 0],...k,[n.CloseParen,")",0,0,void 0]]}function parseCustomMedia(e){const r=atMediaParamsTokens(e),t=new Set;let a="",l=r;for(let e=0;e<r.length;e++)if(r[e][0]!==n.Comment&&r[e][0]!==n.Whitespace){if(r[e][0]===n.Ident){const t=r[e];if(t[4].value.startsWith("--")){a=t[4].value,l=r.slice(e+1);break}}return!1}for(let e=0;e<l.length;e++)if(l[e][0]===n.Ident){const r=l[e];r[4].value.startsWith("--")&&t.add(r[
|