1 | 'use strict';
|
2 |
|
3 | exports.type = 'perItemReverse';
|
4 |
|
5 | exports.active = true;
|
6 |
|
7 | exports.description = 'moves elements attributes to the existing group wrapper';
|
8 |
|
9 | var inheritableAttrs = require('./_collections').inheritableAttrs,
|
10 | pathElems = require('./_collections.js').pathElems;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | exports.fn = function(item) {
|
37 |
|
38 | if (item.isElem('g') && !item.isEmpty() && item.content.length > 1) {
|
39 |
|
40 | var intersection = {},
|
41 | hasTransform = false,
|
42 | hasClip = item.hasAttr('clip-path') || item.hasAttr('mask'),
|
43 | intersected = item.content.every(function(inner) {
|
44 | if (inner.isElem() && inner.hasAttr()) {
|
45 |
|
46 | if (inner.hasAttr('class')) return false;
|
47 | if (!Object.keys(intersection).length) {
|
48 | intersection = inner.attrs;
|
49 | } else {
|
50 | intersection = intersectInheritableAttrs(intersection, inner.attrs);
|
51 |
|
52 | if (!intersection) return false;
|
53 | }
|
54 |
|
55 | return true;
|
56 | }
|
57 | }),
|
58 | allPath = item.content.every(function(inner) {
|
59 | return inner.isElem(pathElems);
|
60 | });
|
61 |
|
62 | if (intersected) {
|
63 |
|
64 | item.content.forEach(function(g) {
|
65 |
|
66 | for (var name in intersection) {
|
67 |
|
68 | if (!allPath && !hasClip || name !== 'transform') {
|
69 |
|
70 | g.removeAttr(name);
|
71 |
|
72 | if (name === 'transform') {
|
73 | if (!hasTransform) {
|
74 | if (item.hasAttr('transform')) {
|
75 | item.attr('transform').value += ' ' + intersection[name].value;
|
76 | } else {
|
77 | item.addAttr(intersection[name]);
|
78 | }
|
79 |
|
80 | hasTransform = true;
|
81 | }
|
82 | } else {
|
83 | item.addAttr(intersection[name]);
|
84 | }
|
85 |
|
86 | }
|
87 | }
|
88 |
|
89 | });
|
90 |
|
91 | }
|
92 |
|
93 | }
|
94 |
|
95 | };
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function intersectInheritableAttrs(a, b) {
|
106 |
|
107 | var c = {};
|
108 |
|
109 | for (var n in a) {
|
110 | if (
|
111 | b.hasOwnProperty(n) &&
|
112 | inheritableAttrs.indexOf(n) > -1 &&
|
113 | a[n].name === b[n].name &&
|
114 | a[n].value === b[n].value &&
|
115 | a[n].prefix === b[n].prefix &&
|
116 | a[n].local === b[n].local
|
117 | ) {
|
118 | c[n] = a[n];
|
119 | }
|
120 | }
|
121 |
|
122 | if (!Object.keys(c).length) return false;
|
123 |
|
124 | return c;
|
125 |
|
126 | }
|