source: trip-planner-front/node_modules/svgo/plugins/moveGroupAttrsToElems.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[6a3a178]1'use strict';
2
3const { pathElems, referencesProps } = require('./_collections.js');
4
5exports.name = 'moveGroupAttrsToElems';
6
7exports.type = 'perItem';
8
9exports.active = true;
10
11exports.description = 'moves some group attributes to the content elements';
12
13const pathElemsWithGroupsAndText = [...pathElems, 'g', 'text'];
14
15/**
16 * Move group attrs to the content elements.
17 *
18 * @example
19 * <g transform="scale(2)">
20 * <path transform="rotate(45)" d="M0,0 L10,20"/>
21 * <path transform="translate(10, 20)" d="M0,10 L20,30"/>
22 * </g>
23 * ⬇
24 * <g>
25 * <path transform="scale(2) rotate(45)" d="M0,0 L10,20"/>
26 * <path transform="scale(2) translate(10, 20)" d="M0,10 L20,30"/>
27 * </g>
28 *
29 * @param {Object} item current iteration item
30 * @return {Boolean} if false, item will be filtered out
31 *
32 * @author Kir Belevich
33 */
34exports.fn = function (item) {
35 // move group transform attr to content's pathElems
36 if (
37 item.type === 'element' &&
38 item.name === 'g' &&
39 item.children.length !== 0 &&
40 item.attributes.transform != null &&
41 Object.entries(item.attributes).some(
42 ([name, value]) =>
43 referencesProps.includes(name) && value.includes('url(')
44 ) === false &&
45 item.children.every(
46 (inner) =>
47 pathElemsWithGroupsAndText.includes(inner.name) &&
48 inner.attributes.id == null
49 )
50 ) {
51 for (const inner of item.children) {
52 const value = item.attributes.transform;
53 if (inner.attributes.transform != null) {
54 inner.attributes.transform = value + ' ' + inner.attributes.transform;
55 } else {
56 inner.attributes.transform = value;
57 }
58 }
59
60 delete item.attributes.transform;
61 }
62};
Note: See TracBrowser for help on using the repository browser.