postcss-editor-styles-wrapper
Version:
Remove your editor-styles from the global scope
2 lines (1 loc) • 1.3 kB
JavaScript
const e={scopeTo:".editor-styles-wrapper",repeat:1,remove:["html"],replace:["body"],ignore:[":root"],tags:["a","svg","a:hover","a:focus","button","button:hover","button:focus","input","label","select","textarea","form",'input[type="button"]','input[type="submit"]','input[type="reset"]','[type="button"]','[type="submit"]','[type="reset"]'],tagSuffix:':not([class^="components-"]):not([class^="editor-"]):not([class^="block-"]):not([aria-owns]):not([id^="mceu_"])'},firstOrLastSelector=(e,t)=>{const[r]=t,o=t[t.length-1],s=[];if(e.includes(r))s.push(r);else{if(!e.includes(o))return!1;s.push(o)}return s};function creator(t){const r={...e,...t};return{postcssPlugin:"postcss-editor-styles-wrapper",OnceExit(e){e.walkRules((e=>{e.selectors=e.selectors.map((t=>{const o=t.split(" ");o[0];let s=o[o.length-1];if("atrule"===e.parent.type&&"keyframes"===e.parent.name)return t;if(firstOrLastSelector(r.remove,o))return e.remove();if(firstOrLastSelector(r.replace,o)){const e=firstOrLastSelector(r.replace,o);return t.replace(RegExp(e,"g"),r.scopeTo.repeat(r.repeat))}return firstOrLastSelector(r.ignore,o)?t:-1!==r.tags.indexOf(s)?`${r.scopeTo.repeat(r.repeat)} ${t}${r.tagSuffix}`:firstOrLastSelector(r.scopeTo,o)?t:`${r.scopeTo.repeat(r.repeat)} ${t}`}))}))}}}creator.postcss=!0;export{creator as default};