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
6.1 KiB

import e from"postcss-selector-parser";import{selectorSpecificity as s}from"@csstools/selector-specificity";function o(s,o){return e.isPseudoElement(s)?n.pseudoElement:n[o]}const n={universal:0,tag:1,pseudoElement:2,id:3,class:4,attribute:5,pseudo:6,selector:7,string:8,root:9,comment:10};function t(s,n,t,d){return s.flatMap((s=>{if(-1===s.indexOf(":-csstools-matches")&&-1===s.indexOf(":is"))return s;const r=e().astSync(s);return r.walkPseudos((s=>{if(":is"===s.value&&s.nodes&&s.nodes.length&&"selector"===s.nodes[0].type&&0===s.nodes[0].nodes.length)return s.value=":not",void s.nodes[0].append(e.universal());if(":-csstools-matches"===s.value)if(!s.nodes||s.nodes.length){if(s.walkPseudos((s=>{if(e.isPseudoElement(s)){let e=s.value;if(e.startsWith("::-csstools-invalid-"))return;for(;e.startsWith(":");)e=e.slice(1);s.value=`::-csstools-invalid-${e}`,d()}})),1===s.nodes.length&&"selector"===s.nodes[0].type){if(1===s.nodes[0].nodes.length)return void s.replaceWith(s.nodes[0].nodes[0]);if(!s.nodes[0].some((e=>"combinator"===e.type)))return void s.replaceWith(...s.nodes[0].nodes)}1!==r.nodes.length||"selector"!==r.nodes[0].type||1!==r.nodes[0].nodes.length||r.nodes[0].nodes[0]!==s?function(e){return!(!e||!e.nodes||"selector"!==e.type||3!==e.nodes.length||!e.nodes[0]||"pseudo"!==e.nodes[0].type||":-csstools-matches"!==e.nodes[0].value||!e.nodes[1]||"combinator"!==e.nodes[1].type||"+"!==e.nodes[1].value||!e.nodes[2]||"pseudo"!==e.nodes[2].type||":-csstools-matches"!==e.nodes[2].value||!e.nodes[0].nodes||1!==e.nodes[0].nodes.length||"selector"!==e.nodes[0].nodes[0].type||!e.nodes[0].nodes[0].nodes||3!==e.nodes[0].nodes[0].nodes.length||!e.nodes[0].nodes[0].nodes||"combinator"!==e.nodes[0].nodes[0].nodes[1].type||">"!==e.nodes[0].nodes[0].nodes[1].value||!e.nodes[2].nodes||1!==e.nodes[2].nodes.length||"selector"!==e.nodes[2].nodes[0].type||!e.nodes[2].nodes[0].nodes||3!==e.nodes[2].nodes[0].nodes.length||!e.nodes[2].nodes[0].nodes||"combinator"!==e.nodes[2].nodes[0].nodes[1].type||">"!==e.nodes[2].nodes[0].nodes[1].value||(e.nodes[0].nodes[0].insertAfter(e.nodes[0].nodes[0].nodes[0],e.nodes[2].nodes[0].nodes[0].clone()),e.nodes[2].nodes[0].nodes[1].remove(),e.nodes[2].nodes[0].nodes[0].remove(),e.nodes[0].replaceWith(e.nodes[0].nodes[0]),e.nodes[2].replaceWith(e.nodes[2].nodes[0]),0))}(s.parent)||function(s){if(!s||!s.nodes)return!1;if("selector"!==s.type)return!1;if(2!==s.nodes.length)return!1;let o,n;return s.nodes[0]&&"pseudo"===s.nodes[0].type&&":-csstools-matches"===s.nodes[0].value?(o=0,n=1):s.nodes[1]&&"pseudo"===s.nodes[1].type&&":-csstools-matches"===s.nodes[1].value&&(o=1,n=0),!(!o||!s.nodes[n]||"selector"===s.nodes[n].type&&s.nodes[n].some((s=>"combinator"===s.type||e.isPseudoElement(s)))||(s.nodes[o].append(s.nodes[n].clone()),s.nodes[o].replaceWith(...s.nodes[o].nodes),s.nodes[n].remove(),0))}(s.parent)||("warning"===n.onComplexSelector&&t(),s.value=":is"):s.replaceWith(...s.nodes[0].nodes)}else s.remove()})),r.walk((e=>{"selector"===e.type&&"nodes"in e&&1===e.nodes.length&&"selector"===e.nodes[0].type&&e.replaceWith(e.nodes[0])})),r.walk((s=>{"nodes"in s&&function(s){if(!s||!s.nodes)return;const n=[];let t=[];for(let o=0;o<s.nodes.length;o++)"combinator"!==s.nodes[o].type?e.isPseudoElement(s.nodes[o])?(n.push(t),t=[s.nodes[o]]):t.push(s.nodes[o]):(n.push(t),n.push([s.nodes[o]]),t=[]);n.push(t);const d=[];for(let e=0;e<n.length;e++){const s=n[e];s.sort(((e,s)=>"selector"===e.type&&"selector"===s.type&&e.nodes.length&&s.nodes.length?o(e.nodes[0],e.nodes[0].type)-o(s.nodes[0],s.nodes[0].type):"selector"===e.type&&e.nodes.length?o(e.nodes[0],e.nodes[0].type)-o(s,s.type):"selector"===s.type&&s.nodes.length?o(e,e.type)-o(s.nodes[0],s.nodes[0].type):o(e,e.type)-o(s,s.type)));for(let e=0;e<s.length;e++)d.push(s[e])}for(let e=d.length-1;e>=0;e--)d[e].remove(),s.prepend(d[e])}(s)})),r.toString()})).filter((e=>!!e))}function d(o,n,t=0){const r=":not(#"+n.specificityMatchingName+")",l=":not(."+n.specificityMatchingName+")",c=":not("+n.specificityMatchingName+")";return o.flatMap((o=>{if(-1===o.indexOf(":is"))return o;let i=!1;const a=[];if(e().astSync(o).walkPseudos((e=>{if(":is"!==e.value||!e.nodes||!e.nodes.length)return;if("selector"===e.nodes[0].type&&0===e.nodes[0].nodes.length)return;let o=e.parent;for(;o;){if(":is"===o.value&&"pseudo"===o.type)return void(i=!0);o=o.parent}const n=s(e),t=e.sourceIndex,d=t+e.toString().length,p=[];e.nodes.forEach((e=>{const o={start:t,end:d,option:""},i=s(e);let a=e.toString().trim();const u=Math.max(0,n.a-i.a),h=Math.max(0,n.b-i.b),f=Math.max(0,n.c-i.c);for(let e=0;e<u;e++)a+=r;for(let e=0;e<h;e++)a+=l;for(let e=0;e<f;e++)a+=c;o.option=a,p.push(o)})),a.push(p)})),!a.length)return[o];let p=[];return function(...e){const s=[],o=e.length-1;function n(t,d){for(let r=0,l=e[d].length;r<l;r++){const l=t.slice(0);l.push(e[d][r]),d==o?s.push(l):n(l,d+1)}}return n([],0),s}(...a).forEach((e=>{let s="";for(let t=0;t<e.length;t++){var n;const d=e[t];s+=o.substring((null==(n=e[t-1])?void 0:n.end)||0,e[t].start),s+=":-csstools-matches("+d.option+")",t===e.length-1&&(s+=o.substring(e[t].end))}p.push(s)})),i&&t<10&&(p=d(p,n,t+1)),p})).filter((e=>!!e))}const r=e=>{const s={specificityMatchingName:"does-not-exist",...e||{}};return{postcssPlugin:"postcss-is-pseudo-class",Rule(e,{result:o}){if(!e.selector)return;if(-1===e.selector.indexOf(":is"))return;let n=!1;const r=()=>{"warning"===s.onComplexSelector&&(n||(n=!0,e.warn(o,`Complex selectors in '${e.selector}' can not be transformed to an equivalent selector without ':is()'.`)))};let l=!1;const c=()=>{"warning"===s.onPseudoElement&&(l||(l=!0,e.warn(o,`Pseudo elements are not allowed in ':is()', unable to transform '${e.selector}'`)))};try{let o=!1;const n=[],l=t(d(e.selectors,{specificityMatchingName:s.specificityMatchingName}),{onComplexSelector:s.onComplexSelector},r,c);if(Array.from(new Set(l)).forEach((s=>{e.selectors.indexOf(s)>-1?n.push(s):(e.cloneBefore({selector:s}),o=!0)})),n.length&&o&&e.cloneBefore({selectors:n}),!s.preserve){if(!o)return;e.remove()}}catch(s){if(s.message.indexOf("call stack size exceeded")>-1)throw s;e.warn(o,`Failed to parse selector "${e.selector}"`)}}}};r.postcss=!0;export{r as default};