epiphany/node_modules/postcss-custom-properties/dist/index.mjs
2023-12-09 22:48:07 -08:00

1 line
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((e=>"div"!==e.type)),{value:c}=l,u=s.get(t);if(!u)return;const p=u.nodes.indexOf(t);if(-1===p)return;let f=!1;i&&e.walk(i,(e=>{if(isVarFunction(e)){const[t]=e.nodes.filter((e=>"word"===e.type));if(r.has(t.value)||n.has(t.value))return;return f=!0,!1}}));let d=(null==(o=n.get(c))?void 0:o.nodes)??(null==(a=r.get(c))?void 0:a.nodes);d||!i.length||f||(d=t.nodes.slice(t.nodes.indexOf(i[0]))),void 0!==d&&(d.length?(u.nodes.splice(p,1,...d),u.nodes.forEach((e=>s.set(e,u)))):(u.nodes.splice(p,1,{type:"comment",value:"",sourceIndex:t.sourceIndex,sourceEndIndex:t.sourceEndIndex}),u.nodes.forEach((e=>s.set(e,u)))))}),!0),e.stringify(t.nodes)}function transformProperties(t,r,n,o,s){if(isTransformableDecl(t)&&!isDeclarationIgnored(t)){const o=t.value;let i=transformValueAST(e(o),r,n);const c=new Set;for(;v.test(i)&&!c.has(i);){c.add(i);i=transformValueAST(e(i),r,n)}if(i!==o){if(parentHasExactFallback(t,i))return void(s.preserve||t.remove());if(s.preserve){var a;const e=t.cloneBefore({value:i});hasTrailingComment(e)&&null!=(a=e.raws)&&a.value&&(e.raws.value.value=e.value.replace(w,"$1"),e.raws.value.raw=e.raws.value.value+e.raws.value.raw.replace(w,"$2"))}else{var l;t.value=i,hasTrailingComment(t)&&null!=(l=t.raws)&&l.value&&(t.raws.value.value=t.value.replace(w,"$1"),t.raws.value.raw=t.raws.value.value+t.raws.value.raw.replace(w,"$2"))}}}}const isTransformableDecl=e=>!e.variable&&e.value.includes("--")&&e.value.toLowerCase().includes("var("),hasTrailingComment=e=>{var t,r;return"value"in Object(Object(e.raws).value)&&"raw"in((null==(t=e.raws)?void 0:t.value)??{})&&w.test((null==(r=e.raws.value)?void 0:r.raw)??"")},w=/^([\W\w]+)(\s*\/\*[\W\w]+?\*\/)$/;function parentHasExactFallback(e,t){if(!e||!e.parent)return!1;let r=!1;const n=e.parent.index(e);return e.parent.each(((o,s)=>o!==e&&(!(s>=n)&&void("decl"===o.type&&o.prop.toLowerCase()===e.prop.toLowerCase()&&o.value===t&&(r=!0))))),r}function hasSupportsAtRuleAncestor(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&/([^\w]var\()|(\(top: var\(--f\))/i.test(t.params))return!0;t=t.parent}else t=t.parent;return!1}const m=/^initial$/i,creator=e=>{const t=!("preserve"in Object(e))||Boolean(null==e?void 0:e.preserve);if("importFrom"in Object(e))throw new Error('[postcss-custom-properties] "importFrom" is no longer supported');if("exportTo"in Object(e))throw new Error('[postcss-custom-properties] "exportTo" is no longer supported');return{postcssPlugin:"postcss-custom-properties",prepare:()=>{let e=new Map;const r=new Map;return{Once:t=>{e=getCustomPropertiesFromRoot(t,r)},Declaration:n=>{if(!v.test(n.value))return;if(hasSupportsAtRuleAncestor(n))return;const o=new Map;t&&n.parent&&n.parent.each((e=>{"decl"===e.type&&e.variable&&n!==e&&(isDeclarationIgnored(e)||(m.test(e.value)?o.delete(e.prop):o.set(e.prop,parseOrCached(e.value,r))))})),transformProperties(n,e,o,0,{preserve:t})}}}}};creator.postcss=!0;export{creator as default};