1 line
1.8 KiB
JavaScript
1 line
1.8 KiB
JavaScript
"use strict";var e=require("postcss-selector-parser");const s=[" ",">","~",":","+","@","#","(",")"];function isValidReplacement(e){let n=!0;for(let t=0,l=s.length;t<l&&n;t++)e.indexOf(s[t])>-1&&(n=!1);return n}const n="js-blank-pseudo",t=":blank",creator=s=>{const l=Object.assign({preserve:!0,replaceWith:"[blank]",disablePolyfillReadyClass:!1},s),r=e().astSync(l.replaceWith);return isValidReplacement(l.replaceWith)?{postcssPlugin:"css-blank-pseudo",prepare(){const s=new WeakSet;return{Rule(o,{result:a}){if(s.has(o))return;if(!o.selector.toLowerCase().includes(t))return;const i=o.selectors.flatMap((s=>{if(!s.toLowerCase().includes(t))return[s];let i;try{i=e().astSync(s)}catch(e){return o.warn(a,`Failed to parse selector : "${s}" with message: "${e.message}"`),[s]}if(void 0===i)return[s];let c=!1;if(i.walkPseudos((e=>{e.value.toLowerCase()===t&&(e.nodes&&e.nodes.length||(c=!0,e.replaceWith(r.clone({}))))})),!c)return[s];const d=i.clone();if(!l.disablePolyfillReadyClass){var u,p,f,g,m;if(null!=(u=i.nodes)&&null!=(p=u[0])&&null!=(f=p.nodes)&&f.length)for(let s=0;s<i.nodes[0].nodes.length;s++){const t=i.nodes[0].nodes[s];if("combinator"===t.type||e.isPseudoElement(t)){i.nodes[0].insertBefore(t,e.className({value:n}));break}if(s===i.nodes[0].nodes.length-1){i.nodes[0].append(e.className({value:n}));break}}return null!=(g=i.nodes)&&null!=(m=g[0])&&m.nodes&&(d.nodes[0].prepend(e.combinator({value:" "})),d.nodes[0].prepend(e.className({value:n}))),[i.toString(),d.toString()]}return[i.toString()]}));i.join(",")!==o.selectors.join(",")&&(s.add(o),o.cloneBefore({selectors:i}),l.preserve||o.remove())}}}}:{postcssPlugin:"css-blank-pseudo",Once:(e,{result:s})=>{e.warn(s,`${l.replaceWith} is not a valid replacement since it can't be applied to single elements.`)}}};creator.postcss=!0,module.exports=creator;
|