@iconify/tools 1.3.17 → 2.0.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +13 -0
- package/.eslintignore +2 -0
- package/lib/colors/attribs.d.ts +16 -0
- package/lib/colors/attribs.js +26 -0
- package/lib/colors/attribs.mjs +28 -0
- package/lib/colors/parse.d.ts +37 -0
- package/lib/colors/parse.js +261 -0
- package/lib/colors/parse.mjs +212 -0
- package/lib/css/parse.d.ts +4 -0
- package/lib/css/parse.js +23 -0
- package/lib/css/parse.mjs +20 -0
- package/lib/css/parser/error.d.ts +11 -0
- package/lib/css/parser/error.js +27 -0
- package/lib/css/parser/error.mjs +23 -0
- package/lib/css/parser/export.d.ts +5 -0
- package/lib/css/parser/export.js +69 -0
- package/lib/css/parser/export.mjs +46 -0
- package/lib/css/parser/strings.d.ts +13 -0
- package/lib/css/parser/strings.js +93 -0
- package/lib/css/parser/strings.mjs +74 -0
- package/lib/css/parser/text.d.ts +17 -0
- package/lib/css/parser/text.js +174 -0
- package/lib/css/parser/text.mjs +133 -0
- package/lib/css/parser/tokens.d.ts +6 -0
- package/lib/css/parser/tokens.js +200 -0
- package/lib/css/parser/tokens.mjs +186 -0
- package/lib/css/parser/tree.d.ts +5 -0
- package/lib/css/parser/tree.js +44 -0
- package/lib/css/parser/tree.mjs +40 -0
- package/lib/css/parser/types.d.ts +51 -0
- package/lib/css/parser/types.js +2 -0
- package/lib/css/parser/types.mjs +0 -0
- package/lib/icon-set/index.d.ts +134 -0
- package/lib/icon-set/index.js +776 -0
- package/lib/icon-set/index.mjs +617 -0
- package/lib/icon-set/match.d.ts +6 -0
- package/lib/icon-set/match.js +66 -0
- package/lib/icon-set/match.mjs +55 -0
- package/lib/icon-set/merge.d.ts +5 -0
- package/lib/icon-set/merge.js +91 -0
- package/lib/icon-set/merge.mjs +75 -0
- package/lib/icon-set/props.d.ts +10 -0
- package/lib/icon-set/props.js +33 -0
- package/lib/icon-set/props.mjs +25 -0
- package/lib/icon-set/types.d.ts +68 -0
- package/lib/icon-set/types.js +2 -0
- package/lib/icon-set/types.mjs +0 -0
- package/lib/import/directory.d.ts +35 -0
- package/lib/import/directory.js +59 -0
- package/lib/import/directory.mjs +47 -0
- package/lib/misc/keyword.d.ts +4 -0
- package/lib/misc/keyword.js +31 -0
- package/lib/misc/keyword.mjs +17 -0
- package/lib/misc/scan.d.ts +24 -0
- package/lib/misc/scan.js +48 -0
- package/lib/misc/scan.mjs +43 -0
- package/lib/optimise/flags.d.ts +5 -0
- package/lib/optimise/flags.js +303 -0
- package/lib/optimise/flags.mjs +241 -0
- package/lib/optimise/scale.d.ts +5 -0
- package/lib/optimise/scale.js +42 -0
- package/lib/optimise/scale.mjs +22 -0
- package/lib/optimise/svgo.d.ts +27 -0
- package/lib/optimise/svgo.js +88 -0
- package/lib/optimise/svgo.mjs +75 -0
- package/lib/svg/cleanup/attribs.d.ts +5 -0
- package/lib/svg/cleanup/attribs.js +43 -0
- package/lib/svg/cleanup/attribs.mjs +36 -0
- package/lib/svg/cleanup/bad-tags.d.ts +5 -0
- package/lib/svg/cleanup/bad-tags.js +69 -0
- package/lib/svg/cleanup/bad-tags.mjs +68 -0
- package/lib/svg/cleanup/inline-style.d.ts +5 -0
- package/lib/svg/cleanup/inline-style.js +77 -0
- package/lib/svg/cleanup/inline-style.mjs +65 -0
- package/lib/svg/cleanup/root-svg.d.ts +5 -0
- package/lib/svg/cleanup/root-svg.js +106 -0
- package/lib/svg/cleanup/root-svg.mjs +88 -0
- package/lib/svg/cleanup/svgo-style.d.ts +5 -0
- package/lib/svg/cleanup/svgo-style.js +35 -0
- package/lib/svg/cleanup/svgo-style.mjs +29 -0
- package/lib/svg/cleanup.d.ts +5 -0
- package/lib/svg/cleanup.js +24 -0
- package/lib/svg/cleanup.mjs +16 -0
- package/lib/svg/data/attributes.d.ts +71 -0
- package/lib/svg/data/attributes.js +403 -0
- package/lib/svg/data/attributes.mjs +352 -0
- package/lib/svg/data/tags.d.ts +89 -0
- package/lib/svg/data/tags.js +185 -0
- package/lib/svg/data/tags.mjs +136 -0
- package/lib/svg/index.d.ts +33 -0
- package/lib/svg/index.js +122 -0
- package/lib/svg/index.mjs +85 -0
- package/lib/svg/parse-style.d.ts +40 -0
- package/lib/svg/parse-style.js +131 -0
- package/lib/svg/parse-style.mjs +109 -0
- package/lib/svg/parse.d.ts +30 -0
- package/lib/svg/parse.js +49 -0
- package/lib/svg/parse.mjs +40 -0
- package/package.json +167 -13
- package/README.md +0 -576
- package/license.txt +0 -21
- package/sample/parse.js +0 -74
- package/sample/source/icon-close.svg +0 -15
- package/sample/source/icon-confirm.svg +0 -14
- package/sample/source/icon-search.svg +0 -16
- package/src/collection.js +0 -641
- package/src/colors/change_palette.js +0 -227
- package/src/colors/get_palette.js +0 -143
- package/src/colors/opacify.js +0 -195
- package/src/export/component.js +0 -482
- package/src/export/dir.js +0 -109
- package/src/export/json.js +0 -329
- package/src/export/phantomjs.js +0 -76
- package/src/export/phantomjs_script.js +0 -125
- package/src/export/png.js +0 -193
- package/src/export/svg.js +0 -55
- package/src/export/templates/component.md +0 -79
- package/src/export/templates/info.md +0 -3
- package/src/export/templates/sample-react-1.md +0 -21
- package/src/export/templates/sample-react-2.md +0 -15
- package/src/export/templates/sample-react.md +0 -11
- package/src/export/templates/sample-svelte.md +0 -11
- package/src/export/templates/sample-svelte1.md +0 -22
- package/src/export/templates/sample-svelte2.md +0 -13
- package/src/export/templates/sample-vue-0.md +0 -30
- package/src/export/templates/sample-vue-1.md +0 -25
- package/src/export/templates/sample-vue-2.md +0 -27
- package/src/export/templates/sample-vue.md +0 -28
- package/src/helpers.js +0 -43
- package/src/import/dir.js +0 -234
- package/src/import/font.js +0 -402
- package/src/import/json.js +0 -200
- package/src/import/svg.js +0 -60
- package/src/import/web_icons.js +0 -248
- package/src/modules.js +0 -50
- package/src/optimize/crop.js +0 -554
- package/src/optimize/crop_script.js +0 -525
- package/src/optimize/flags.js +0 -430
- package/src/optimize/scale.js +0 -72
- package/src/optimize/svgo.js +0 -161
- package/src/optimize/tags.js +0 -522
- package/src/shapes/convert.js +0 -264
- package/src/shapes/index.js +0 -135
- package/src/shapes/length.js +0 -707
- package/src/shapes/length_script.js +0 -105
- package/src/shapes/options.js +0 -60
- package/src/svg.js +0 -162
package/src/optimize/tags.js
DELETED
|
@@ -1,522 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the @iconify/tools package.
|
|
3
|
-
*
|
|
4
|
-
* (c) Vjacheslav Trushkin <cyberalien@gmail.com>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const cheerio = require('cheerio');
|
|
13
|
-
const Color = require('cyberalien-color');
|
|
14
|
-
const Tokenizer = require('simple-tokenizer');
|
|
15
|
-
|
|
16
|
-
const defaults = {
|
|
17
|
-
'strict-tags-validation': true,
|
|
18
|
-
'allowFont': false,
|
|
19
|
-
'browserCSSPrefixes': ['-webkit-', '-moz-', '-ms-', '-o-', '-inkscape-'],
|
|
20
|
-
'debug': false,
|
|
21
|
-
'log': console.log, // function for logging
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const complexSelectorTest = /[\+~\[\]\*>]|\S+[\.#]\S+/;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Optimize tags
|
|
28
|
-
*
|
|
29
|
-
* Must run SVGO optimization before running tags optimization!
|
|
30
|
-
*
|
|
31
|
-
* @param {SVG} svg SVG object
|
|
32
|
-
* @param {object} options Options
|
|
33
|
-
* @return {Promise}
|
|
34
|
-
*/
|
|
35
|
-
module.exports = (svg, options) => {
|
|
36
|
-
// Set options
|
|
37
|
-
options = options === void 0 ? Object.create(null) : options;
|
|
38
|
-
Object.keys(defaults).forEach((key) => {
|
|
39
|
-
if (options[key] === void 0) {
|
|
40
|
-
options[key] = defaults[key];
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Return promise object
|
|
45
|
-
return new Promise((fulfill, reject) => {
|
|
46
|
-
let $root = svg.$svg(':root'),
|
|
47
|
-
rootAttributes = $root.get(0).attribs,
|
|
48
|
-
styles = Object.create(null),
|
|
49
|
-
usedSelectors = Object.create(null),
|
|
50
|
-
keepSelectors = Object.create(null),
|
|
51
|
-
complexSelectors = false,
|
|
52
|
-
styleTags = [],
|
|
53
|
-
xlink = false;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Check color
|
|
57
|
-
*
|
|
58
|
-
* @param value
|
|
59
|
-
*/
|
|
60
|
-
function checkColor(value) {
|
|
61
|
-
if (value === '' || value === 'none') {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let clr = Color.fromString(value);
|
|
66
|
-
if (clr === null && options['strict-tags-validation']) {
|
|
67
|
-
value = value.toLowerCase();
|
|
68
|
-
if (
|
|
69
|
-
value === 'transparent' ||
|
|
70
|
-
value === 'currentcolor' ||
|
|
71
|
-
value.slice(0, 3) === 'url'
|
|
72
|
-
) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
throw new Error('Invalid color value: "' + value + '"');
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Removed used styles
|
|
81
|
-
*
|
|
82
|
-
* @param $tag
|
|
83
|
-
* @return {boolean}
|
|
84
|
-
*/
|
|
85
|
-
function removeUsedStyles($tag) {
|
|
86
|
-
let tokens = new Tokenizer({
|
|
87
|
-
splitRules: true,
|
|
88
|
-
}).tree($tag.text()),
|
|
89
|
-
changed = false;
|
|
90
|
-
|
|
91
|
-
tokens.forEach((token, index) => {
|
|
92
|
-
if (token.token !== '{' || !token.selectors) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
let unused = false;
|
|
97
|
-
token.selectors.forEach((selector) => {
|
|
98
|
-
if (usedSelectors[selector] === void 0 || keepSelectors[selector]) {
|
|
99
|
-
unused = true;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
if (!unused) {
|
|
103
|
-
tokens[index] = null;
|
|
104
|
-
changed = true;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
if (!changed) {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
tokens = tokens.filter((token) => token !== null);
|
|
113
|
-
if (!tokens.length) {
|
|
114
|
-
$tag.remove();
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
$tag.text(Tokenizer.build(tokens));
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Check style
|
|
124
|
-
*
|
|
125
|
-
* @param {string} code
|
|
126
|
-
* @param {boolean} inline
|
|
127
|
-
*/
|
|
128
|
-
function checkStyle(code, inline) {
|
|
129
|
-
let tokens = new Tokenizer({
|
|
130
|
-
splitRules: true,
|
|
131
|
-
}).tokenize(code);
|
|
132
|
-
|
|
133
|
-
let selectors = false;
|
|
134
|
-
|
|
135
|
-
tokens.forEach((token) => {
|
|
136
|
-
switch (token.token) {
|
|
137
|
-
case '{':
|
|
138
|
-
if (token.selectors) {
|
|
139
|
-
selectors = token.selectors;
|
|
140
|
-
}
|
|
141
|
-
return;
|
|
142
|
-
|
|
143
|
-
case '}':
|
|
144
|
-
selectors = false;
|
|
145
|
-
return;
|
|
146
|
-
|
|
147
|
-
case 'rule':
|
|
148
|
-
let key = token.key.toLowerCase();
|
|
149
|
-
switch (key) {
|
|
150
|
-
case 'stop-color':
|
|
151
|
-
case 'fill':
|
|
152
|
-
case 'stroke':
|
|
153
|
-
// Check color value
|
|
154
|
-
let result = checkColor(token.value.toLowerCase());
|
|
155
|
-
if (typeof result === 'string') {
|
|
156
|
-
token.value = result;
|
|
157
|
-
}
|
|
158
|
-
break;
|
|
159
|
-
|
|
160
|
-
default:
|
|
161
|
-
// Remove css rules that aren't supposed to be in SVG file (leftovers from bad editors)
|
|
162
|
-
let key = token.key;
|
|
163
|
-
options.browserCSSPrefixes.forEach((prefix) => {
|
|
164
|
-
if (key.slice(0, prefix.length) === prefix) {
|
|
165
|
-
key = key.slice(prefix.length);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
let list = key.split('-');
|
|
169
|
-
switch (list[0]) {
|
|
170
|
-
case 'fill':
|
|
171
|
-
case 'stroke':
|
|
172
|
-
case 'clip':
|
|
173
|
-
case 'transform':
|
|
174
|
-
case 'stop':
|
|
175
|
-
case 'shape':
|
|
176
|
-
break;
|
|
177
|
-
|
|
178
|
-
case 'font':
|
|
179
|
-
case 'line':
|
|
180
|
-
case 'text':
|
|
181
|
-
case 'mix':
|
|
182
|
-
case 'block':
|
|
183
|
-
case 'isolation':
|
|
184
|
-
case 'white':
|
|
185
|
-
case 'word':
|
|
186
|
-
case 'solid':
|
|
187
|
-
token.token = 'ignore';
|
|
188
|
-
return;
|
|
189
|
-
|
|
190
|
-
case 'marker':
|
|
191
|
-
if (token.value === 'none') {
|
|
192
|
-
token.token = 'ignore';
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
break;
|
|
196
|
-
|
|
197
|
-
default:
|
|
198
|
-
if (options['strict-tags-validation']) {
|
|
199
|
-
throw new Error(
|
|
200
|
-
'Invalid css rule: ' + token.key + ':' + token.value
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (!inline && selectors !== false && !complexSelectors) {
|
|
207
|
-
selectors.forEach((selector) => {
|
|
208
|
-
// Check for complex selectors
|
|
209
|
-
if (complexSelectorTest.test(selector)) {
|
|
210
|
-
complexSelectors = true;
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (styles[selector] === void 0) {
|
|
215
|
-
styles[selector] = Object.create(null);
|
|
216
|
-
}
|
|
217
|
-
styles[selector][key] = token;
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
return;
|
|
221
|
-
|
|
222
|
-
default:
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
return Tokenizer.build(tokens, { minify: true });
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Check shape
|
|
232
|
-
*
|
|
233
|
-
* @param {object} $node
|
|
234
|
-
* @param {object} node
|
|
235
|
-
* @param {object} extra
|
|
236
|
-
*/
|
|
237
|
-
function checkShape($node, node, extra) {
|
|
238
|
-
let nodeAttributes = node.attribs;
|
|
239
|
-
|
|
240
|
-
function expandClass(name) {
|
|
241
|
-
let style = styles[name];
|
|
242
|
-
Object.keys(style).forEach((key) => {
|
|
243
|
-
if (nodeAttributes[key] === void 0 || style[key].important) {
|
|
244
|
-
$node.attr(key, style[key].value);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
usedSelectors[name] = true;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Check attributes
|
|
251
|
-
if (nodeAttributes) {
|
|
252
|
-
// Expand style
|
|
253
|
-
if (nodeAttributes.style !== void 0) {
|
|
254
|
-
let value = checkStyle(nodeAttributes.style, true);
|
|
255
|
-
$node.removeAttr('style');
|
|
256
|
-
|
|
257
|
-
// Tokenize and move style to attributes
|
|
258
|
-
let tokens = new Tokenizer({
|
|
259
|
-
splitRules: true,
|
|
260
|
-
}).tree(value);
|
|
261
|
-
tokens.forEach((token) => {
|
|
262
|
-
if (token.token === 'rule') {
|
|
263
|
-
let key = token.key.toLowerCase();
|
|
264
|
-
if (nodeAttributes[key] === void 0) {
|
|
265
|
-
$node.attr(key, token.value);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// Expand class and id
|
|
272
|
-
if (!complexSelectors) {
|
|
273
|
-
if (
|
|
274
|
-
nodeAttributes['class'] !== void 0 &&
|
|
275
|
-
styles['.' + nodeAttributes['class']] !== void 0
|
|
276
|
-
) {
|
|
277
|
-
let classList = nodeAttributes['class'].split(/\s+/g);
|
|
278
|
-
if (classList.length > 1) {
|
|
279
|
-
// More than one selector - do nothing, mark selectors as undeletable
|
|
280
|
-
classList.forEach((className) => {
|
|
281
|
-
keepSelectors['.' + className] = true;
|
|
282
|
-
});
|
|
283
|
-
} else {
|
|
284
|
-
expandClass('.' + nodeAttributes['class']);
|
|
285
|
-
$node.removeAttr('class');
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (
|
|
289
|
-
nodeAttributes['id'] !== void 0 &&
|
|
290
|
-
styles['#' + nodeAttributes['id']] !== void 0
|
|
291
|
-
) {
|
|
292
|
-
expandClass('#' + nodeAttributes['id']);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Check other attributes
|
|
297
|
-
Object.keys(nodeAttributes).forEach((attr) => {
|
|
298
|
-
let value = nodeAttributes[attr],
|
|
299
|
-
attrib = attr.toLowerCase();
|
|
300
|
-
|
|
301
|
-
//noinspection FallThroughInSwitchStatementJS
|
|
302
|
-
switch (attrib) {
|
|
303
|
-
case 'stop-color':
|
|
304
|
-
if (!extra.gradient) {
|
|
305
|
-
throw new Error(
|
|
306
|
-
'Unexpected attribute "' + attr + '" outside of gradient'
|
|
307
|
-
);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
case 'fill':
|
|
311
|
-
case 'stroke':
|
|
312
|
-
checkColor(value.toLowerCase());
|
|
313
|
-
break;
|
|
314
|
-
|
|
315
|
-
default:
|
|
316
|
-
if (attrib.slice(0, 2) === 'on') {
|
|
317
|
-
// Possible JavaScript?
|
|
318
|
-
throw new Error('Unexpected attribute "' + attr + '"');
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
let list = attrib.split('-');
|
|
322
|
-
switch (list[0]) {
|
|
323
|
-
// Clean up BPMN junk
|
|
324
|
-
case 'font':
|
|
325
|
-
case 'line':
|
|
326
|
-
case 'text':
|
|
327
|
-
case 'mix':
|
|
328
|
-
case 'block':
|
|
329
|
-
case 'isolation':
|
|
330
|
-
case 'overflow':
|
|
331
|
-
case 'white':
|
|
332
|
-
case 'word':
|
|
333
|
-
case 'color':
|
|
334
|
-
$node.removeAttr(attr);
|
|
335
|
-
return;
|
|
336
|
-
|
|
337
|
-
case 'marker':
|
|
338
|
-
if (value === 'none') {
|
|
339
|
-
$node.removeAttr(attr);
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
break;
|
|
343
|
-
|
|
344
|
-
default:
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
checkChildElements($node, extra);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Check child elements
|
|
355
|
-
*
|
|
356
|
-
* @param {object} $parent
|
|
357
|
-
* @param {object} extra
|
|
358
|
-
*/
|
|
359
|
-
function checkChildElements($parent, extra) {
|
|
360
|
-
$parent.children().each((index, child) => {
|
|
361
|
-
let $child = cheerio(child),
|
|
362
|
-
shape = true;
|
|
363
|
-
|
|
364
|
-
//noinspection FallThroughInSwitchStatementJS
|
|
365
|
-
switch (child.tagName) {
|
|
366
|
-
case 'style':
|
|
367
|
-
let value = checkStyle($child.text(), false);
|
|
368
|
-
if (typeof value === 'string') {
|
|
369
|
-
if (!value.length) {
|
|
370
|
-
$child.remove();
|
|
371
|
-
return;
|
|
372
|
-
} else {
|
|
373
|
-
$child.text(value);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
styleTags.push($child);
|
|
377
|
-
return;
|
|
378
|
-
|
|
379
|
-
case 'g':
|
|
380
|
-
case 'switch':
|
|
381
|
-
checkChildElements($child, extra);
|
|
382
|
-
|
|
383
|
-
// Remove empty tags
|
|
384
|
-
if (!$child.children().length) {
|
|
385
|
-
$child.remove();
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
shape = false;
|
|
389
|
-
|
|
390
|
-
case 'path':
|
|
391
|
-
case 'circle':
|
|
392
|
-
case 'line':
|
|
393
|
-
case 'polygon':
|
|
394
|
-
case 'polyline':
|
|
395
|
-
case 'rect':
|
|
396
|
-
case 'ellipse':
|
|
397
|
-
checkShape($child, child, extra);
|
|
398
|
-
return;
|
|
399
|
-
|
|
400
|
-
case 'text':
|
|
401
|
-
case 'tspan':
|
|
402
|
-
if (!options.allowFont) {
|
|
403
|
-
throw new Error('Unexpected tag "' + child.tagName + '"');
|
|
404
|
-
}
|
|
405
|
-
checkShape($child, child, extra);
|
|
406
|
-
return;
|
|
407
|
-
|
|
408
|
-
case 'linearGradient':
|
|
409
|
-
case 'radialGradient':
|
|
410
|
-
checkShape(
|
|
411
|
-
$child,
|
|
412
|
-
child,
|
|
413
|
-
Object.assign(Object.create(null), extra, { gradient: true })
|
|
414
|
-
);
|
|
415
|
-
return;
|
|
416
|
-
|
|
417
|
-
case 'stop':
|
|
418
|
-
if (!extra.gradient) {
|
|
419
|
-
throw new Error('Unexpected tag "' + child.tagName + '"');
|
|
420
|
-
}
|
|
421
|
-
checkShape($child, child, extra);
|
|
422
|
-
return;
|
|
423
|
-
|
|
424
|
-
case 'use':
|
|
425
|
-
xlink = true;
|
|
426
|
-
case 'mask':
|
|
427
|
-
case 'defs':
|
|
428
|
-
case 'clipPath':
|
|
429
|
-
case 'symbol':
|
|
430
|
-
case 'pattern':
|
|
431
|
-
checkChildElements($child, extra);
|
|
432
|
-
return;
|
|
433
|
-
|
|
434
|
-
case 'font':
|
|
435
|
-
if (!options.allowFont) {
|
|
436
|
-
throw new Error('Unexpected tag "' + child.tagName + '"');
|
|
437
|
-
}
|
|
438
|
-
// Ignore font
|
|
439
|
-
return;
|
|
440
|
-
|
|
441
|
-
case 'filter':
|
|
442
|
-
// Ignore filter
|
|
443
|
-
return;
|
|
444
|
-
|
|
445
|
-
default:
|
|
446
|
-
throw new Error('Unexpected tag "' + child.tagName + '"');
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// Check root attributes
|
|
452
|
-
let groupAttributes = Object.create(null);
|
|
453
|
-
|
|
454
|
-
Object.keys(rootAttributes).forEach((attr) => {
|
|
455
|
-
if (
|
|
456
|
-
attr.toLowerCase().slice(0, 6) === 'stroke' ||
|
|
457
|
-
attr.toLowerCase().slice(0, 4) === 'fill'
|
|
458
|
-
) {
|
|
459
|
-
groupAttributes[attr] = rootAttributes[attr];
|
|
460
|
-
$root.removeAttr(attr);
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
if (attr.toLowerCase().slice(0, 4) === 'aria') {
|
|
464
|
-
$root.removeAttr(attr);
|
|
465
|
-
return;
|
|
466
|
-
}
|
|
467
|
-
switch (attr.toLowerCase()) {
|
|
468
|
-
case 'id':
|
|
469
|
-
$root.removeAttr(attr);
|
|
470
|
-
break;
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
if (Object.keys(groupAttributes).length) {
|
|
475
|
-
let group = '<g';
|
|
476
|
-
Object.keys(groupAttributes).forEach((attr) => {
|
|
477
|
-
group += ' ' + attr + '="' + groupAttributes[attr] + '"';
|
|
478
|
-
});
|
|
479
|
-
$root.html(group + '>' + $root.html() + '</g>');
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// Do stuff
|
|
483
|
-
try {
|
|
484
|
-
checkChildElements($root, Object.create(null));
|
|
485
|
-
} catch (err) {
|
|
486
|
-
reject(err);
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// Clean up root element attributes
|
|
491
|
-
let attributes = {
|
|
492
|
-
xmlns: 'http://www.w3.org/2000/svg',
|
|
493
|
-
preserveAspectRatio: 'xMidYMid meet',
|
|
494
|
-
width: svg.width,
|
|
495
|
-
height: svg.height,
|
|
496
|
-
viewBox:
|
|
497
|
-
rootAttributes.viewBox === void 0
|
|
498
|
-
? svg.left + ' ' + svg.top + ' ' + svg.width + ' ' + svg.height
|
|
499
|
-
: rootAttributes.viewBox,
|
|
500
|
-
};
|
|
501
|
-
if (xlink) {
|
|
502
|
-
attributes['xmlns:xlink'] = 'http://www.w3.org/1999/xlink';
|
|
503
|
-
}
|
|
504
|
-
Object.keys(attributes).forEach((attr) => {
|
|
505
|
-
$root.attr(attr, attributes[attr]);
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
// Remove styles that were changed to inline attributes
|
|
509
|
-
if (!complexSelectors && Object.keys(usedSelectors).length) {
|
|
510
|
-
styleTags.forEach(($tag) => {
|
|
511
|
-
if (!removeUsedStyles($tag) && options.debug) {
|
|
512
|
-
options.log('Style attribute was found');
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
if (complexSelectors && options.debug) {
|
|
517
|
-
options.log('Style with complex selectors was found.');
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
fulfill(svg);
|
|
521
|
-
});
|
|
522
|
-
};
|