2 lines
6.9 KiB
JavaScript
2 lines
6.9 KiB
JavaScript
|
import e from"postcss-value-parser";import{parse as t,addLayerToModel as r}from"@csstools/cascade-layer-name-parser";const n=t("csstools-implicit-layer")[0];function collectCascadeLayerOrder(e){const o=new Map,s=new Map,a=[];e.walkAtRules((e=>{var l;if("layer"!==e.name.toLowerCase())return;{let t=e.parent;for(;t;){if("atrule"!==t.type||"layer"!==t.name.toLowerCase()){if(t===e.root())break;return}t=t.parent}}let i;if(e.nodes)i=normalizeLayerName(e.params,1);else{if(!e.params.trim())return;i=e.params}let c=t(i);if(null!=(l=c)&&l.length){{let t=e.parent;for(;t&&"atrule"===t.type&&"layer"===t.name.toLowerCase();){const e=s.get(t);e?(c=c.map((t=>e.concat(t))),t=t.parent):t=t.parent}}if(r(a,c),e.nodes){const t=c[0].concat(n);o.set(e,t),s.set(e,c[0])}}}));for(const e of o.values())r(a,[e]);const l=new WeakMap;for(const[e,t]of o)l.set(e,a.findIndex((e=>t.equal(e))));return l}function normalizeLayerName(e,t){return e.trim()?e:"csstools-anon-layer--"+t++}const o=/(!\s*)?postcss-custom-properties:\s*off\b/i,s=new WeakMap;function isBlockIgnored(e){if(!e||!e.nodes)return!1;if(s.has(e))return s.get(e);const t=e.some((e=>isIgnoreComment(e,o)));return s.set(e,t),t}const a=/(!\s*)?postcss-custom-properties:\s*ignore\s+next\b/i;function isDeclarationIgnored(e){return!!e&&(!!isBlockIgnored(e.parent)||isIgnoreComment(e.prev(),a))}function isIgnoreComment(e,t){return!!e&&"comment"===e.type&&t.test(e.text)}const l=new Set(["layer"]);function isProcessableRule(e){let t=e.parent;for(;t;){if("atrule"===t.type&&!l.has(t.name.toLowerCase()))return!1;t=t.parent}return!0}const i=/^html$/i,c=/^:where\(html\)$/i,u=/^:root$/i,p=/^:where\(:root\)$/i,f=/(html|:root)/i,d=/^var$/i;function isVarFunction(e){return"function"===e.type&&d.test(e.value)&&Object(e.nodes).length>0}const v=/var\(/i;function removeCyclicReferences(e,t){const r=new Set;let n=t;for(;e.size>0;)try{toposort(Array.from(e.keys()),n);break}catch(t){if(!t._graphNode)throw t;e.delete(t._graphNode),r.add(t._graphNode),n=n.filter((e=>-1===e.indexOf(t._graphNode)))}return r}function toposort(e,t){let r=e.length;const n=new Array(r),o={};let s=r;const a=makeOutgoingEdges(t),l=makeNodesHash(e);for(;s--;)o[s]||visit(e[s],s,new Set);return n;function visit(e,t,s){if(s.has(e)){const t=new Error("Cyclic dependency"+JSON.stringify(e));throw t._graphNode=e,t}if(!l.has(e))return;if(o[t])return;o[t]=!0;let i=a.get(e)||new Set;if(i=Array.from(i),t=i.length){s.add(e);do{const e=i[--t];visit(e,l.get(e),s)}while(t);s.delete(e)}n[--r]=e}}function makeOutgoingEdges(e){const t=new Map;for(let r=0,n=e.length;r<n;r++){const n=e[r];t.has(n[0])||t.set(n[0],new Set),t.has(n[1])||t.set(n[1],new Set),t.get(n[0]).add(n[1])}return t}function makeNodesHash(e){const t=new Map;for(let r=0,n=e.length;r<n;r++)t.set(e[r],r);return t}function parseOrCached(t,r){let n=r.get(t);return n||(n=e(t),r.set(t,n),n)}function getCustomPropertiesFromRoot(t,r){const n=new Map,o=new Map,s=collectCascadeLayerOrder(t);t.walkRules((e=>{var t;f.test(e.selector)&&null!=(t=e.nodes)&&t.length&&isProcessableRule(e)&&(isBlockIgnored(e)||e.selectors.forEach((t=>{let r=-1;if(c.test(t)||p.test(t))r=0;else if(i.test(t))r=1;else{if(!u.test(t))return;r=2}const a=(f=s,((l=e).parent&&"atrule"===l.parent.type&&"layer"===l.parent.name.toLowerCase()?f.has(l.parent)?f.get(l.parent)+1:0:1e7)+10+r);var l,f;e.each((e=>{if("decl"!==e.type)return;if(!e.variable||isDeclarationIgnored(e))return;if("initial"===e.value.toLowerCase().trim())return;const t=o.get(e.prop)??-1;a>=t&&(o.set(e.prop,a),n.set(e.prop,e.value))}))})))}));const a=[],l=new Map;for(const[t,o]of n.entries()){const n=parseOrCached(o,r);e.walk(n.nodes,(e=>{if(isVarFunction(e)){const[r]=e.nodes.filter((e=>"word"===e.type));a.push([r.value,t])}})),l.set(t,n)}return removeCyclicReferences(l,a),l}function transformValueAST(t,r,n){var o;if(null==(o=t.nodes)||!o.length)return"";const s=new Map;return t.nodes.forEach((e=>{s.set(e,t)})),e.walk(t.nodes,(e=>{"nodes"in e&&e.nodes.length&&e.nodes.forEach((t=>{s.set(t,e)}))})),e.walk(t.nodes,(t=>{var o,a;if(!isVarFunction(t))return;const[l,...i]=t.nodes.filter((
|