diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-05-18 23:10:59 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-05-18 23:41:44 +0800 |
| commit | fc87c009806dcc98867f6b58efe29b042aa01dee (patch) | |
| tree | 8f59b3f4a5b66e9dfef2d40c8d318e258a521228 /node_modules/svgo/plugins/moveElemsAttrsToGroup.js | |
| parent | 3336b2356ee6152d9c6ae59bd9b87ccbc0958407 (diff) | |
node: updatemain
Diffstat (limited to 'node_modules/svgo/plugins/moveElemsAttrsToGroup.js')
| -rw-r--r-- | node_modules/svgo/plugins/moveElemsAttrsToGroup.js | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/node_modules/svgo/plugins/moveElemsAttrsToGroup.js b/node_modules/svgo/plugins/moveElemsAttrsToGroup.js deleted file mode 100644 index 633f4439..00000000 --- a/node_modules/svgo/plugins/moveElemsAttrsToGroup.js +++ /dev/null @@ -1,128 +0,0 @@ -'use strict'; - -const { visit } = require('../lib/xast.js'); -const { inheritableAttrs, pathElems } = require('./_collections.js'); - -exports.name = 'moveElemsAttrsToGroup'; -exports.description = 'Move common attributes of group children to the group'; - -/** - * Move common attributes of group children to the group - * - * @example - * <g attr1="val1"> - * <g attr2="val2"> - * text - * </g> - * <circle attr2="val2" attr3="val3"/> - * </g> - * ⬇ - * <g attr1="val1" attr2="val2"> - * <g> - * text - * </g> - * <circle attr3="val3"/> - * </g> - * - * @author Kir Belevich - * - * @type {import('./plugins-types').Plugin<'moveElemsAttrsToGroup'>} - */ -exports.fn = (root) => { - // find if any style element is present - let deoptimizedWithStyles = false; - visit(root, { - element: { - enter: (node) => { - if (node.name === 'style') { - deoptimizedWithStyles = true; - } - }, - }, - }); - - return { - element: { - exit: (node) => { - // process only groups with more than 1 children - if (node.name !== 'g' || node.children.length <= 1) { - return; - } - - // deoptimize the plugin when style elements are present - // selectors may rely on id, classes or tag names - if (deoptimizedWithStyles) { - return; - } - - /** - * find common attributes in group children - * @type {Map<string, string>} - */ - const commonAttributes = new Map(); - let initial = true; - let everyChildIsPath = true; - for (const child of node.children) { - if (child.type === 'element') { - if (!pathElems.has(child.name)) { - everyChildIsPath = false; - } - if (initial) { - initial = false; - // collect all inheritable attributes from first child element - for (const [name, value] of Object.entries(child.attributes)) { - // consider only inheritable attributes - if (inheritableAttrs.has(name)) { - commonAttributes.set(name, value); - } - } - } else { - // exclude uncommon attributes from initial list - for (const [name, value] of commonAttributes) { - if (child.attributes[name] !== value) { - commonAttributes.delete(name); - } - } - } - } - } - - // preserve transform on children when group has clip-path or mask - if ( - node.attributes['clip-path'] != null || - node.attributes.mask != null - ) { - commonAttributes.delete('transform'); - } - - // preserve transform when all children are paths - // so the transform could be applied to path data by other plugins - if (everyChildIsPath) { - commonAttributes.delete('transform'); - } - - // add common children attributes to group - for (const [name, value] of commonAttributes) { - if (name === 'transform') { - if (node.attributes.transform != null) { - node.attributes.transform = `${node.attributes.transform} ${value}`; - } else { - node.attributes.transform = value; - } - } else { - node.attributes[name] = value; - } - } - - // delete common attributes from children - for (const child of node.children) { - if (child.type === 'element') { - for (const [name] of commonAttributes) { - delete child.attributes[name]; - } - } - } - }, - }, - }; -}; |
