@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.
Files changed (85) hide show
  1. package/README.md +8 -2
  2. package/lib/colors/detect.cjs +4 -4
  3. package/lib/colors/detect.d.ts +1 -1
  4. package/lib/colors/detect.mjs +5 -5
  5. package/lib/colors/parse.cjs +275 -129
  6. package/lib/colors/parse.d.ts +14 -5
  7. package/lib/colors/parse.mjs +278 -133
  8. package/lib/colors/validate.cjs +11 -2
  9. package/lib/colors/validate.d.ts +10 -2
  10. package/lib/colors/validate.mjs +12 -4
  11. package/lib/css/parser/export.cjs +1 -10
  12. package/lib/css/parser/export.mjs +1 -10
  13. package/lib/css/parser/text.cjs +6 -25
  14. package/lib/css/parser/text.mjs +6 -25
  15. package/lib/css/parser/types.d.ts +2 -3
  16. package/lib/download/api/download.cjs +4 -4
  17. package/lib/download/api/download.mjs +4 -4
  18. package/lib/download/api/index.cjs +0 -1
  19. package/lib/download/api/index.mjs +0 -1
  20. package/lib/download/git/index.cjs +1 -1
  21. package/lib/download/git/index.mjs +1 -1
  22. package/lib/download/git/reset.cjs +1 -1
  23. package/lib/download/git/reset.mjs +1 -1
  24. package/lib/download/github/hash.cjs +0 -1
  25. package/lib/download/github/hash.mjs +0 -1
  26. package/lib/download/github/index.cjs +1 -1
  27. package/lib/download/github/index.mjs +1 -1
  28. package/lib/download/gitlab/hash.cjs +0 -1
  29. package/lib/download/gitlab/hash.mjs +0 -1
  30. package/lib/download/gitlab/index.cjs +1 -1
  31. package/lib/download/gitlab/index.mjs +1 -1
  32. package/lib/download/helpers/unzip.cjs +5 -1
  33. package/lib/download/index.cjs +1 -1
  34. package/lib/download/index.mjs +1 -1
  35. package/lib/download/npm/index.cjs +1 -1
  36. package/lib/download/npm/index.mjs +1 -1
  37. package/lib/icon-set/index.cjs +18 -0
  38. package/lib/icon-set/index.d.ts +7 -1
  39. package/lib/icon-set/index.mjs +18 -0
  40. package/lib/icon-set/tags.cjs +2 -2
  41. package/lib/icon-set/tags.d.ts +1 -1
  42. package/lib/icon-set/tags.mjs +2 -2
  43. package/lib/icon-set/types.d.ts +5 -4
  44. package/lib/import/directory.cjs +101 -28
  45. package/lib/import/directory.d.ts +12 -5
  46. package/lib/import/directory.mjs +103 -31
  47. package/lib/import/figma/index.cjs +1 -2
  48. package/lib/import/figma/index.mjs +1 -2
  49. package/lib/import/figma/query.cjs +0 -1
  50. package/lib/import/figma/query.mjs +0 -1
  51. package/lib/index.cjs +7 -1
  52. package/lib/index.d.ts +6 -6
  53. package/lib/index.mjs +7 -7
  54. package/lib/misc/scan.cjs +61 -8
  55. package/lib/misc/scan.d.ts +12 -6
  56. package/lib/misc/scan.mjs +62 -10
  57. package/lib/svg/analyse.cjs +2 -2
  58. package/lib/svg/analyse.d.ts +1 -1
  59. package/lib/svg/analyse.mjs +4 -4
  60. package/lib/svg/cleanup/attribs.cjs +2 -2
  61. package/lib/svg/cleanup/attribs.d.ts +1 -1
  62. package/lib/svg/cleanup/attribs.mjs +3 -3
  63. package/lib/svg/cleanup/bad-tags.cjs +2 -2
  64. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  65. package/lib/svg/cleanup/bad-tags.mjs +3 -3
  66. package/lib/svg/cleanup/inline-style.cjs +2 -2
  67. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  68. package/lib/svg/cleanup/inline-style.mjs +3 -3
  69. package/lib/svg/cleanup/root-style.cjs +19 -8
  70. package/lib/svg/cleanup/root-style.d.ts +5 -5
  71. package/lib/svg/cleanup/root-style.mjs +20 -9
  72. package/lib/svg/cleanup/svgo-style.cjs +5 -2
  73. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  74. package/lib/svg/cleanup/svgo-style.mjs +6 -3
  75. package/lib/svg/cleanup.cjs +6 -6
  76. package/lib/svg/cleanup.d.ts +1 -1
  77. package/lib/svg/cleanup.mjs +6 -6
  78. package/lib/svg/index.cjs +5 -1
  79. package/lib/svg/parse-style.cjs +266 -103
  80. package/lib/svg/parse-style.d.ts +21 -3
  81. package/lib/svg/parse-style.mjs +267 -105
  82. package/lib/svg/parse.cjs +56 -18
  83. package/lib/svg/parse.d.ts +8 -2
  84. package/lib/svg/parse.mjs +56 -19
  85. package/package.json +15 -17
@@ -9,129 +9,292 @@ require('../css/parser/error.cjs');
9
9
  require('../css/parser/strings.cjs');
10
10
  require('../css/parser/text.cjs');
11
11
 
12
- async function parseSVGStyle(svg, callback) {
13
- return svg_parse.parseSVG(svg, async (item) => {
14
- const tagName = item.tagName;
15
- const $element = item.$element;
16
- if (tagName === "style") {
17
- const content = $element.text();
18
- if (typeof content !== "string") {
19
- $element.remove();
20
- return;
12
+ function parseItem(item, callback, done) {
13
+ const tagName = item.tagName;
14
+ const $element = item.$element;
15
+ function parseStyleItem(done2) {
16
+ const content = $element.text();
17
+ if (typeof content !== "string") {
18
+ $element.remove();
19
+ return done2();
20
+ }
21
+ const tokens = css_parser_tokens.getTokens(content);
22
+ if (!(tokens instanceof Array)) {
23
+ throw new Error("Error parsing style");
24
+ }
25
+ let changed2 = false;
26
+ const selectorStart = [];
27
+ let newTokens = [];
28
+ const parsedTokens = () => {
29
+ if (changed2) {
30
+ const tree = css_parser_tree.tokensTree(
31
+ newTokens.filter((token) => token !== null)
32
+ );
33
+ if (!tree.length) {
34
+ $element.remove();
35
+ } else {
36
+ const newContent = css_parser_export.tokensToString(tree);
37
+ item.$element.text("\n" + newContent);
38
+ }
21
39
  }
22
- const tokens = css_parser_tokens.getTokens(content);
23
- if (!(tokens instanceof Array)) {
24
- throw new Error("Error parsing style");
40
+ done2();
41
+ };
42
+ const nextToken = () => {
43
+ const token = tokens.shift();
44
+ if (token === void 0) {
45
+ return parsedTokens();
25
46
  }
26
- let changed2 = false;
27
- const selectorStart = [];
28
- const newTokens = [];
29
- for (let i = 0; i < tokens.length; i++) {
30
- const token = tokens[i];
31
- switch (token.type) {
32
- case "selector":
33
- case "at-rule":
34
- selectorStart.push(newTokens.length);
35
- break;
36
- case "close":
37
- selectorStart.pop();
38
- break;
39
- }
40
- if (token.type !== "rule") {
47
+ switch (token.type) {
48
+ case "selector":
49
+ selectorStart.push(newTokens.length);
41
50
  newTokens.push(token);
42
- continue;
43
- }
44
- const value = token.value;
45
- const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
46
- let result = callback({
47
- type: "global",
48
- prop: token.prop,
49
- value,
50
- token,
51
- selectorTokens,
52
- selectors: selectorTokens.reduce(
53
- (prev, current) => {
54
- switch (current.type) {
55
- case "selector": {
56
- return prev.concat(current.selectors);
51
+ return nextToken();
52
+ case "close":
53
+ selectorStart.pop();
54
+ newTokens.push(token);
55
+ return nextToken();
56
+ case "at-rule": {
57
+ selectorStart.push(newTokens.length);
58
+ const prop = token.rule;
59
+ const value = token.value;
60
+ const isAnimation = prop === "keyframes" || prop.slice(0, 1) === "-" && prop.split("-").pop() === "keyframes";
61
+ const childTokens = [];
62
+ const animationRules = /* @__PURE__ */ Object.create(null);
63
+ let depth = 1;
64
+ let index = 0;
65
+ let isFrom = false;
66
+ while (depth > 0) {
67
+ const childToken = tokens[index];
68
+ index++;
69
+ if (!childToken) {
70
+ throw new Error("Something went wrong parsing CSS");
71
+ }
72
+ childTokens.push(childToken);
73
+ switch (childToken.type) {
74
+ case "close": {
75
+ depth--;
76
+ isFrom = false;
77
+ break;
78
+ }
79
+ case "selector": {
80
+ depth++;
81
+ if (isAnimation) {
82
+ const rule = childToken.code;
83
+ if (rule === "from" || rule === "0%") {
84
+ isFrom = true;
85
+ }
57
86
  }
87
+ break;
58
88
  }
59
- return prev;
89
+ case "at-rule": {
90
+ depth++;
91
+ if (isAnimation) {
92
+ throw new Error(
93
+ "Nested at-rule in keyframes ???"
94
+ );
95
+ }
96
+ break;
97
+ }
98
+ case "rule": {
99
+ if (isAnimation && isFrom) {
100
+ animationRules[childToken.prop] = childToken.value;
101
+ }
102
+ break;
103
+ }
104
+ }
105
+ }
106
+ const skipCount = childTokens.length;
107
+ callback(
108
+ isAnimation ? {
109
+ type: "keyframes",
110
+ prop,
111
+ value,
112
+ token,
113
+ childTokens,
114
+ from: animationRules,
115
+ prevTokens: newTokens,
116
+ nextTokens: tokens.slice(0)
117
+ } : {
118
+ type: "at-rule",
119
+ prop,
120
+ value,
121
+ token,
122
+ childTokens,
123
+ prevTokens: newTokens,
124
+ nextTokens: tokens.slice(0)
60
125
  },
61
- []
62
- ),
63
- prevTokens: newTokens,
64
- nextTokens: tokens.slice(i + 1)
65
- });
66
- if (result instanceof Promise) {
67
- result = await result;
126
+ (result) => {
127
+ if (result !== void 0) {
128
+ if (isAnimation) {
129
+ if (result !== value) {
130
+ changed2 = true;
131
+ token.value = result;
132
+ }
133
+ newTokens.push(token);
134
+ for (let i = 0; i < skipCount; i++) {
135
+ tokens.shift();
136
+ }
137
+ newTokens = newTokens.concat(childTokens);
138
+ } else {
139
+ if (result !== value) {
140
+ throw new Error(
141
+ "Changing value for at-rule is not supported"
142
+ );
143
+ }
144
+ newTokens.push(token);
145
+ }
146
+ } else {
147
+ changed2 = true;
148
+ for (let i = 0; i < skipCount; i++) {
149
+ tokens.shift();
150
+ }
151
+ }
152
+ nextToken();
153
+ }
154
+ );
155
+ return;
68
156
  }
69
- if (result !== void 0) {
70
- if (result !== value) {
71
- changed2 = true;
72
- token.value = result;
73
- }
74
- newTokens.push(token);
75
- } else {
76
- changed2 = true;
157
+ case "rule": {
158
+ const value = token.value;
159
+ const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
160
+ callback(
161
+ {
162
+ type: "global",
163
+ prop: token.prop,
164
+ value,
165
+ token,
166
+ selectorTokens,
167
+ selectors: selectorTokens.reduce(
168
+ (prev, current) => {
169
+ switch (current.type) {
170
+ case "selector": {
171
+ return prev.concat(
172
+ current.selectors
173
+ );
174
+ }
175
+ }
176
+ return prev;
177
+ },
178
+ []
179
+ ),
180
+ prevTokens: newTokens,
181
+ nextTokens: tokens.slice(0)
182
+ },
183
+ (result) => {
184
+ if (result !== void 0) {
185
+ if (result !== value) {
186
+ changed2 = true;
187
+ token.value = result;
188
+ }
189
+ newTokens.push(token);
190
+ } else {
191
+ changed2 = true;
192
+ }
193
+ nextToken();
194
+ }
195
+ );
196
+ return;
77
197
  }
78
198
  }
79
- if (!changed2) {
80
- return;
81
- }
82
- const tree = css_parser_tree.tokensTree(
83
- newTokens.filter((token) => token !== null)
84
- );
85
- if (!tree.length) {
86
- $element.remove();
87
- return;
199
+ };
200
+ nextToken();
201
+ }
202
+ if (tagName === "style") {
203
+ return parseStyleItem(done);
204
+ }
205
+ const attribs = item.element.attribs;
206
+ if (attribs.style === void 0) {
207
+ return done();
208
+ }
209
+ const parsedStyle = css_parse.parseInlineStyle(attribs.style);
210
+ if (parsedStyle === null) {
211
+ $element.removeAttr("style");
212
+ return done();
213
+ }
214
+ const propsQueue = Object.keys(parsedStyle);
215
+ let changed = false;
216
+ const parsedProps = () => {
217
+ if (changed) {
218
+ const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
219
+ if (!newStyle.length) {
220
+ $element.removeAttr("style");
221
+ } else {
222
+ $element.attr("style", newStyle);
88
223
  }
89
- const newContent = css_parser_export.tokensToString(tree);
90
- item.$element.text(newContent);
91
- return;
92
224
  }
93
- const attribs = item.element.attribs;
94
- if (attribs.style === void 0) {
95
- return;
225
+ done();
226
+ };
227
+ const nextProp = () => {
228
+ const prop = propsQueue.shift();
229
+ if (prop === void 0) {
230
+ return parsedProps();
96
231
  }
97
- const parsedStyle = css_parse.parseInlineStyle(attribs.style);
98
- if (parsedStyle === null) {
99
- $element.removeAttr("style");
100
- return;
101
- }
102
- const props = Object.keys(parsedStyle);
103
- let changed = false;
104
- for (let i = 0; i < props.length; i++) {
105
- const prop = props[i];
106
- const value = parsedStyle[prop];
107
- let result = callback({
232
+ const value = parsedStyle[prop];
233
+ callback(
234
+ {
108
235
  type: "inline",
109
236
  prop,
110
237
  value,
111
238
  item
112
- });
113
- if (result instanceof Promise) {
114
- result = await result;
239
+ },
240
+ (result) => {
241
+ if (result !== value) {
242
+ changed = true;
243
+ if (result === void 0) {
244
+ delete parsedStyle[prop];
245
+ } else {
246
+ parsedStyle[prop] = result;
247
+ }
248
+ }
249
+ nextProp();
115
250
  }
116
- if (result !== value) {
117
- changed = true;
118
- if (result === void 0) {
119
- delete parsedStyle[prop];
120
- } else {
121
- parsedStyle[prop] = result;
251
+ );
252
+ };
253
+ nextProp();
254
+ }
255
+ async function parseSVGStyle(svg, callback) {
256
+ return svg_parse.parseSVG(svg, (item) => {
257
+ return new Promise((fulfill, reject) => {
258
+ try {
259
+ parseItem(
260
+ item,
261
+ (styleItem, done) => {
262
+ try {
263
+ const result = callback(styleItem);
264
+ if (result instanceof Promise) {
265
+ result.then(done).catch(reject);
266
+ } else {
267
+ done(result);
268
+ }
269
+ } catch (err) {
270
+ reject(err);
271
+ }
272
+ },
273
+ fulfill
274
+ );
275
+ } catch (err) {
276
+ reject(err);
277
+ }
278
+ });
279
+ });
280
+ }
281
+ function parseSVGStyleSync(svg, callback) {
282
+ let isSync = true;
283
+ svg_parse.parseSVGSync(svg, (item) => {
284
+ parseItem(
285
+ item,
286
+ (styleItem, done) => {
287
+ done(callback(styleItem));
288
+ },
289
+ () => {
290
+ if (!isSync) {
291
+ throw new Error("parseSVGStyleSync callback was async");
122
292
  }
123
293
  }
124
- }
125
- if (!changed) {
126
- return;
127
- }
128
- const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
129
- if (!newStyle.length) {
130
- $element.removeAttr("style");
131
- } else {
132
- $element.attr("style", newStyle);
133
- }
294
+ );
134
295
  });
296
+ isSync = false;
135
297
  }
136
298
 
137
299
  exports.parseSVGStyle = parseSVGStyle;
300
+ exports.parseSVGStyleSync = parseSVGStyleSync;
@@ -1,5 +1,5 @@
1
1
  import { SVG } from './index.js';
2
- import { CSSRuleToken, CSSToken } from '../css/parser/types.js';
2
+ import { CSSRuleToken, CSSToken, CSSAtRuleToken } from '../css/parser/types.js';
3
3
  import { ParseSVGCallbackItem } from './parse.js';
4
4
  import '@iconify/types';
5
5
  import '@iconify/utils/lib/customisations/defaults';
@@ -24,7 +24,20 @@ interface ParseSVGStyleCallbackItemGlobal extends ParseSVGStyleCallbackItemCommo
24
24
  prevTokens: (CSSToken | null)[];
25
25
  nextTokens: CSSToken[];
26
26
  }
27
- type ParseSVGStyleCallbackItem = ParseSVGStyleCallbackItemInline | ParseSVGStyleCallbackItemGlobal;
27
+ interface ParseSVGStyleCallbackItemGlobalAtRule extends ParseSVGStyleCallbackItemCommon {
28
+ token: CSSAtRuleToken;
29
+ childTokens: CSSToken[];
30
+ prevTokens: (CSSToken | null)[];
31
+ nextTokens: CSSToken[];
32
+ }
33
+ interface ParseSVGStyleCallbackItemGlobalGenericAtRule extends ParseSVGStyleCallbackItemGlobalAtRule {
34
+ type: 'at-rule';
35
+ }
36
+ interface ParseSVGStyleCallbackItemGlobalKeyframesAtRule extends ParseSVGStyleCallbackItemGlobalAtRule {
37
+ type: 'keyframes';
38
+ from: Record<string, string>;
39
+ }
40
+ type ParseSVGStyleCallbackItem = ParseSVGStyleCallbackItemInline | ParseSVGStyleCallbackItemGlobal | ParseSVGStyleCallbackItemGlobalGenericAtRule | ParseSVGStyleCallbackItemGlobalKeyframesAtRule;
28
41
  /**
29
42
  * Result: undefined to remove item, string to change/keep item
30
43
  */
@@ -33,6 +46,7 @@ type ParseSVGStyleCallbackResult = string | undefined;
33
46
  * Callback function
34
47
  */
35
48
  type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult | Promise<ParseSVGStyleCallbackResult>;
49
+ type ParseSVGStyleCallbackSync = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult;
36
50
  /**
37
51
  * Parse styles in SVG
38
52
  *
@@ -41,5 +55,9 @@ type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleC
41
55
  * Callback can be asynchronous.
42
56
  */
43
57
  declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): Promise<void>;
58
+ /**
59
+ * Synchronous version
60
+ */
61
+ declare function parseSVGStyleSync(svg: SVG, callback: ParseSVGStyleCallbackSync): void;
44
62
 
45
- export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, parseSVGStyle };
63
+ export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, ParseSVGStyleCallbackSync, parseSVGStyle, parseSVGStyleSync };