-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
convertOneStopGradients.js
168 lines (139 loc) · 4.56 KB
/
convertOneStopGradients.js
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
/**
* @typedef {import('../lib/types').XastElement} XastElement
* @typedef {import('../lib/types').XastParent} XastParent
*/
const { attrsGroupsDefaults, colorsProps } = require('./_collections');
const {
detachNodeFromParent,
querySelectorAll,
querySelector,
} = require('../lib/xast');
const { computeStyle, collectStylesheet } = require('../lib/style');
exports.name = 'convertOneStopGradients';
exports.description =
'converts one-stop (single color) gradients to a plain color';
/**
* Converts one-stop (single color) gradients to a plain color.
*
* @author Seth Falco <seth@falco.fun>
* @type {import('./plugins-types').Plugin<'convertOneStopGradients'>}
* @see https://developer.mozilla.org/docs/Web/SVG/Element/linearGradient
* @see https://developer.mozilla.org/docs/Web/SVG/Element/radialGradient
*/
exports.fn = (root) => {
const stylesheet = collectStylesheet(root);
/**
* Parent defs that had gradients elements removed from them.
*
* @type {Set<XastElement>}
*/
const effectedDefs = new Set();
/**
* @type {Map<XastElement, XastParent>}
*/
const allDefs = new Map();
/**
* @type {Map<XastElement, XastParent>}
*/
const gradientsToDetach = new Map();
/** Number of references to the xlink:href attribute. */
let xlinkHrefCount = 0;
return {
element: {
enter: (node, parentNode) => {
if (node.attributes['xlink:href'] != null) {
xlinkHrefCount++;
}
if (node.name === 'defs') {
allDefs.set(node, parentNode);
return;
}
if (node.name !== 'linearGradient' && node.name !== 'radialGradient') {
return;
}
const stops = node.children.filter((child) => {
return child.type === 'element' && child.name === 'stop';
});
const href = node.attributes['xlink:href'] || node.attributes['href'];
let effectiveNode =
stops.length === 0 && href != null && href.startsWith('#')
? querySelector(root, href)
: node;
if (effectiveNode == null || effectiveNode.type !== 'element') {
gradientsToDetach.set(node, parentNode);
return;
}
const effectiveStops = effectiveNode.children.filter((child) => {
return child.type === 'element' && child.name === 'stop';
});
if (
effectiveStops.length !== 1 ||
effectiveStops[0].type !== 'element'
) {
return;
}
if (parentNode.type === 'element' && parentNode.name === 'defs') {
effectedDefs.add(parentNode);
}
gradientsToDetach.set(node, parentNode);
let color;
const style = computeStyle(stylesheet, effectiveStops[0])['stop-color'];
if (style != null && style.type === 'static') {
color = style.value;
}
const selectorVal = `url(#${node.attributes.id})`;
const selector = colorsProps
.map((attr) => `[${attr}="${selectorVal}"]`)
.join(',');
const elements = querySelectorAll(root, selector);
for (const element of elements) {
if (element.type !== 'element') {
continue;
}
for (const attr of colorsProps) {
if (element.attributes[attr] !== selectorVal) {
continue;
}
if (color != null) {
element.attributes[attr] = color;
} else {
delete element.attributes[attr];
}
}
}
const styledElements = querySelectorAll(
root,
`[style*=${selectorVal}]`
);
for (const element of styledElements) {
if (element.type !== 'element') {
continue;
}
element.attributes.style = element.attributes.style.replace(
selectorVal,
color || attrsGroupsDefaults.presentation['stop-color']
);
}
},
exit: (node) => {
if (node.name === 'svg') {
for (const [gradient, parent] of gradientsToDetach.entries()) {
if (gradient.attributes['xlink:href'] != null) {
xlinkHrefCount--;
}
detachNodeFromParent(gradient, parent);
}
if (xlinkHrefCount === 0) {
delete node.attributes['xmlns:xlink'];
}
for (const [defs, parent] of allDefs.entries()) {
if (effectedDefs.has(defs) && defs.children.length === 0) {
detachNodeFromParent(defs, parent);
}
}
}
},
},
};
};