[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const { pathElems, referencesProps } = require('./_collections.js');
|
---|
| 4 |
|
---|
| 5 | exports.name = 'moveGroupAttrsToElems';
|
---|
| 6 |
|
---|
| 7 | exports.type = 'perItem';
|
---|
| 8 |
|
---|
| 9 | exports.active = true;
|
---|
| 10 |
|
---|
| 11 | exports.description = 'moves some group attributes to the content elements';
|
---|
| 12 |
|
---|
| 13 | const 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 | */
|
---|
| 34 | exports.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 | };
|
---|