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.

2 lines
5.9 KiB

2 years ago
"use strict";var e=require("postcss-selector-parser"),t=require("@csstools/selector-specificity");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(e);function r(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 s(e){const t=e.parent,n=t.index(e);if(n){const e=t.cloneBefore().removeAll().append(t.nodes.slice(0,n));e.raws.semicolon=!0,r(e)}return t.before(e),t.raws.semicolon=!0,t}function l(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 p=o.default.pseudo({value:":is"});function c(e){if(!e||!e.nodes)return;const t=[];let n=[];for(let r=0;r<e.nodes.length;r++)if("combinator"!==e.nodes[r].type)if(o.default.isPseudoElement(e.nodes[r]))t.push(n),n=[e.nodes[r]];else{if("tag"===e.nodes[r].type&&n.find((e=>"tag"===e.type))){const t=p.clone(),n=e.nodes[r];n.replaceWith(t),t.append(n)}n.push(e.nodes[r])}else t.push(n),t.push([e.nodes[r]]),n=[];t.push(n);const r=[];for(let e=0;e<t.length;e++){const n=t[e];n.sort(((e,t)=>"selector"===e.type&&"selector"===t.type&&e.nodes.length&&t.nodes.length?a(e.nodes[0],e.nodes[0].type)-a(t.nodes[0],t.nodes[0].type):"selector"===e.type&&e.nodes.length?a(e.nodes[0],e.nodes[0].type)-a(t,t.type):"selector"===t.type&&t.nodes.length?a(e,e.type)-a(t.nodes[0],t.nodes[0].type):a(e,e.type)-a(t,t.type)));for(let e=0;e<n.length;e++)r.push(n[e])}for(let t=r.length-1;t>=0;t--)r[t].remove(),e.prepend(r[t])}function a(e,t){return o.default.isPseudoElement(e)?i.pseudoElement:i[t]}const i={universal:0,tag:1,pseudoElement:2,id:3,class:4,attribute:5,pseudo:6,selector:7,string:8,root:9,comment:10};function u(e,n,r){let s=[];s=function(e){const n=e.map((e=>o.default().astSync(e))).map((e=>t.selectorSpecificity(e))),r=n[0];for(let e=1;e<n.length;e++)if(r.a!==n[e].a||r.b!==n[e].b||r.c!==n[e].c)return!1;return!0}(e)||r.noIsPseudoSelector?e.map((e=>o.default().astSync(e))):[o.default().astSync(`:is(${e.join(",")})`)];let p=[];for(let e=0;e<n.length;e++){const t=n[e];let a=1,i=[],u=0;if(o.default().astSync(t).walkNesting((()=>{u++})),u>1&&s.length>1)i=l(s,u),a=i.length;else{a=s.length;for(let e=0;e<s.length;e++){i.push([]);for(let t=0;t<u;t++)i[e].push(s[e])}}for(let e=0;e<a;e++){let n=0;const s=o.default().astSync(t);s.walk((t=>{if("nesting"!==t.type)return;let s=i[e][n];n++,"root"===s.type&&1===s.nodes.length&&(s=s.nodes[0]);const l=o.default().astSync(`:is(${s.toString()})`),p=d(s.nodes[0]),a=f(s.nodes[0]),u=d(t),m=f(t);if(p&&u)return void t.replaceWith(s.clone());if((p||a)&&(u||m)){const e=t.parent;return p&&"selector"===s.type?t.replaceWith(s.clone().nodes[0]):t.replaceWith(...s.clone().nodes),void(e&&e.nodes.length>1&&c(e))}if(p){const e=t.parent;return t.replaceWith(s.clone().nodes[0]),void(e&&c(e))}if(a){const e=t.parent;return t.replaceWith(...s.clone().nodes),void(e&&c(e))}if(h(t)){const e=t.parent;return t.replaceWith(...s.clone().nodes),void(e&&c(e))}if(y(t)){const e=t.parent;return t.replaceWith(...s.clone().nodes),void(e&&c(e))}const g=t.parent;r.noIsPseudoSelector?t.replaceWith(...s.clone().nodes):t.replaceWith(...l.clone().nodes),g&&c(g)})),p.push(s.toString())}}return p}function d(e){return"combinator"!==e.type&&!(e.parent&&e.parent.nodes.length>1)}function f(e,t=null){if(d(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&&!f(t))}function h(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!