|
| 1 | +/** |
| 2 | + * PostCSS plugin to scope CSS rules under `.opencor-scoped-styles`. This prevents CSS leakage when OpenCOR is used in |
| 3 | + * an application that uses another UI framework than PrimeVue (e.g., Bootstrap, Element Plus, Vuetify). |
| 4 | + */ |
| 5 | + |
| 6 | +export default function postcssScopePlugin(opts = {}) { |
| 7 | + const scopeSelector = opts.scopeSelector || '.opencor-scoped-styles'; |
| 8 | + const shouldScope = opts.shouldScope || (() => true); |
| 9 | + |
| 10 | + return { |
| 11 | + postcssPlugin: 'postcss-scope-plugin', |
| 12 | + |
| 13 | + Rule(rule) { |
| 14 | + // Skip if custom filter says no. |
| 15 | + |
| 16 | + if (!shouldScope(rule)) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // Skip if already scoped. |
| 21 | + |
| 22 | + const selectors = rule.selector.split(',').map((s) => s.trim()); |
| 23 | + |
| 24 | + if (selectors.every((s) => s.startsWith(scopeSelector))) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Skip @-rules (like @keyframes, @media, etc.; they will be handled by their parent). |
| 29 | + |
| 30 | + if ( |
| 31 | + rule.parent?.type === 'atrule' && |
| 32 | + ['keyframes', 'font-face', '-webkit-keyframes'].includes(rule.parent.name) |
| 33 | + ) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Handle `:root` specially (convert it to our scope selector). |
| 38 | + |
| 39 | + if (rule.selector === ':root') { |
| 40 | + rule.selector = scopeSelector; |
| 41 | + |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Handle `html` and `body` specially (convert them to our scope selector). |
| 46 | + |
| 47 | + if (rule.selector === 'html' || rule.selector === 'body') { |
| 48 | + rule.selector = scopeSelector; |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // Split and scope each selector. |
| 54 | + |
| 55 | + const scopedSelectors = selectors.flatMap((selector) => { |
| 56 | + // If a selector starts with a pseudo (like `::before`, `:hover`) then scope it directly. |
| 57 | + |
| 58 | + if (selector.startsWith('::') || (selector.startsWith(':') && !selector.includes('('))) { |
| 59 | + return `${scopeSelector}${selector}`; |
| 60 | + } |
| 61 | + |
| 62 | + // Handle a universal selector specially (only as a descendant). |
| 63 | + |
| 64 | + if (selector === '*' || selector.startsWith('* ')) { |
| 65 | + return `${scopeSelector} ${selector}`; |
| 66 | + } |
| 67 | + |
| 68 | + // For simple class/id selectors (like `#app`, `.h-full`), we create both a compound and a descendant. This |
| 69 | + // allows utilities to work on the scoped element itself and its children. We only do this for selectors that |
| 70 | + // start with . or # and have no spaces. |
| 71 | + |
| 72 | + if ((selector.startsWith('.') || selector.startsWith('#')) && !selector.includes(' ')) { |
| 73 | + return [`${scopeSelector}${selector}`, `${scopeSelector} ${selector}`]; |
| 74 | + } |
| 75 | + |
| 76 | + // For everything else (complex selectors, element selectors), we only create a descendant. |
| 77 | + |
| 78 | + return `${scopeSelector} ${selector}`; |
| 79 | + }); |
| 80 | + |
| 81 | + rule.selector = scopedSelectors.join(', '); |
| 82 | + } |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +postcssScopePlugin.postcss = true; |
0 commit comments