'use strict'; /** * @typedef {import('../lib/types').Specificity} Specificity * @typedef {import('../lib/types').XastElement} XastElement * @typedef {import('../lib/types').XastParent} XastParent */ const csstree = require('css-tree'); // @ts-ignore not defined in @types/csso const specificity = require('csso/lib/restructure/prepare/specificity'); const stable = require('stable'); const { visitSkip, querySelectorAll, detachNodeFromParent, } = require('../lib/xast.js'); exports.type = 'visitor'; exports.name = 'inlineStyles'; exports.active = true; exports.description = 'inline styles (additional options)'; /** * Compares two selector specificities. * extracted from https://github.com/keeganstreet/specificity/blob/master/specificity.js#L211 * * @type {(a: Specificity, b: Specificity) => number} */ const compareSpecificity = (a, b) => { for (var i = 0; i < 4; i += 1) { if (a[i] < b[i]) { return -1; } else if (a[i] > b[i]) { return 1; } } return 0; }; /** * Moves + merges styles from style elements to element styles * * Options * onlyMatchedOnce (default: true) * inline only selectors that match once * * removeMatchedSelectors (default: true) * clean up matched selectors, * leave selectors that hadn't matched * * useMqs (default: ['', 'screen']) * what media queries to be used * empty string element for styles outside media queries * * usePseudos (default: ['']) * what pseudo-classes/-elements to be used * empty string element for all non-pseudo-classes and/or -elements * * @author strarsis * * @type {import('../lib/types').Plugin<{ * onlyMatchedOnce?: boolean, * removeMatchedSelectors?: boolean, * useMqs?: Array, * usePseudos?: Array * }>} */ exports.fn = (root, params) => { const { onlyMatchedOnce = true, removeMatchedSelectors = true, useMqs = ['', 'screen'], usePseudos = [''], } = params; /** * @type {Array<{ node: XastElement, parentNode: XastParent, cssAst: csstree.StyleSheet }>} */ const styles = []; /** * @type {Array<{ * node: csstree.Selector, * item: csstree.ListItem, * rule: csstree.Rule, * matchedElements?: Array * }>} */ let selectors = []; return { element: { enter: (node, parentNode) => { // skip content if (node.name === 'foreignObject') { return visitSkip; } // collect only non-empty