| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'moves some group attributes to the content elements';
|
|---|
| 8 |
|
|---|
| 9 | var collections = require('./_collections.js'),
|
|---|
| 10 | pathElems = collections.pathElems.concat(['g', 'text']),
|
|---|
| 11 | referencesProps = collections.referencesProps;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Move group attrs to the content elements.
|
|---|
| 15 | *
|
|---|
| 16 | * @example
|
|---|
| 17 | * <g transform="scale(2)">
|
|---|
| 18 | * <path transform="rotate(45)" d="M0,0 L10,20"/>
|
|---|
| 19 | * <path transform="translate(10, 20)" d="M0,10 L20,30"/>
|
|---|
| 20 | * </g>
|
|---|
| 21 | * ⬇
|
|---|
| 22 | * <g>
|
|---|
| 23 | * <path transform="scale(2) rotate(45)" d="M0,0 L10,20"/>
|
|---|
| 24 | * <path transform="scale(2) translate(10, 20)" d="M0,10 L20,30"/>
|
|---|
| 25 | * </g>
|
|---|
| 26 | *
|
|---|
| 27 | * @param {Object} item current iteration item
|
|---|
| 28 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 29 | *
|
|---|
| 30 | * @author Kir Belevich
|
|---|
| 31 | */
|
|---|
| 32 | exports.fn = function(item) {
|
|---|
| 33 |
|
|---|
| 34 | // move group transform attr to content's pathElems
|
|---|
| 35 | if (
|
|---|
| 36 | item.isElem('g') &&
|
|---|
| 37 | item.hasAttr('transform') &&
|
|---|
| 38 | !item.isEmpty() &&
|
|---|
| 39 | !item.someAttr(function(attr) {
|
|---|
| 40 | return ~referencesProps.indexOf(attr.name) && ~attr.value.indexOf('url(');
|
|---|
| 41 | }) &&
|
|---|
| 42 | item.content.every(function(inner) {
|
|---|
| 43 | return inner.isElem(pathElems) && !inner.hasAttr('id');
|
|---|
| 44 | })
|
|---|
| 45 | ) {
|
|---|
| 46 | item.content.forEach(function(inner) {
|
|---|
| 47 | var attr = item.attr('transform');
|
|---|
| 48 | if (inner.hasAttr('transform')) {
|
|---|
| 49 | inner.attr('transform').value = attr.value + ' ' + inner.attr('transform').value;
|
|---|
| 50 | } else {
|
|---|
| 51 | inner.addAttr({
|
|---|
| 52 | 'name': attr.name,
|
|---|
| 53 | 'local': attr.local,
|
|---|
| 54 | 'prefix': attr.prefix,
|
|---|
| 55 | 'value': attr.value
|
|---|
| 56 | });
|
|---|
| 57 | }
|
|---|
| 58 | });
|
|---|
| 59 |
|
|---|
| 60 | item.removeAttr('transform');
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | };
|
|---|