@iconify/tools 2.2.6 → 3.0.0-beta.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/README.md +8 -2
- package/lib/colors/detect.cjs +4 -4
- package/lib/colors/detect.d.ts +1 -1
- package/lib/colors/detect.mjs +5 -5
- package/lib/colors/parse.cjs +275 -129
- package/lib/colors/parse.d.ts +14 -5
- package/lib/colors/parse.mjs +278 -133
- package/lib/colors/validate.cjs +11 -2
- package/lib/colors/validate.d.ts +10 -2
- package/lib/colors/validate.mjs +12 -4
- package/lib/css/parser/export.cjs +1 -10
- package/lib/css/parser/export.mjs +1 -10
- package/lib/css/parser/text.cjs +6 -25
- package/lib/css/parser/text.mjs +6 -25
- package/lib/css/parser/types.d.ts +2 -3
- package/lib/download/api/download.cjs +4 -4
- package/lib/download/api/download.mjs +4 -4
- package/lib/download/api/index.cjs +0 -1
- package/lib/download/api/index.mjs +0 -1
- package/lib/download/git/index.cjs +1 -1
- package/lib/download/git/index.mjs +1 -1
- package/lib/download/git/reset.cjs +1 -1
- package/lib/download/git/reset.mjs +1 -1
- package/lib/download/github/hash.cjs +0 -1
- package/lib/download/github/hash.mjs +0 -1
- package/lib/download/github/index.cjs +1 -1
- package/lib/download/github/index.mjs +1 -1
- package/lib/download/gitlab/hash.cjs +0 -1
- package/lib/download/gitlab/hash.mjs +0 -1
- package/lib/download/gitlab/index.cjs +1 -1
- package/lib/download/gitlab/index.mjs +1 -1
- package/lib/download/helpers/unzip.cjs +5 -1
- package/lib/download/index.cjs +1 -1
- package/lib/download/index.mjs +1 -1
- package/lib/download/npm/index.cjs +1 -1
- package/lib/download/npm/index.mjs +1 -1
- package/lib/icon-set/index.cjs +18 -0
- package/lib/icon-set/index.d.ts +7 -1
- package/lib/icon-set/index.mjs +18 -0
- package/lib/icon-set/tags.cjs +2 -2
- package/lib/icon-set/tags.d.ts +1 -1
- package/lib/icon-set/tags.mjs +2 -2
- package/lib/icon-set/types.d.ts +5 -4
- package/lib/import/directory.cjs +101 -28
- package/lib/import/directory.d.ts +12 -5
- package/lib/import/directory.mjs +103 -31
- package/lib/import/figma/index.cjs +1 -2
- package/lib/import/figma/index.mjs +1 -2
- package/lib/import/figma/query.cjs +0 -1
- package/lib/import/figma/query.mjs +0 -1
- package/lib/index.cjs +7 -1
- package/lib/index.d.ts +6 -6
- package/lib/index.mjs +7 -7
- package/lib/misc/scan.cjs +61 -8
- package/lib/misc/scan.d.ts +12 -6
- package/lib/misc/scan.mjs +62 -10
- package/lib/svg/analyse.cjs +2 -2
- package/lib/svg/analyse.d.ts +1 -1
- package/lib/svg/analyse.mjs +4 -4
- package/lib/svg/cleanup/attribs.cjs +2 -2
- package/lib/svg/cleanup/attribs.d.ts +1 -1
- package/lib/svg/cleanup/attribs.mjs +3 -3
- package/lib/svg/cleanup/bad-tags.cjs +2 -2
- package/lib/svg/cleanup/bad-tags.d.ts +1 -1
- package/lib/svg/cleanup/bad-tags.mjs +3 -3
- package/lib/svg/cleanup/inline-style.cjs +2 -2
- package/lib/svg/cleanup/inline-style.d.ts +1 -1
- package/lib/svg/cleanup/inline-style.mjs +3 -3
- package/lib/svg/cleanup/root-style.cjs +19 -8
- package/lib/svg/cleanup/root-style.d.ts +5 -5
- package/lib/svg/cleanup/root-style.mjs +20 -9
- package/lib/svg/cleanup/svgo-style.cjs +5 -2
- package/lib/svg/cleanup/svgo-style.d.ts +1 -1
- package/lib/svg/cleanup/svgo-style.mjs +6 -3
- package/lib/svg/cleanup.cjs +6 -6
- package/lib/svg/cleanup.d.ts +1 -1
- package/lib/svg/cleanup.mjs +6 -6
- package/lib/svg/index.cjs +5 -1
- package/lib/svg/parse-style.cjs +266 -103
- package/lib/svg/parse-style.d.ts +21 -3
- package/lib/svg/parse-style.mjs +267 -105
- package/lib/svg/parse.cjs +56 -18
- package/lib/svg/parse.d.ts +8 -2
- package/lib/svg/parse.mjs +56 -19
- package/package.json +15 -17
package/lib/svg/parse-style.mjs
CHANGED
|
@@ -2,134 +2,296 @@ import { parseInlineStyle } from '../css/parse.mjs';
|
|
|
2
2
|
import { tokensToString } from '../css/parser/export.mjs';
|
|
3
3
|
import { getTokens } from '../css/parser/tokens.mjs';
|
|
4
4
|
import { tokensTree } from '../css/parser/tree.mjs';
|
|
5
|
-
import { parseSVG } from './parse.mjs';
|
|
5
|
+
import { parseSVG, parseSVGSync } from './parse.mjs';
|
|
6
6
|
import '../css/parser/error.mjs';
|
|
7
7
|
import '../css/parser/strings.mjs';
|
|
8
8
|
import '../css/parser/text.mjs';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
function parseItem(item, callback, done) {
|
|
11
|
+
const tagName = item.tagName;
|
|
12
|
+
const $element = item.$element;
|
|
13
|
+
function parseStyleItem(done2) {
|
|
14
|
+
const content = $element.text();
|
|
15
|
+
if (typeof content !== "string") {
|
|
16
|
+
$element.remove();
|
|
17
|
+
return done2();
|
|
18
|
+
}
|
|
19
|
+
const tokens = getTokens(content);
|
|
20
|
+
if (!(tokens instanceof Array)) {
|
|
21
|
+
throw new Error("Error parsing style");
|
|
22
|
+
}
|
|
23
|
+
let changed2 = false;
|
|
24
|
+
const selectorStart = [];
|
|
25
|
+
let newTokens = [];
|
|
26
|
+
const parsedTokens = () => {
|
|
27
|
+
if (changed2) {
|
|
28
|
+
const tree = tokensTree(
|
|
29
|
+
newTokens.filter((token) => token !== null)
|
|
30
|
+
);
|
|
31
|
+
if (!tree.length) {
|
|
32
|
+
$element.remove();
|
|
33
|
+
} else {
|
|
34
|
+
const newContent = tokensToString(tree);
|
|
35
|
+
item.$element.text("\n" + newContent);
|
|
36
|
+
}
|
|
19
37
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
38
|
+
done2();
|
|
39
|
+
};
|
|
40
|
+
const nextToken = () => {
|
|
41
|
+
const token = tokens.shift();
|
|
42
|
+
if (token === void 0) {
|
|
43
|
+
return parsedTokens();
|
|
23
44
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
28
|
-
const token = tokens[i];
|
|
29
|
-
switch (token.type) {
|
|
30
|
-
case "selector":
|
|
31
|
-
case "at-rule":
|
|
32
|
-
selectorStart.push(newTokens.length);
|
|
33
|
-
break;
|
|
34
|
-
case "close":
|
|
35
|
-
selectorStart.pop();
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
if (token.type !== "rule") {
|
|
45
|
+
switch (token.type) {
|
|
46
|
+
case "selector":
|
|
47
|
+
selectorStart.push(newTokens.length);
|
|
39
48
|
newTokens.push(token);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
token
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
return nextToken();
|
|
50
|
+
case "close":
|
|
51
|
+
selectorStart.pop();
|
|
52
|
+
newTokens.push(token);
|
|
53
|
+
return nextToken();
|
|
54
|
+
case "at-rule": {
|
|
55
|
+
selectorStart.push(newTokens.length);
|
|
56
|
+
const prop = token.rule;
|
|
57
|
+
const value = token.value;
|
|
58
|
+
const isAnimation = prop === "keyframes" || prop.slice(0, 1) === "-" && prop.split("-").pop() === "keyframes";
|
|
59
|
+
const childTokens = [];
|
|
60
|
+
const animationRules = /* @__PURE__ */ Object.create(null);
|
|
61
|
+
let depth = 1;
|
|
62
|
+
let index = 0;
|
|
63
|
+
let isFrom = false;
|
|
64
|
+
while (depth > 0) {
|
|
65
|
+
const childToken = tokens[index];
|
|
66
|
+
index++;
|
|
67
|
+
if (!childToken) {
|
|
68
|
+
throw new Error("Something went wrong parsing CSS");
|
|
69
|
+
}
|
|
70
|
+
childTokens.push(childToken);
|
|
71
|
+
switch (childToken.type) {
|
|
72
|
+
case "close": {
|
|
73
|
+
depth--;
|
|
74
|
+
isFrom = false;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case "selector": {
|
|
78
|
+
depth++;
|
|
79
|
+
if (isAnimation) {
|
|
80
|
+
const rule = childToken.code;
|
|
81
|
+
if (rule === "from" || rule === "0%") {
|
|
82
|
+
isFrom = true;
|
|
83
|
+
}
|
|
55
84
|
}
|
|
85
|
+
break;
|
|
56
86
|
}
|
|
57
|
-
|
|
87
|
+
case "at-rule": {
|
|
88
|
+
depth++;
|
|
89
|
+
if (isAnimation) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"Nested at-rule in keyframes ???"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case "rule": {
|
|
97
|
+
if (isAnimation && isFrom) {
|
|
98
|
+
animationRules[childToken.prop] = childToken.value;
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const skipCount = childTokens.length;
|
|
105
|
+
callback(
|
|
106
|
+
isAnimation ? {
|
|
107
|
+
type: "keyframes",
|
|
108
|
+
prop,
|
|
109
|
+
value,
|
|
110
|
+
token,
|
|
111
|
+
childTokens,
|
|
112
|
+
from: animationRules,
|
|
113
|
+
prevTokens: newTokens,
|
|
114
|
+
nextTokens: tokens.slice(0)
|
|
115
|
+
} : {
|
|
116
|
+
type: "at-rule",
|
|
117
|
+
prop,
|
|
118
|
+
value,
|
|
119
|
+
token,
|
|
120
|
+
childTokens,
|
|
121
|
+
prevTokens: newTokens,
|
|
122
|
+
nextTokens: tokens.slice(0)
|
|
58
123
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
124
|
+
(result) => {
|
|
125
|
+
if (result !== void 0) {
|
|
126
|
+
if (isAnimation) {
|
|
127
|
+
if (result !== value) {
|
|
128
|
+
changed2 = true;
|
|
129
|
+
token.value = result;
|
|
130
|
+
}
|
|
131
|
+
newTokens.push(token);
|
|
132
|
+
for (let i = 0; i < skipCount; i++) {
|
|
133
|
+
tokens.shift();
|
|
134
|
+
}
|
|
135
|
+
newTokens = newTokens.concat(childTokens);
|
|
136
|
+
} else {
|
|
137
|
+
if (result !== value) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
"Changing value for at-rule is not supported"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
newTokens.push(token);
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
changed2 = true;
|
|
146
|
+
for (let i = 0; i < skipCount; i++) {
|
|
147
|
+
tokens.shift();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
nextToken();
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
return;
|
|
66
154
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
155
|
+
case "rule": {
|
|
156
|
+
const value = token.value;
|
|
157
|
+
const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
|
|
158
|
+
callback(
|
|
159
|
+
{
|
|
160
|
+
type: "global",
|
|
161
|
+
prop: token.prop,
|
|
162
|
+
value,
|
|
163
|
+
token,
|
|
164
|
+
selectorTokens,
|
|
165
|
+
selectors: selectorTokens.reduce(
|
|
166
|
+
(prev, current) => {
|
|
167
|
+
switch (current.type) {
|
|
168
|
+
case "selector": {
|
|
169
|
+
return prev.concat(
|
|
170
|
+
current.selectors
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return prev;
|
|
175
|
+
},
|
|
176
|
+
[]
|
|
177
|
+
),
|
|
178
|
+
prevTokens: newTokens,
|
|
179
|
+
nextTokens: tokens.slice(0)
|
|
180
|
+
},
|
|
181
|
+
(result) => {
|
|
182
|
+
if (result !== void 0) {
|
|
183
|
+
if (result !== value) {
|
|
184
|
+
changed2 = true;
|
|
185
|
+
token.value = result;
|
|
186
|
+
}
|
|
187
|
+
newTokens.push(token);
|
|
188
|
+
} else {
|
|
189
|
+
changed2 = true;
|
|
190
|
+
}
|
|
191
|
+
nextToken();
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
return;
|
|
75
195
|
}
|
|
76
196
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
197
|
+
};
|
|
198
|
+
nextToken();
|
|
199
|
+
}
|
|
200
|
+
if (tagName === "style") {
|
|
201
|
+
return parseStyleItem(done);
|
|
202
|
+
}
|
|
203
|
+
const attribs = item.element.attribs;
|
|
204
|
+
if (attribs.style === void 0) {
|
|
205
|
+
return done();
|
|
206
|
+
}
|
|
207
|
+
const parsedStyle = parseInlineStyle(attribs.style);
|
|
208
|
+
if (parsedStyle === null) {
|
|
209
|
+
$element.removeAttr("style");
|
|
210
|
+
return done();
|
|
211
|
+
}
|
|
212
|
+
const propsQueue = Object.keys(parsedStyle);
|
|
213
|
+
let changed = false;
|
|
214
|
+
const parsedProps = () => {
|
|
215
|
+
if (changed) {
|
|
216
|
+
const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
|
|
217
|
+
if (!newStyle.length) {
|
|
218
|
+
$element.removeAttr("style");
|
|
219
|
+
} else {
|
|
220
|
+
$element.attr("style", newStyle);
|
|
86
221
|
}
|
|
87
|
-
const newContent = tokensToString(tree);
|
|
88
|
-
item.$element.text(newContent);
|
|
89
|
-
return;
|
|
90
222
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
223
|
+
done();
|
|
224
|
+
};
|
|
225
|
+
const nextProp = () => {
|
|
226
|
+
const prop = propsQueue.shift();
|
|
227
|
+
if (prop === void 0) {
|
|
228
|
+
return parsedProps();
|
|
94
229
|
}
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const props = Object.keys(parsedStyle);
|
|
101
|
-
let changed = false;
|
|
102
|
-
for (let i = 0; i < props.length; i++) {
|
|
103
|
-
const prop = props[i];
|
|
104
|
-
const value = parsedStyle[prop];
|
|
105
|
-
let result = callback({
|
|
230
|
+
const value = parsedStyle[prop];
|
|
231
|
+
callback(
|
|
232
|
+
{
|
|
106
233
|
type: "inline",
|
|
107
234
|
prop,
|
|
108
235
|
value,
|
|
109
236
|
item
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
result
|
|
237
|
+
},
|
|
238
|
+
(result) => {
|
|
239
|
+
if (result !== value) {
|
|
240
|
+
changed = true;
|
|
241
|
+
if (result === void 0) {
|
|
242
|
+
delete parsedStyle[prop];
|
|
243
|
+
} else {
|
|
244
|
+
parsedStyle[prop] = result;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
nextProp();
|
|
113
248
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
249
|
+
);
|
|
250
|
+
};
|
|
251
|
+
nextProp();
|
|
252
|
+
}
|
|
253
|
+
async function parseSVGStyle(svg, callback) {
|
|
254
|
+
return parseSVG(svg, (item) => {
|
|
255
|
+
return new Promise((fulfill, reject) => {
|
|
256
|
+
try {
|
|
257
|
+
parseItem(
|
|
258
|
+
item,
|
|
259
|
+
(styleItem, done) => {
|
|
260
|
+
try {
|
|
261
|
+
const result = callback(styleItem);
|
|
262
|
+
if (result instanceof Promise) {
|
|
263
|
+
result.then(done).catch(reject);
|
|
264
|
+
} else {
|
|
265
|
+
done(result);
|
|
266
|
+
}
|
|
267
|
+
} catch (err) {
|
|
268
|
+
reject(err);
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
fulfill
|
|
272
|
+
);
|
|
273
|
+
} catch (err) {
|
|
274
|
+
reject(err);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function parseSVGStyleSync(svg, callback) {
|
|
280
|
+
let isSync = true;
|
|
281
|
+
parseSVGSync(svg, (item) => {
|
|
282
|
+
parseItem(
|
|
283
|
+
item,
|
|
284
|
+
(styleItem, done) => {
|
|
285
|
+
done(callback(styleItem));
|
|
286
|
+
},
|
|
287
|
+
() => {
|
|
288
|
+
if (!isSync) {
|
|
289
|
+
throw new Error("parseSVGStyleSync callback was async");
|
|
120
290
|
}
|
|
121
291
|
}
|
|
122
|
-
|
|
123
|
-
if (!changed) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
|
|
127
|
-
if (!newStyle.length) {
|
|
128
|
-
$element.removeAttr("style");
|
|
129
|
-
} else {
|
|
130
|
-
$element.attr("style", newStyle);
|
|
131
|
-
}
|
|
292
|
+
);
|
|
132
293
|
});
|
|
294
|
+
isSync = false;
|
|
133
295
|
}
|
|
134
296
|
|
|
135
|
-
export { parseSVGStyle };
|
|
297
|
+
export { parseSVGStyle, parseSVGStyleSync };
|
package/lib/svg/parse.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
function parse(svg, callback, done) {
|
|
4
|
+
function checkNode(element, parents, done2) {
|
|
5
5
|
if (element.type !== "tag") {
|
|
6
|
-
return;
|
|
6
|
+
return done2();
|
|
7
7
|
}
|
|
8
8
|
const $element = cheerio(element);
|
|
9
9
|
const tagName = element.tagName;
|
|
@@ -16,25 +16,63 @@ async function parseSVG(svg, callback) {
|
|
|
16
16
|
testChildren: true,
|
|
17
17
|
removeNode: false
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const children = $element.children().toArray();
|
|
27
|
-
for (let i = 0; i < children.length; i++) {
|
|
28
|
-
await checkNode(children[i], newParents);
|
|
19
|
+
callback(item, () => {
|
|
20
|
+
const newParents = parents.slice(0);
|
|
21
|
+
newParents.unshift(item);
|
|
22
|
+
let queue = [];
|
|
23
|
+
if (tagName !== "style" && item.testChildren && !item.removeNode) {
|
|
24
|
+
const children = $element.children().toArray();
|
|
25
|
+
queue = children.slice(0);
|
|
29
26
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
const next = () => {
|
|
28
|
+
const queueItem = queue.shift();
|
|
29
|
+
if (!queueItem) {
|
|
30
|
+
if (item.removeNode) {
|
|
31
|
+
$element.remove();
|
|
32
|
+
}
|
|
33
|
+
return done2();
|
|
34
|
+
}
|
|
35
|
+
checkNode(queueItem, newParents, next);
|
|
36
|
+
};
|
|
37
|
+
next();
|
|
38
|
+
});
|
|
34
39
|
}
|
|
35
40
|
const cheerio = svg.$svg;
|
|
36
41
|
const $root = svg.$svg(":root");
|
|
37
|
-
|
|
42
|
+
checkNode($root.get(0), [], done);
|
|
43
|
+
}
|
|
44
|
+
function parseSVG(svg, callback) {
|
|
45
|
+
return new Promise((fulfill, reject) => {
|
|
46
|
+
parse(
|
|
47
|
+
svg,
|
|
48
|
+
(item, next) => {
|
|
49
|
+
const result = callback(item);
|
|
50
|
+
if (result instanceof Promise) {
|
|
51
|
+
result.then(next).catch(reject);
|
|
52
|
+
} else {
|
|
53
|
+
next();
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
fulfill
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function parseSVGSync(svg, callback) {
|
|
61
|
+
let isSync = true;
|
|
62
|
+
parse(
|
|
63
|
+
svg,
|
|
64
|
+
(item, next) => {
|
|
65
|
+
callback(item);
|
|
66
|
+
next();
|
|
67
|
+
},
|
|
68
|
+
() => {
|
|
69
|
+
if (!isSync) {
|
|
70
|
+
throw new Error("parseSVGSync callback was async");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
isSync = false;
|
|
38
75
|
}
|
|
39
76
|
|
|
40
77
|
exports.parseSVG = parseSVG;
|
|
78
|
+
exports.parseSVGSync = parseSVGSync;
|
package/lib/svg/parse.d.ts
CHANGED
|
@@ -18,7 +18,9 @@ interface ParseSVGCallbackItem {
|
|
|
18
18
|
/**
|
|
19
19
|
* Callback function
|
|
20
20
|
*/
|
|
21
|
-
type
|
|
21
|
+
type Callback<T> = (item: ParseSVGCallbackItem) => T;
|
|
22
|
+
type ParseSVGCallback = Callback<void | Promise<void>>;
|
|
23
|
+
type ParseSVGCallbackSync = Callback<void>;
|
|
22
24
|
/**
|
|
23
25
|
* Parse SVG
|
|
24
26
|
*
|
|
@@ -26,5 +28,9 @@ type ParseSVGCallback = (item: ParseSVGCallbackItem) => void | Promise<void>;
|
|
|
26
28
|
* Callback can be asynchronous.
|
|
27
29
|
*/
|
|
28
30
|
declare function parseSVG(svg: SVG, callback: ParseSVGCallback): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Sync version
|
|
33
|
+
*/
|
|
34
|
+
declare function parseSVGSync(svg: SVG, callback: ParseSVGCallbackSync): void;
|
|
29
35
|
|
|
30
|
-
export { ParseSVGCallback, ParseSVGCallbackItem, parseSVG };
|
|
36
|
+
export { ParseSVGCallback, ParseSVGCallbackItem, ParseSVGCallbackSync, parseSVG, parseSVGSync };
|
package/lib/svg/parse.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
function parse(svg, callback, done) {
|
|
2
|
+
function checkNode(element, parents, done2) {
|
|
3
3
|
if (element.type !== "tag") {
|
|
4
|
-
return;
|
|
4
|
+
return done2();
|
|
5
5
|
}
|
|
6
6
|
const $element = cheerio(element);
|
|
7
7
|
const tagName = element.tagName;
|
|
@@ -14,25 +14,62 @@ async function parseSVG(svg, callback) {
|
|
|
14
14
|
testChildren: true,
|
|
15
15
|
removeNode: false
|
|
16
16
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const children = $element.children().toArray();
|
|
25
|
-
for (let i = 0; i < children.length; i++) {
|
|
26
|
-
await checkNode(children[i], newParents);
|
|
17
|
+
callback(item, () => {
|
|
18
|
+
const newParents = parents.slice(0);
|
|
19
|
+
newParents.unshift(item);
|
|
20
|
+
let queue = [];
|
|
21
|
+
if (tagName !== "style" && item.testChildren && !item.removeNode) {
|
|
22
|
+
const children = $element.children().toArray();
|
|
23
|
+
queue = children.slice(0);
|
|
27
24
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
const next = () => {
|
|
26
|
+
const queueItem = queue.shift();
|
|
27
|
+
if (!queueItem) {
|
|
28
|
+
if (item.removeNode) {
|
|
29
|
+
$element.remove();
|
|
30
|
+
}
|
|
31
|
+
return done2();
|
|
32
|
+
}
|
|
33
|
+
checkNode(queueItem, newParents, next);
|
|
34
|
+
};
|
|
35
|
+
next();
|
|
36
|
+
});
|
|
32
37
|
}
|
|
33
38
|
const cheerio = svg.$svg;
|
|
34
39
|
const $root = svg.$svg(":root");
|
|
35
|
-
|
|
40
|
+
checkNode($root.get(0), [], done);
|
|
41
|
+
}
|
|
42
|
+
function parseSVG(svg, callback) {
|
|
43
|
+
return new Promise((fulfill, reject) => {
|
|
44
|
+
parse(
|
|
45
|
+
svg,
|
|
46
|
+
(item, next) => {
|
|
47
|
+
const result = callback(item);
|
|
48
|
+
if (result instanceof Promise) {
|
|
49
|
+
result.then(next).catch(reject);
|
|
50
|
+
} else {
|
|
51
|
+
next();
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
fulfill
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function parseSVGSync(svg, callback) {
|
|
59
|
+
let isSync = true;
|
|
60
|
+
parse(
|
|
61
|
+
svg,
|
|
62
|
+
(item, next) => {
|
|
63
|
+
callback(item);
|
|
64
|
+
next();
|
|
65
|
+
},
|
|
66
|
+
() => {
|
|
67
|
+
if (!isSync) {
|
|
68
|
+
throw new Error("parseSVGSync callback was async");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
isSync = false;
|
|
36
73
|
}
|
|
37
74
|
|
|
38
|
-
export { parseSVG };
|
|
75
|
+
export { parseSVG, parseSVGSync };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
|
|
5
5
|
"author": "Vjacheslav Trushkin",
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "3.0.0-beta.2",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"bugs": "https://github.com/iconify/tools/issues",
|
|
9
9
|
"homepage": "https://github.com/iconify/tools",
|
|
@@ -16,35 +16,33 @@
|
|
|
16
16
|
"types": "./lib/index.d.ts",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@iconify/types": "^2.0.0",
|
|
19
|
-
"@iconify/utils": "^2.1.
|
|
19
|
+
"@iconify/utils": "^2.1.5",
|
|
20
20
|
"@types/cheerio": "^0.22.31",
|
|
21
|
-
"@types/node-fetch": "^2.6.2",
|
|
22
21
|
"@types/tar": "^6.1.4",
|
|
23
22
|
"cheerio": "^1.0.0-rc.12",
|
|
24
23
|
"extract-zip": "^2.0.1",
|
|
25
24
|
"local-pkg": "^0.4.3",
|
|
26
|
-
"node-fetch": "^2.6.9",
|
|
27
25
|
"pathe": "^1.1.0",
|
|
28
26
|
"svgo": "^3.0.2",
|
|
29
27
|
"tar": "^6.1.13"
|
|
30
28
|
},
|
|
31
29
|
"devDependencies": {
|
|
32
|
-
"@types/jest": "^29.
|
|
33
|
-
"@types/node": "^18.
|
|
34
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
35
|
-
"@typescript-eslint/parser": "^5.
|
|
30
|
+
"@types/jest": "^29.5.0",
|
|
31
|
+
"@types/node": "^18.15.11",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^5.58.0",
|
|
33
|
+
"@typescript-eslint/parser": "^5.58.0",
|
|
36
34
|
"cross-env": "^7.0.3",
|
|
37
|
-
"eslint": "^8.
|
|
38
|
-
"eslint-config-prettier": "^8.
|
|
35
|
+
"eslint": "^8.38.0",
|
|
36
|
+
"eslint-config-prettier": "^8.8.0",
|
|
39
37
|
"eslint-plugin-jasmine": "^4.1.3",
|
|
40
38
|
"eslint-plugin-prettier": "^4.2.1",
|
|
41
|
-
"jasmine": "^4.
|
|
42
|
-
"jest": "^29.
|
|
43
|
-
"prettier": "^2.8.
|
|
44
|
-
"rimraf": "^
|
|
45
|
-
"ts-jest": "^29.0
|
|
46
|
-
"typescript": "^
|
|
47
|
-
"unbuild": "^1.1
|
|
39
|
+
"jasmine": "^4.6.0",
|
|
40
|
+
"jest": "^29.5.0",
|
|
41
|
+
"prettier": "^2.8.7",
|
|
42
|
+
"rimraf": "^5.0.0",
|
|
43
|
+
"ts-jest": "^29.1.0",
|
|
44
|
+
"typescript": "^5.0.4",
|
|
45
|
+
"unbuild": "^1.2.1"
|
|
48
46
|
},
|
|
49
47
|
"exports": {
|
|
50
48
|
"./*": "./*",
|