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.9 KiB

"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!1;return!0}function y(e){if(d(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 m=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 g=["container","document","media","supports","layer"];function v(e){const t=s(e);var n,o;e.params=(n=t.params,o=e.params,m(n).map((e=>m(o).map((t=>`${e} and ${t}`)).join(", "))).join(", ")),r(t)}function b(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 n=s(e);e.selectors=u(n.selectors,e.selectors,t),("rule"===e.type&&"rule"===n.type&&e.selector===n.selector||"atrule"===e.type&&"atrule"===n.type&&e.params===n.params)&&e.append(...n.nodes),r(n)}(e,t):(e=>"atrule"===e.type&&"nest"===e.name&&"rule"===Object(e.parent).type&&m(e.params).every((e=>e.split("&").length>=2&&-1===e.indexOf("|"))))(e)?function(e,t,n){const o=s(e),l=o.clone().removeAll().append(e.nodes);l.raws.semicolon=!0,e.replaceWith(l),l.selectors=u(o.selectors,m(e.params),n),r(o),t(l,n)}(e,b,t):(e=>"atrule"===e.type&&g.includes(e.name)&&"rule"===Object(e.parent).type)(e)?function(e,t,n){const o=s(e),l=o.clone().removeAll().append(e.nodes);e.append(l),r(o),t(l,n)}(e,b,t):(e=>"atrule"===e.type&&g.includes(e.name)&&"atrule"===Object(e.parent).type&&e.name===e.parent.name)(e)&&v(e),Object(e.nodes).length&&b(e,t)}))}function w(e){const t=Object(e).noIsPseudoSelector||!1;return{postcssPlugin:"postcss-nesting",Rule(e){b(e,{noIsPseudoSelector:t})}}}w.postcss=!0,module.exports=w;