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

import e from"postcss-selector-parser";import{selectorSpecificity as t}from"@csstools/selector-specificity";function n(e){if(!e.nodes.length)return void e.remove();const t=e.nodes.filter((e=>"comment"===e.type));t.length===e.nodes.length&&e.replaceWith(...t)}function o(e){const t=e.parent,o=t.index(e);if(o){const e=t.cloneBefore().removeAll().append(t.nodes.slice(0,o));e.raws.semicolon=!0,n(e)}return t.before(e),t.raws.semicolon=!0,t}function r(e,t){if(t<2)throw new Error("n must be greater than 1");if(e.length<2)throw new Error("s must be greater than 1");if(Math.pow(e.length,t)>1e4)throw new Error("Too many combinations when trying to resolve a nested selector with lists, reduce the complexity of your selectors");const n=[];for(let e=0;e<t;e++)n[e]=0;const o=[];for(;;){const r=[];for(let s=t-1;s>=0;s--){let t=n[s];if(t>=e.length){if(t=0,n[s]=0,0===s)return o;n[s-1]+=1}r[s]=e[t]}o.push(r),n[n.length-1]++}}const s=e.pseudo({value:":is"});function l(t){if(!t||!t.nodes)return;const n=[];let o=[];for(let r=0;r<t.nodes.length;r++)if("combinator"!==t.nodes[r].type)if(e.isPseudoElement(t.nodes[r]))n.push(o),o=[t.nodes[r]];else{if("tag"===t.nodes[r].type&&o.find((e=>"tag"===e.type))){const e=s.clone(),n=t.nodes[r];n.replaceWith(e),e.append(n)}o.push(t.nodes[r])}else n.push(o),n.push([t.nodes[r]]),o=[];n.push(o);const r=[];for(let e=0;e<n.length;e++){const t=n[e];t.sort(((e,t)=>"selector"===e.type&&"selector"===t.type&&e.nodes.length&&t.nodes.length?p(e.nodes[0],e.nodes[0].type)-p(t.nodes[0],t.nodes[0].type):"selector"===e.type&&e.nodes.length?p(e.nodes[0],e.nodes[0].type)-p(t,t.type):"selector"===t.type&&t.nodes.length?p(e,e.type)-p(t.nodes[0],t.nodes[0].type):p(e,e.type)-p(t,t.type)));for(let e=0;e<t.length;e++)r.push(t[e])}for(let e=r.length-1;e>=0;e--)r[e].remove(),t.prepend(r[e])}function p(t,n){return e.isPseudoElement(t)?c.pseudoElement:c[n]}const c={universal:0,tag:1,pseudoElement:2,id:3,class:4,attribute:5,pseudo:6,selector:7,string:8,root:9,comment:10};function a(n,o,s){let p=[];p=function(n){const o=n.map((t=>e().astSync(t))).map((e=>t(e))),r=o[0];for(let e=1;e<o.length;e++)if(r.a!==o[e].a||r.b!==o[e].b||r.c!==o[e].c)return!1;return!0}(n)||s.noIsPseudoSelector?n.map((t=>e().astSync(t))):[e().astSync(`:is(${n.join(",")})`)];let c=[];for(let t=0;t<o.length;t++){const n=o[t];let a=1,h=[],m=0;if(e().astSync(n).walkNesting((()=>{m++})),m>1&&p.length>1)h=r(p,m),a=h.length;else{a=p.length;for(let e=0;e<p.length;e++){h.push([]);for(let t=0;t<m;t++)h[e].push(p[e])}}for(let t=0;t<a;t++){let o=0;const r=e().astSync(n);r.walk((n=>{if("nesting"!==n.type)return;let r=h[t][o];o++,"root"===r.type&&1===r.nodes.length&&(r=r.nodes[0]);const p=e().astSync(`:is(${r.toString()})`),c=i(r.nodes[0]),a=d(r.nodes[0]),m=i(n),y=d(n);if(c&&m)return void n.replaceWith(r.clone());if((c||a)&&(m||y)){const e=n.parent;return c&&"selector"===r.type?n.replaceWith(r.clone().nodes[0]):n.replaceWith(...r.clone().nodes),void(e&&e.nodes.length>1&&l(e))}if(c){const e=n.parent;return n.replaceWith(r.clone().nodes[0]),void(e&&l(e))}if(a){const e=n.parent;return n.replaceWith(...r.clone().nodes),void(e&&l(e))}if(u(n)){const e=n.parent;return n.replaceWith(...r.clone().nodes),void(e&&l(e))}if(f(n)){const e=n.parent;return n.replaceWith(...r.clone().nodes),void(e&&l(e))}const g=n.parent;s.noIsPseudoSelector?n.replaceWith(...r.clone().nodes):n.replaceWith(...p.clone().nodes),g&&l(g)})),c.push(r.toString())}}return c}function i(e){return"combinator"!==e.type&&!(e.parent&&e.parent.nodes.length>1)}function d(e,t=null){if(i(e))return!1;if(!e.parent)return!1;if(!!e.parent.nodes.find((e=>"combinator"===e.type)))return!1;return!(!!e.parent.nodes.find((e=>"nesting"===e.type))&&t&&!d(t))}function u(e){if(!e.parent)return!1;if(0!==e.parent.nodes.indexOf(e))return!1;for(let t=1;t<e.parent.nodes.length;t++)if("combinator"===e.parent.nodes[t].type&&" "!==e.parent.nodes[t].value&&">"!==e.parent.nodes[t].value)return!1;return!0}function f(e){if(i(e))return!0;if(!e.parent)return!1;for(let t=0;t<e.parent.nodes.length;t++)if("nesting"!==!e.parent.nodes[t].type&&(e.parent.nodes[t].prev()||e.parent.nodes[t].next())){if(e.parent.nodes[t].prev()&&"combinator"!==e.parent.nodes[t].prev().type)return!1;if(e.parent.nodes[t].next()&&"combinator"!==e.parent.nodes[t].next().type)return!1}return!0}const h=e=>{let t=[],n="",o=!1,r=0,s=!1,l=!1;for(let p of e)l?l=!1:"\\"===p?l=!0:s?p===s&&(s=!1):'"'===p||"'"===p?s=p:"("===p?r+=1:")"===p?r>0&&(r-=1):0===r&&","===p&&(o=!0),o?(""!==n&&t.push(n.trim()),n="",o=!1):n+=p;return t.push(n.trim()),t};var m=["container","document","media","supports","layer"];function y(e){const t=o(e);var r,s;e.params=(r=t.params,s=e.params,h(r).map((e=>h(s).map((t=>`${e} and ${t}`)).join(", "))).join(", ")),n(t)}function g(e,t){e.each((e=>{(e=>"rule"===e.type&&"rule"===Object(e.parent).type&&e.selectors.every((e=>0===e.trim().indexOf("&")&&-1===e.indexOf("|"))))(e)?function(e,t){const r=o(e);e.selectors=a(r.selectors,e.selectors,t),("rule"===e.type&&"rule"===r.type&&e.selector===r.selector||"atrule"===e.type&&"atrule"===r.type&&e.params===r.params)&&e.append(...r.nodes),n(r)}(e,t):(e=>"atrule"===e.type&&"nest"===e.name&&"rule"===Object(e.parent).type&&h(e.params).every((e=>e.split("&").length>=2&&-1===e.indexOf("|"))))(e)?function(e,t,r){const s=o(e),l=s.clone().removeAll().append(e.nodes);l.raws.semicolon=!0,e.replaceWith(l),l.selectors=a(s.selectors,h(e.params),r),n(s),t(l,r)}(e,g,t):(e=>"atrule"===e.type&&m.includes(e.name)&&"rule"===Object(e.parent).type)(e)?function(e,t,r){const s=o(e),l=s.clone().removeAll().append(e.nodes);e.append(l),n(s),t(l,r)}(e,g,t):(e=>"atrule"===e.type&&m.includes(e.name)&&"atrule"===Object(e.parent).type&&e.name===e.parent.name)(e)&&y(e),Object(e.nodes).length&&g(e,t)}))}function v(e){const t=Object(e).noIsPseudoSelector||!1;return{postcssPlugin:"postcss-nesting",Rule(e){g(e,{noIsPseudoSelector:t})}}}v.postcss=!0;export{v as default};