You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
8.4 KiB
1 lines
8.4 KiB
import e from"postcss-selector-parser";import{selectorSpecificity as r}from"@csstools/selector-specificity";const a=["media","supports"],t=["keyframes"];class s{constructor(){this.anonymousLayerCount=0,this.layerCount=0,this.anonymousLayerCount=0,this.layerCount=0,this.layerOrder=new Map,this.layerParamsParsed=new Map,this.layerNameParts=new Map}createAnonymousLayerName(){const e=`anonymous-${this.anonymousLayerCount}-6efdb677-bb05-44e5-840f-29d2175862fd`;return this.addLayerNameParts(e),this.layerParamsParsed.set(e,[e]),this.anonymousLayerCount++,e}createImplicitLayerName(e){const r=this.layerNameParts.get(e),a=`implicit-${r[r.length-1]}-b147acf6-11a6-4338-a4d0-80aef4cd1a2f`;return this.addLayerNameParts([...r,a]),this.layerParamsParsed.set(a,[a]),a}addLayerParams(e,r){r?"string"!=typeof r?this.layerParamsParsed.set(e,r):this.layerParamsParsed.set(e,[r]):this.layerParamsParsed.set(e,[e])}addLayerNameParts(e){"string"!=typeof e?this.layerNameParts.set(e.join("."),e):this.layerNameParts.set(e,[e])}getLayerParams(e){const r=[...this.layerParamsParsed.get(e.params)];let a=e.parent;for(;a;)"atrule"===a.type?("layer"===a.name&&r.push(...this.layerParamsParsed.get(a.params)),a=a.parent):a=a.parent;return r.reverse(),r.flatMap((e=>this.layerNameParts.get(e)))}getLayerNameList(e){const r=this.layerNameParts.get(e),a=[];for(let e=0;e<r.length;e++){const t=r.slice(0,e+1).join(".");this.layerParamsParsed.has(t)||this.layerParamsParsed.set(t,[t]),this.layerNameParts.has(t)||this.layerNameParts.set(t,r.slice(0,e+1)),a.push(r.slice(0,e+1).join("."))}return a}sortLayerNames(){for(const[e,r]of this.layerOrder){const a=this.layerNameParts.get(e);for(let e=1;e<a.length;e++){const t=a.slice(0,e).join(".");this.layerOrder.has(t)||this.layerOrder.set(t,r)}}let e=Array.from(this.layerOrder.entries());e=e.sort(((e,r)=>{const a=this.layerNameParts.get(e[0]),t=this.layerNameParts.get(r[0]);if(a[0]!==t[0])return this.layerOrder.get(a[0])-this.layerOrder.get(t[0]);const s=Math.max(a.length,t.length);for(let e=0;e<s;e++){const r=a[e],s=t[e];if(r!==s)return r?s?this.layerOrder.get(a.slice(0,e).join("."))-this.layerOrder.get(t.slice(0,e).join(".")):-1:1}})),this.layerOrder.clear(),e.forEach(((e,r)=>{this.layerOrder.set(e[0],r)}))}}function n(r,a){const t=e().astSync(r),s=e().astSync(function(e){if(0===e)return"";let r="";for(let a=0;a<e;a++)r+=":not(#\\#)";return r}(a));let n=!1;for(let r=0;r<t.nodes[0].nodes.length;r++)if("combinator"===t.nodes[0].nodes[r].type||e.isPseudoElement(t.nodes[0].nodes[r])){t.nodes[0].insertBefore(t.nodes[0].nodes[r],s),n=!0;break}return n||t.nodes[0].insertAfter(t.nodes[0].nodes[t.nodes[0].nodes.length-1],s),t.toString()}function l(e,r){let a=!1;return e.walk((e=>{if(r(e))return a=!0,!1})),a}function o(e,r){let a=!1;return e.walkAtRules((e=>{if(r(e))return a=!0,!1})),a}function i(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("layer"===r.name)return r;r=r.parent}else r=r.parent;return null}function y(e){e.walk((e=>{("rule"===e.type||"atrule"===e.type&&["layer",...a].includes(e.name))&&(e.nodes&&e.nodes.length||e.remove())})),e.nodes&&e.nodes.length||e.remove()}function m(e){let r=e;for(;r;){if(r.nodes&&r.nodes.length>0)return;const e=r.parent;r.remove(),r=e}}function u(e,r,{result:t,options:s}){e.walkAtRules("layer",(e=>{const n=r.getLayerParams(e),l=n.join(".");r.layerOrder.has(l)||(s.onConditionalRulesChangingLayerOrder&&function(e){let r=e.parent;for(;r;)if("atrule"===r.type){if(a.includes(r.name))return r;r=r.parent}else r=r.parent;return null}(e)&&!e.params.endsWith("b147acf6-11a6-4338-a4d0-80aef4cd1a2f")&&!e.params.endsWith("6efdb677-bb05-44e5-840f-29d2175862fd")&&e.warn(t,"handling different layer orders in conditional rules is unsupported by this plugin and will cause style differences between browser versions."),r.layerParamsParsed.has(l)||r.layerParamsParsed.set(l,[l]),r.layerNameParts.has(l)||r.layerNameParts.set(l,[...n]),r.getLayerNameList(l).forEach((e=>{r.layerOrder.has(e)||(r.layerOrder.set(e,r.layerCount),r.layerCount+=1)}))),e.nodes&&0!==e.nodes.length||e.remove()}))}const p=p=>{const c=Object.assign({onRevertLayerKeyword:"warn",onConditionalRulesChangingLayerOrder:"warn",onImportLayerRule:"warn"},p);return{postcssPlugin:"postcss-cascade-layers",OnceExit(p,{result:d}){c.onRevertLayerKeyword&&p.walkDecls((e=>{"revert-layer"===e.value&&e.warn(d,'handling "revert-layer" is unsupported by this plugin and will cause style differences between browser versions.')})),c.onImportLayerRule&&p.walkAtRules("import",(e=>{e.params.includes("layer")&&e.warn(d,"To use @import with layers, the postcss-import plugin is also required. This plugin alone will not support using the @import at-rule.")})),function(e){e.walkDecls((e=>{if(!e.important)return;const r=e.parent;if(r.parent&&"atrule"===r.parent.type&&t.includes(r.parent.name))return;const a=r.clone();a.each((e=>{"decl"===e.type&&e.important||e.remove()})),r.each((e=>{"decl"===e.type&&e.important&&e.remove()})),r.before(a),y(r)}))}(p);const f=new s;if(function(r,a){r.walkAtRules("layer",(r=>{if(r.params){const t=[];let s=!1;if(e().astSync(r.params).each((e=>{const r=[];e.walk((e=>{switch(e.type){case"class":case"tag":r.push(e.value);break;default:s=!0}})),s||(t.push(r.join(".")),a.addLayerNameParts(r))})),a.addLayerParams(r.params,t),r.nodes&&t.length>1&&(s=!0),s)return void(r.name="csstools-invalid-layer");if(!r.nodes||0===r.nodes.length){if(t.length<=1)return;return t.slice(0,-1).forEach((e=>{a.addLayerParams(e,e),r.cloneBefore({params:e})})),a.addLayerParams(t[t.length-1],t[t.length-1]),void(r.params=t[t.length-1])}}r.params||(r.raws.afterName=" ",r.params=a.createAnonymousLayerName());const t=o(r,(e=>"layer"===e.name)),s=l(r,(e=>{if("rule"===e.type)return i(e)===r}));if(t&&s){const e=a.createImplicitLayerName(r.params),t=r.clone({params:e});t.walkAtRules("layer",(e=>{e.remove()})),r.walk((e=>{"rule"===e.type&&i(e)===r&&e.remove()})),r.append(t),y(r),m(r)}}))}(p,f),u(p,f,{result:d,options:c}),!f.layerCount)return void p.walkAtRules("csstools-invalid-layer",(e=>{e.name="layer"}));let h=0;for(p.walkRules((a=>{a.selectors.forEach((a=>{const t=r(e().astSync(a));h=Math.max(h,t.a+1)}))})),p.walkRules((e=>{e.parent&&"atrule"===e.parent.type&&t.includes(e.parent.name)||i(e)||e.some((e=>"decl"===e.type&&e.important))||(e.selectors=e.selectors.map((e=>n(e,f.layerCount*h))))})),f.sortLayerNames(),function(e,r){for(;o(e,(e=>e.nodes&&o(e,(e=>"layer"===e.name))));){let a=!1;if(e.walkAtRules("layer",(t=>{if(t.parent!==e){if("atrule"===t.parent.type&&"layer"===t.parent.name){const e=t.parent;return r.layerNameParts.set(`${e.params}.${t.params}`,[...r.layerNameParts.get(e.params),...r.layerNameParts.get(t.params)]),r.layerParamsParsed.set(`${e.params}.${t.params}`,[`${e.params}.${t.params}`]),t.params=`${e.params}.${t.params}`,e.before(t),y(e),void m(e)}if("atrule"===t.parent.type){const e=t.parent,r=e.clone(),a=t.clone();return r.removeAll(),a.removeAll(),r.append(t.nodes),a.append(r),e.before(a),t.remove(),y(e),void m(e)}a=!0}})),a)break}}(p,f),function(e,r){e.walkAtRules("layer",(e=>{const r=e.clone(),s=e.clone();r.walkAtRules((e=>{if(t.includes(e.name)){const r=e.parent;return e.remove(),y(r),void m(r)}if(l(e,(e=>"rule"===e.type)))return;const r=e.parent;e.remove(),y(r),m(r)})),s.walkRules((e=>{if(e.parent&&"atrule"===e.parent.type&&t.includes(e.parent.name))return;const r=e.parent;e.remove(),y(r),m(r)})),s.walkAtRules((e=>{if(a.includes(e.name))return y(e),void m(e)})),r.name="csstools-layer-with-selector-rules",e.replaceWith(r,s),0===r.nodes.length&&r.remove(),0===s.nodes.length&&s.remove()})),e.nodes.sort(((e,a)=>{const t="atrule"===e.type&&"charset"===e.name,s="atrule"===a.type&&"charset"===a.name;if(t&&s)return 0;if(t!==s)return t?-1:1;const n="atrule"===e.type&&"import"===e.name,l="atrule"===a.type&&"import"===a.name;if(n&&l)return 0;if(n!==l)return n?-1:1;const o="atrule"===e.type&&"layer"===e.name,i="atrule"===a.type&&"layer"===a.name;return o&&i?r.layerOrder.get(e.params)-r.layerOrder.get(a.params):o!==i?o?-1:1:0})),e.walkAtRules("csstools-layer-with-selector-rules",(e=>{e.name="layer"}))}(p,f),p.walkRules((e=>{if(e.parent&&"atrule"===e.parent.type&&t.includes(e.parent.name))return;const r=i(e);if(!r)return;const a=f.getLayerParams(r).join(".");let s=f.layerOrder.get(a)*h;e.some((e=>"decl"===e.type&&e.important))&&(s=f.layerCount-s),e.selectors=e.selectors.map((e=>n(e,s)))}));o(p,(e=>"layer"===e.name));)p.walkAtRules("layer",(e=>{e.replaceWith(e.nodes)}));p.walkAtRules("csstools-invalid-layer",(e=>{e.name="layer"}))}}};p.postcss=!0;export{p as default};
|
|
|