| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItemReverse';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'collapses useless groups';
|
|---|
| 8 |
|
|---|
| 9 | var collections = require('./_collections'),
|
|---|
| 10 | attrsInheritable = collections.inheritableAttrs,
|
|---|
| 11 | animationElems = collections.elemsGroups.animation;
|
|---|
| 12 |
|
|---|
| 13 | function hasAnimatedAttr(item) {
|
|---|
| 14 | /* jshint validthis:true */
|
|---|
| 15 | return item.isElem(animationElems) && item.hasAttr('attributeName', this) ||
|
|---|
| 16 | !item.isEmpty() && item.content.some(hasAnimatedAttr, this);
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | /*
|
|---|
| 20 | * Collapse useless groups.
|
|---|
| 21 | *
|
|---|
| 22 | * @example
|
|---|
| 23 | * <g>
|
|---|
| 24 | * <g attr1="val1">
|
|---|
| 25 | * <path d="..."/>
|
|---|
| 26 | * </g>
|
|---|
| 27 | * </g>
|
|---|
| 28 | * ⬇
|
|---|
| 29 | * <g>
|
|---|
| 30 | * <g>
|
|---|
| 31 | * <path attr1="val1" d="..."/>
|
|---|
| 32 | * </g>
|
|---|
| 33 | * </g>
|
|---|
| 34 | * ⬇
|
|---|
| 35 | * <path attr1="val1" d="..."/>
|
|---|
| 36 | *
|
|---|
| 37 | * @param {Object} item current iteration item
|
|---|
| 38 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 39 | *
|
|---|
| 40 | * @author Kir Belevich
|
|---|
| 41 | */
|
|---|
| 42 | exports.fn = function(item) {
|
|---|
| 43 |
|
|---|
| 44 | // non-empty elements
|
|---|
| 45 | if (item.isElem() && !item.isElem('switch') && !item.isEmpty()) {
|
|---|
| 46 | item.content.forEach(function(g, i) {
|
|---|
| 47 | // non-empty groups
|
|---|
| 48 | if (g.isElem('g') && !g.isEmpty()) {
|
|---|
| 49 | // move group attibutes to the single content element
|
|---|
| 50 | if (g.hasAttr() && g.content.length === 1) {
|
|---|
| 51 | var inner = g.content[0];
|
|---|
| 52 |
|
|---|
| 53 | if (inner.isElem() && !inner.hasAttr('id') && !g.hasAttr('filter') &&
|
|---|
| 54 | !(g.hasAttr('class') && inner.hasAttr('class')) && (
|
|---|
| 55 | !g.hasAttr('clip-path') && !g.hasAttr('mask') ||
|
|---|
| 56 | inner.isElem('g') && !g.hasAttr('transform') && !inner.hasAttr('transform')
|
|---|
| 57 | )
|
|---|
| 58 | ) {
|
|---|
| 59 | g.eachAttr(function(attr) {
|
|---|
| 60 | if (g.content.some(hasAnimatedAttr, attr.name)) return;
|
|---|
| 61 |
|
|---|
| 62 | if (!inner.hasAttr(attr.name)) {
|
|---|
| 63 | inner.addAttr(attr);
|
|---|
| 64 | } else if (attr.name == 'transform') {
|
|---|
| 65 | inner.attr(attr.name).value = attr.value + ' ' + inner.attr(attr.name).value;
|
|---|
| 66 | } else if (inner.hasAttr(attr.name, 'inherit')) {
|
|---|
| 67 | inner.attr(attr.name).value = attr.value;
|
|---|
| 68 | } else if (
|
|---|
| 69 | attrsInheritable.indexOf(attr.name) < 0 &&
|
|---|
| 70 | !inner.hasAttr(attr.name, attr.value)
|
|---|
| 71 | ) {
|
|---|
| 72 | return;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | g.removeAttr(attr.name);
|
|---|
| 76 | });
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | // collapse groups without attributes
|
|---|
| 81 | if (!g.hasAttr() && !g.content.some(function(item) { return item.isElem(animationElems) })) {
|
|---|
| 82 | item.spliceContent(i, 1, g.content);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | });
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|