diff --git a/plugins/inlineStyles.js b/plugins/inlineStyles.js index 8f23bd347..a19f3fbd5 100644 --- a/plugins/inlineStyles.js +++ b/plugins/inlineStyles.js @@ -1,24 +1,43 @@ 'use strict'; -const csstree = require('css-tree'); -const { querySelectorAll, closestByName } = require('../lib/xast.js'); -const cssTools = require('../lib/css-tools'); +/** + * @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.type = 'full'; - exports.active = true; +exports.description = 'inline styles (additional options)'; -exports.params = { - onlyMatchedOnce: true, - removeMatchedSelectors: true, - useMqs: ['', 'screen'], - usePseudos: [''], +/** + * 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; }; -exports.description = 'inline styles (additional options)'; - /** * Moves + merges styles from style elements to element styles * @@ -38,267 +57,323 @@ exports.description = 'inline styles (additional options)'; * what pseudo-classes/-elements to be used * empty string element for all non-pseudo-classes and/or -elements * - * @param {Object} root document element - * @param {Object} opts plugin params - * * @author strarsis + * + * @type {import('../lib/types').Plugin<{ + * onlyMatchedOnce?: boolean, + * removeMatchedSelectors?: boolean, + * useMqs?: Array, + * usePseudos?: Array + * }>} */ -exports.fn = function (root, opts) { - // collect - + @@@ diff --git a/test/plugins/inlineStyles.16.svg b/test/plugins/inlineStyles.16.svg index 1688ba8b9..0f73f776c 100644 --- a/test/plugins/inlineStyles.16.svg +++ b/test/plugins/inlineStyles.16.svg @@ -25,6 +25,7 @@ @@@ + button diff --git a/tsconfig.json b/tsconfig.json index f7dbe1cca..197dbe3c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,7 +29,6 @@ "plugins/removeDimensions.js", "plugins/removeNonInheritableGroupAttrs.js", "plugins/removeXMLNS.js", - "plugins/inlineStyles.js", "plugins/preset-default.js" ] }