| 1 |
// Mirrors WP_Theme_JSON::process_blocks_custom_css — diverge and the preview |
| 2 |
// shows an effect the save drops, or hides one it keeps. |
| 3 |
const HAS_MARKUP = /<\/?\w/; |
| 4 |
const PSEUDO_ELEMENT = /([>+~\s]*::[a-zA-Z-]+)/; |
| 5 |
|
| 6 |
export const processCustomCss = (css, selector) => { |
| 7 |
if (!css || HAS_MARKUP.test(css)) return ''; |
| 8 |
return css.split('&').reduce((out, part) => { |
| 9 |
if (!part) return out; |
| 10 |
if (!part.includes('{')) { |
| 11 |
return `${out}:root :where(${selector.trim()}){${part.trim()}}`; |
| 12 |
} |
| 13 |
// A bare } makes make-pot skip the file, dropping the bundle's translations. |
| 14 |
const [nested, declarations, ...rest] = part.replace(/\}/g, '').split('{'); |
| 15 |
if (rest.length || declarations === undefined) return out; |
| 16 |
const pseudo = nested.match(PSEUDO_ELEMENT)?.[1] ?? ''; |
| 17 |
const bare = pseudo ? nested.replace(pseudo, '') : nested; |
| 18 |
const scoped = bare.startsWith(' ') |
| 19 |
? `${selector} ${bare.trim()}` |
| 20 |
: `${selector}${bare}`; |
| 21 |
return `${out}:root :where(${scoped})${pseudo}{${declarations.trim()}}`; |
| 22 |
}, ''); |
| 23 |
}; |
| 24 |
|