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 |
|
15 | return item.isElem(animationElems) && item.hasAttr('attributeName', this) ||
|
16 | !item.isEmpty() && item.content.some(hasAnimatedAttr, this);
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | exports.fn = function(item) {
|
43 |
|
44 |
|
45 | if (item.isElem() && !item.isElem('switch') && !item.isEmpty()) {
|
46 | item.content.forEach(function(g, i) {
|
47 |
|
48 | if (g.isElem('g') && !g.isEmpty()) {
|
49 |
|
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 |
|
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 | };
|