@iconify/tools 2.2.5 → 3.0.0-beta.1

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/download/api/download.cjs +4 -4
  12. package/lib/download/api/download.mjs +4 -4
  13. package/lib/download/api/index.cjs +0 -1
  14. package/lib/download/api/index.mjs +0 -1
  15. package/lib/download/git/index.cjs +1 -1
  16. package/lib/download/git/index.mjs +1 -1
  17. package/lib/download/git/reset.cjs +1 -1
  18. package/lib/download/git/reset.mjs +1 -1
  19. package/lib/download/github/hash.cjs +0 -1
  20. package/lib/download/github/hash.mjs +0 -1
  21. package/lib/download/github/index.cjs +1 -1
  22. package/lib/download/github/index.mjs +1 -1
  23. package/lib/download/gitlab/hash.cjs +0 -1
  24. package/lib/download/gitlab/hash.mjs +0 -1
  25. package/lib/download/gitlab/index.cjs +1 -1
  26. package/lib/download/gitlab/index.mjs +1 -1
  27. package/lib/download/helpers/unzip.cjs +5 -1
  28. package/lib/download/index.cjs +1 -1
  29. package/lib/download/index.mjs +1 -1
  30. package/lib/download/npm/index.cjs +1 -1
  31. package/lib/download/npm/index.mjs +1 -1
  32. package/lib/icon-set/index.cjs +18 -0
  33. package/lib/icon-set/index.d.ts +7 -1
  34. package/lib/icon-set/index.mjs +18 -0
  35. package/lib/icon-set/tags.cjs +2 -2
  36. package/lib/icon-set/tags.d.ts +1 -1
  37. package/lib/icon-set/tags.mjs +2 -2
  38. package/lib/icon-set/types.d.ts +5 -4
  39. package/lib/import/directory.cjs +101 -28
  40. package/lib/import/directory.d.ts +12 -5
  41. package/lib/import/directory.mjs +103 -31
  42. package/lib/import/figma/index.cjs +1 -2
  43. package/lib/import/figma/index.mjs +1 -2
  44. package/lib/import/figma/query.cjs +0 -1
  45. package/lib/import/figma/query.mjs +0 -1
  46. package/lib/index.cjs +9 -1
  47. package/lib/index.d.ts +7 -6
  48. package/lib/index.mjs +8 -7
  49. package/lib/misc/scan.cjs +61 -8
  50. package/lib/misc/scan.d.ts +12 -6
  51. package/lib/misc/scan.mjs +62 -10
  52. package/lib/optimise/origin.cjs +23 -0
  53. package/lib/optimise/origin.d.ts +10 -0
  54. package/lib/optimise/origin.mjs +21 -0
  55. package/lib/optimise/scale.cjs +17 -15
  56. package/lib/optimise/scale.mjs +17 -15
  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 +1 -1
  70. package/lib/svg/cleanup/root-style.d.ts +1 -5
  71. package/lib/svg/cleanup/root-style.mjs +2 -2
  72. package/lib/svg/cleanup/svgo-style.cjs +2 -2
  73. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  74. package/lib/svg/cleanup/svgo-style.mjs +3 -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 +149 -89
  80. package/lib/svg/parse-style.d.ts +6 -1
  81. package/lib/svg/parse-style.mjs +150 -91
  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 +23 -20
package/README.md CHANGED
@@ -50,7 +50,7 @@ import { parseColors, isEmptyColor } from '@iconify/tools/lib/colors/parse';
50
50
 
51
51
  // Clean up and optimise icons
52
52
  try {
53
- await cleanupSVG(svg);
53
+ cleanupSVG(svg);
54
54
  await parseColors(svg, {
55
55
  defaultColor: 'currentColor',
56
56
  callback: (attr, colorStr, color) => {
@@ -59,7 +59,7 @@ import { parseColors, isEmptyColor } from '@iconify/tools/lib/colors/parse';
59
59
  : 'currentColor';
60
60
  },
61
61
  });
62
- await runSVGO(svg);
62
+ runSVGO(svg);
63
63
  } catch (err) {
64
64
  // Invalid icon
65
65
  console.error(`Error parsing ${name}:`, err);
@@ -83,6 +83,12 @@ import { parseColors, isEmptyColor } from '@iconify/tools/lib/colors/parse';
83
83
 
84
84
  Full documentation is too big for simple README file. See [Iconify Tools documentation](https://docs.iconify.design/tools/tools2/) for detailed documentation with code samples.
85
85
 
86
+ ## Synchronous functions
87
+
88
+ Most functions in example above are asynchronous.
89
+
90
+ If you need to import or parse icons synchronously, such as in config file of package that does not support async configuration files, most functions have synchronous copies, such as `importDirectorySync()`.
91
+
86
92
  ## License
87
93
 
88
94
  Library is released with MIT license.
@@ -17,10 +17,10 @@ require('../svg/data/attributes.cjs');
17
17
  require('../svg/analyse.cjs');
18
18
  require('../svg/analyse/error.cjs');
19
19
 
20
- async function detectIconSetPalette(iconSet) {
20
+ function detectIconSetPalette(iconSet) {
21
21
  let palette;
22
- await iconSet.forEach(
23
- async (name) => {
22
+ iconSet.forEachSync(
23
+ (name) => {
24
24
  if (palette === null) {
25
25
  return;
26
26
  }
@@ -29,7 +29,7 @@ async function detectIconSetPalette(iconSet) {
29
29
  return;
30
30
  }
31
31
  let iconPalette;
32
- await colors_parse.parseColors(svg, {
32
+ colors_parse.parseColorsSync(svg, {
33
33
  callback: (attr, colorStr, color) => {
34
34
  if (!color) {
35
35
  iconPalette = null;
@@ -10,6 +10,6 @@ import '@iconify/utils/lib/icon-set/tree';
10
10
  *
11
11
  * Returns null if icon set has mixed colors
12
12
  */
13
- declare function detectIconSetPalette(iconSet: IconSet): Promise<boolean | null>;
13
+ declare function detectIconSetPalette(iconSet: IconSet): boolean | null;
14
14
 
15
15
  export { detectIconSetPalette };
@@ -1,4 +1,4 @@
1
- import { parseColors, isEmptyColor } from './parse.mjs';
1
+ import { parseColorsSync, isEmptyColor } from './parse.mjs';
2
2
  import '@iconify/utils/lib/colors';
3
3
  import '../svg/data/tags.mjs';
4
4
  import '../svg/parse-style.mjs';
@@ -15,10 +15,10 @@ import '../svg/data/attributes.mjs';
15
15
  import '../svg/analyse.mjs';
16
16
  import '../svg/analyse/error.mjs';
17
17
 
18
- async function detectIconSetPalette(iconSet) {
18
+ function detectIconSetPalette(iconSet) {
19
19
  let palette;
20
- await iconSet.forEach(
21
- async (name) => {
20
+ iconSet.forEachSync(
21
+ (name) => {
22
22
  if (palette === null) {
23
23
  return;
24
24
  }
@@ -27,7 +27,7 @@ async function detectIconSetPalette(iconSet) {
27
27
  return;
28
28
  }
29
29
  let iconPalette;
30
- await parseColors(svg, {
30
+ parseColorsSync(svg, {
31
31
  callback: (attr, colorStr, color) => {
32
32
  if (!color) {
33
33
  iconPalette = null;
@@ -18,7 +18,7 @@ require('../svg/analyse/error.cjs');
18
18
 
19
19
  const propsToCheck = Object.keys(colors_attribs.defaultColorValues);
20
20
  const animatePropsToCheck = ["from", "to", "values"];
21
- async function parseColors(svg, options = {}) {
21
+ function createContext(options, callback) {
22
22
  const result = {
23
23
  colors: [],
24
24
  hasUnsetColor: false,
@@ -49,12 +49,12 @@ async function parseColors(svg, options = {}) {
49
49
  itemColors[prop] = addedColor === null ? color : addedColor;
50
50
  }
51
51
  }
52
- function getElementColor(prop, item, elements2) {
52
+ function getElementColor(prop, item, elements) {
53
53
  function find(prop2) {
54
54
  let currentItem = item;
55
55
  const allowDefaultColor = colors_attribs.allowDefaultColorValue[prop2];
56
56
  while (currentItem) {
57
- const element = elements2.get(
57
+ const element = elements.get(
58
58
  currentItem.index
59
59
  );
60
60
  const color = element._colors?.[prop2];
@@ -79,67 +79,81 @@ async function parseColors(svg, options = {}) {
79
79
  }
80
80
  return propColor;
81
81
  }
82
- async function checkColor(prop, value, item) {
82
+ function checkColor(done, prop, value, item) {
83
83
  switch (value.trim().toLowerCase()) {
84
84
  case "":
85
85
  case "inherit":
86
- return;
86
+ return done();
87
87
  }
88
88
  const parsedColor = colors.stringToColor(value);
89
89
  const defaultValue = parsedColor || value;
90
90
  if (parsedColor?.type === "function" && parsedColor.func === "url") {
91
91
  addColorToItem(prop, defaultValue, item, false);
92
- return value;
92
+ return done(value);
93
93
  }
94
- if (!options.callback) {
94
+ if (!callback) {
95
95
  addColorToItem(prop, defaultValue, item);
96
- return value;
96
+ return done(value);
97
97
  }
98
- let callbackResult = options.callback(
99
- prop,
100
- value,
101
- parsedColor,
102
- item?.tagName,
103
- item
104
- );
105
- callbackResult = callbackResult instanceof Promise ? await callbackResult : callbackResult;
106
- switch (callbackResult) {
107
- case "remove": {
108
- return item ? callbackResult : void 0;
98
+ callback(
99
+ [prop, value, parsedColor, item?.tagName, item],
100
+ (callbackResult) => {
101
+ switch (callbackResult) {
102
+ case "remove": {
103
+ return done(item ? callbackResult : void 0);
104
+ }
105
+ case "unset":
106
+ return done();
107
+ }
108
+ if (callbackResult === value || parsedColor && callbackResult === parsedColor) {
109
+ addColorToItem(prop, defaultValue, item);
110
+ return done(value);
111
+ }
112
+ if (typeof callbackResult === "string") {
113
+ const newColor = colors.stringToColor(callbackResult);
114
+ addColorToItem(prop, newColor || callbackResult, item);
115
+ return done(callbackResult);
116
+ }
117
+ const newValue = colors.colorToString(callbackResult);
118
+ addColorToItem(prop, callbackResult, item);
119
+ return done(newValue);
109
120
  }
110
- case "unset":
111
- return;
112
- }
113
- if (callbackResult === value || parsedColor && callbackResult === parsedColor) {
114
- addColorToItem(prop, defaultValue, item);
115
- return value;
116
- }
117
- if (typeof callbackResult === "string") {
118
- const newColor = colors.stringToColor(callbackResult);
119
- addColorToItem(prop, newColor || callbackResult, item);
120
- return callbackResult;
121
- }
122
- const newValue = colors.colorToString(callbackResult);
123
- addColorToItem(prop, callbackResult, item);
124
- return newValue;
121
+ );
125
122
  }
126
- await svg_parseStyle.parseSVGStyle(svg, async (item) => {
123
+ function parseStyleItem(item, done) {
127
124
  const prop = item.prop;
128
125
  const value = item.value;
129
126
  if (propsToCheck.indexOf(prop) === -1) {
130
- return value;
127
+ return done(value);
131
128
  }
132
129
  const attr = prop;
133
- const newValue = await checkColor(attr, value);
134
- if (newValue === void 0) {
135
- return newValue;
136
- }
137
- if (item.type === "global") {
138
- result.hasGlobalStyle = true;
139
- }
140
- return newValue;
141
- });
142
- const iconData = await svg_analyse.analyseSVGStructure(svg, options);
130
+ checkColor(
131
+ (newValue) => {
132
+ if (newValue === void 0) {
133
+ return done(newValue);
134
+ }
135
+ if (item.type === "global") {
136
+ result.hasGlobalStyle = true;
137
+ }
138
+ return done(newValue);
139
+ },
140
+ attr,
141
+ value
142
+ );
143
+ }
144
+ return {
145
+ result,
146
+ defaultColor,
147
+ rawOptions: options,
148
+ findColor,
149
+ addColorToItem,
150
+ getElementColor,
151
+ checkColor,
152
+ parseStyleItem
153
+ };
154
+ }
155
+ function analyseSVG(svg, context, done) {
156
+ const iconData = svg_analyse.analyseSVGStructure(svg, context.rawOptions);
143
157
  const { elements, tree } = iconData;
144
158
  const cheerio = svg.$svg;
145
159
  const removedElements = /* @__PURE__ */ new Set();
@@ -164,15 +178,15 @@ async function parseColors(svg, options = {}) {
164
178
  removeChildren(element);
165
179
  cheerio(element).remove();
166
180
  }
167
- async function parseTreeItem(item) {
181
+ function parseTreeItem(item, done2) {
168
182
  const index = item.index;
169
183
  if (removedElements.has(index) || parsedElements.has(index)) {
170
- return;
184
+ return done2();
171
185
  }
172
186
  parsedElements.add(index);
173
187
  const element = elements.get(index);
174
188
  if (element._removed) {
175
- return;
189
+ return done2();
176
190
  }
177
191
  const { tagName, attribs } = element;
178
192
  if (item.parent) {
@@ -186,108 +200,239 @@ async function parseColors(svg, options = {}) {
186
200
  };
187
201
  }
188
202
  }
189
- for (let i = 0; i < propsToCheck.length; i++) {
190
- const prop = propsToCheck[i];
191
- if (prop === "fill" && svg_data_tags.animateTags.has(tagName)) {
192
- continue;
193
- }
194
- const value = attribs[prop];
195
- if (value !== void 0) {
196
- const newValue = await checkColor(prop, value, element);
197
- if (newValue !== value) {
198
- if (newValue === void 0) {
199
- cheerio(element).removeAttr(prop);
200
- if (element._colors) {
201
- delete element._colors[prop];
202
- }
203
- } else if (newValue === "remove") {
204
- removeElement(index, element);
205
- return;
206
- } else {
207
- cheerio(element).attr(prop, newValue);
208
- }
203
+ function parseCommonProps(done3) {
204
+ const propsQueue = [];
205
+ for (let i = 0; i < propsToCheck.length; i++) {
206
+ const prop = propsToCheck[i];
207
+ if (prop === "fill" && svg_data_tags.animateTags.has(tagName)) {
208
+ continue;
209
+ }
210
+ const value = attribs[prop];
211
+ if (value !== void 0) {
212
+ propsQueue.push([prop, value]);
209
213
  }
210
214
  }
211
- }
212
- if (svg_data_tags.animateTags.has(tagName)) {
213
- const attr = attribs.attributeName;
214
- if (propsToCheck.indexOf(attr) !== -1) {
215
- for (let i = 0; i < animatePropsToCheck.length; i++) {
216
- const elementProp = animatePropsToCheck[i];
217
- const fullValue = attribs[elementProp];
218
- if (typeof fullValue !== "string") {
219
- continue;
220
- }
221
- const splitValues = fullValue.split(";");
222
- let updatedValues = false;
223
- for (let j = 0; j < splitValues.length; j++) {
224
- const value = splitValues[j];
225
- if (value !== void 0) {
226
- const newValue = await checkColor(
227
- elementProp,
228
- value
229
- // Do not pass third parameter
230
- );
231
- if (newValue !== value) {
232
- updatedValues = true;
233
- splitValues[j] = typeof newValue === "string" ? newValue : "";
215
+ const parsePropsQueue = () => {
216
+ const queueItem = propsQueue.shift();
217
+ if (!queueItem) {
218
+ return done3();
219
+ }
220
+ const [prop, value] = queueItem;
221
+ context.checkColor(
222
+ (newValue) => {
223
+ if (newValue !== value) {
224
+ if (newValue === void 0) {
225
+ cheerio(element).removeAttr(prop);
226
+ if (element._colors) {
227
+ delete element._colors[prop];
228
+ }
229
+ } else if (newValue === "remove") {
230
+ removeElement(index, element);
231
+ } else {
232
+ cheerio(element).attr(prop, newValue);
234
233
  }
235
234
  }
235
+ return parsePropsQueue();
236
+ },
237
+ prop,
238
+ value,
239
+ element
240
+ );
241
+ };
242
+ parsePropsQueue();
243
+ }
244
+ function checkAnimations(done3) {
245
+ const propsQueue = [];
246
+ if (svg_data_tags.animateTags.has(tagName)) {
247
+ const attr = attribs.attributeName;
248
+ if (propsToCheck.indexOf(attr) !== -1) {
249
+ for (let i = 0; i < animatePropsToCheck.length; i++) {
250
+ const elementProp = animatePropsToCheck[i];
251
+ const fullValue = attribs[elementProp];
252
+ if (typeof fullValue !== "string") {
253
+ continue;
254
+ }
255
+ propsQueue.push([elementProp, fullValue]);
236
256
  }
257
+ }
258
+ }
259
+ const parsePropsQueue = () => {
260
+ const queueItem = propsQueue.shift();
261
+ if (!queueItem) {
262
+ return done3();
263
+ }
264
+ const [elementProp, fullValue] = queueItem;
265
+ const splitValues = fullValue.split(";");
266
+ let updatedValues = false;
267
+ const parsedAllItems = () => {
237
268
  if (updatedValues) {
238
269
  cheerio(element).attr(
239
270
  elementProp,
240
271
  splitValues.join(";")
241
272
  );
242
273
  }
243
- }
244
- }
274
+ return parsePropsQueue();
275
+ };
276
+ const parseItem = (index2) => {
277
+ if (index2 >= splitValues.length) {
278
+ return parsedAllItems();
279
+ }
280
+ const value = splitValues[index2];
281
+ if (value === void 0) {
282
+ return parseItem(index2 + 1);
283
+ }
284
+ context.checkColor(
285
+ (newValue) => {
286
+ if (newValue !== value) {
287
+ updatedValues = true;
288
+ splitValues[index2] = typeof newValue === "string" ? newValue : "";
289
+ }
290
+ parseItem(index2 + 1);
291
+ },
292
+ elementProp,
293
+ value
294
+ // Do not pass third parameter
295
+ );
296
+ };
297
+ parseItem(0);
298
+ };
299
+ parsePropsQueue();
245
300
  }
246
- if (!result.hasGlobalStyle) {
247
- let requiredProps;
248
- if (svg_data_tags.shapeTags.has(tagName)) {
249
- requiredProps = colors_attribs.shapeColorAttributes;
250
- }
251
- colors_attribs.specialColorAttributes.forEach((attr) => {
252
- if (svg_data_attributes.tagSpecificPresentationalAttributes[tagName]?.has(attr)) {
253
- requiredProps = [attr];
254
- }
255
- });
256
- if (requiredProps) {
257
- const itemColors = element._colors || (element._colors = {});
258
- for (let i = 0; i < requiredProps.length; i++) {
259
- const prop = requiredProps[i];
260
- const color = getElementColor(prop, item, elements);
261
- if (color === colors_attribs.defaultBlackColor) {
262
- if (defaultColor) {
263
- const defaultColorValue = typeof defaultColor === "function" ? defaultColor(
301
+ parseCommonProps(() => {
302
+ checkAnimations(() => {
303
+ if (!context.result.hasGlobalStyle) {
304
+ let requiredProps;
305
+ if (svg_data_tags.shapeTags.has(tagName)) {
306
+ requiredProps = colors_attribs.shapeColorAttributes;
307
+ }
308
+ colors_attribs.specialColorAttributes.forEach((attr) => {
309
+ if (svg_data_attributes.tagSpecificPresentationalAttributes[tagName]?.has(
310
+ attr
311
+ )) {
312
+ requiredProps = [attr];
313
+ }
314
+ });
315
+ if (requiredProps) {
316
+ const itemColors = element._colors || (element._colors = {});
317
+ for (let i = 0; i < requiredProps.length; i++) {
318
+ const prop = requiredProps[i];
319
+ const color = context.getElementColor(
264
320
  prop,
265
- element,
266
321
  item,
267
- iconData
268
- ) : defaultColor;
269
- findColor(defaultColorValue, true);
270
- cheerio(element).attr(
271
- prop,
272
- colors.colorToString(defaultColorValue)
322
+ elements
273
323
  );
274
- itemColors[prop] = defaultColorValue;
324
+ if (color === colors_attribs.defaultBlackColor) {
325
+ const defaultColor = context.defaultColor;
326
+ if (defaultColor) {
327
+ const defaultColorValue = typeof defaultColor === "function" ? defaultColor(
328
+ prop,
329
+ element,
330
+ item,
331
+ iconData
332
+ ) : defaultColor;
333
+ context.findColor(defaultColorValue, true);
334
+ cheerio(element).attr(
335
+ prop,
336
+ colors.colorToString(defaultColorValue)
337
+ );
338
+ itemColors[prop] = defaultColorValue;
339
+ } else {
340
+ context.result.hasUnsetColor = true;
341
+ }
342
+ }
343
+ }
344
+ }
345
+ }
346
+ let index2 = 0;
347
+ const parseChildItem = () => {
348
+ if (index2 >= item.children.length) {
349
+ return done2();
350
+ }
351
+ const childItem = item.children[index2];
352
+ index2++;
353
+ if (!childItem.usedAsMask) {
354
+ parseTreeItem(childItem, parseChildItem);
355
+ } else {
356
+ parseChildItem();
357
+ }
358
+ };
359
+ parseChildItem();
360
+ });
361
+ });
362
+ }
363
+ parseTreeItem(tree, done);
364
+ }
365
+ function parseColors(svg, options = {}) {
366
+ const callback = options.callback;
367
+ return new Promise((fulfill, reject) => {
368
+ let context;
369
+ try {
370
+ context = createContext(
371
+ options,
372
+ callback ? (params, done) => {
373
+ try {
374
+ const result = callback(...params);
375
+ if (result instanceof Promise) {
376
+ result.then(done).catch(reject);
275
377
  } else {
276
- result.hasUnsetColor = true;
378
+ done(result);
277
379
  }
380
+ } catch (err) {
381
+ reject(err);
278
382
  }
383
+ } : void 0
384
+ );
385
+ } catch (err) {
386
+ reject(err);
387
+ return;
388
+ }
389
+ svg_parseStyle.parseSVGStyle(svg, (item) => {
390
+ return new Promise((fulfill2, reject2) => {
391
+ try {
392
+ context.parseStyleItem(item, fulfill2);
393
+ } catch (err) {
394
+ reject2(err);
279
395
  }
396
+ });
397
+ }).then(() => {
398
+ try {
399
+ analyseSVG(svg, context, () => {
400
+ fulfill(context.result);
401
+ });
402
+ } catch (err) {
403
+ reject(err);
280
404
  }
281
- }
282
- for (let i = 0; i < item.children.length; i++) {
283
- const childItem = item.children[i];
284
- if (!childItem.usedAsMask) {
285
- await parseTreeItem(childItem);
405
+ }).catch(reject);
406
+ });
407
+ }
408
+ function parseColorsSync(svg, options = {}) {
409
+ const callback = options.callback;
410
+ const context = createContext(
411
+ options,
412
+ callback ? (params, done) => {
413
+ done(callback(...params));
414
+ } : void 0
415
+ );
416
+ svg_parseStyle.parseSVGStyleSync(svg, (item) => {
417
+ let isSync2 = true;
418
+ let result;
419
+ context.parseStyleItem(item, (value) => {
420
+ if (!isSync2) {
421
+ throw new Error("parseStyleItem callback supposed to be sync");
286
422
  }
423
+ result = value;
424
+ });
425
+ isSync2 = false;
426
+ return result;
427
+ });
428
+ let isSync = true;
429
+ analyseSVG(svg, context, () => {
430
+ if (!isSync) {
431
+ throw new Error("analyseSVG callback supposed to be sync");
287
432
  }
288
- }
289
- await parseTreeItem(tree);
290
- return result;
433
+ });
434
+ isSync = false;
435
+ return context.result;
291
436
  }
292
437
  function isEmptyColor(color) {
293
438
  const type = color.type;
@@ -296,3 +441,4 @@ function isEmptyColor(color) {
296
441
 
297
442
  exports.isEmptyColor = isEmptyColor;
298
443
  exports.parseColors = parseColors;
444
+ exports.parseColorsSync = parseColorsSync;
@@ -1,7 +1,7 @@
1
1
  import { Color } from '@iconify/utils/lib/colors/types';
2
2
  import { SVG } from '../svg/index.js';
3
3
  import { ColorAttributes } from './attribs.js';
4
- import { ElementsTreeItem, AnalyseSVGStructureResult, AnalyseSVGStructureOptions, ExtendedTagElement } from '../svg/analyse/types.js';
4
+ import { ElementsTreeItem, AnalyseSVGStructureResult, ExtendedTagElement, AnalyseSVGStructureOptions } from '../svg/analyse/types.js';
5
5
  import '@iconify/types';
6
6
  import '@iconify/utils/lib/customisations/defaults';
7
7
 
@@ -23,7 +23,7 @@ interface FindColorsResult {
23
23
  * - 'remove' to remove shape or rule
24
24
  */
25
25
  type ParseColorsCallbackResult = Color | string | 'remove' | 'unset';
26
- type ParseColorsCallback = (attr: ColorAttributes, colorString: string, parsedColor: Color | null, tagName?: string, item?: ExtendedTagElementWithColors) => ParseColorsCallbackResult | Promise<ParseColorsCallbackResult>;
26
+ type ParseColorsCallback<T> = (attr: ColorAttributes, colorString: string, parsedColor: Color | null, tagName?: string, item?: ExtendedTagElementWithColors) => T;
27
27
  /**
28
28
  * Callback for default color
29
29
  */
@@ -31,10 +31,12 @@ type ParseColorOptionsDefaultColorCallback = (prop: string, item: ExtendedTagEle
31
31
  /**
32
32
  * Options
33
33
  */
34
- interface ParseColorsOptions extends AnalyseSVGStructureOptions {
35
- callback?: ParseColorsCallback;
34
+ interface Options<T> extends AnalyseSVGStructureOptions {
35
+ callback?: T;
36
36
  defaultColor?: Color | string | ParseColorOptionsDefaultColorCallback;
37
37
  }
38
+ type ParseColorsOptions = Options<ParseColorsCallback<ParseColorsCallbackResult | Promise<ParseColorsCallbackResult>>>;
39
+ type ParseColorsSyncOptions = Options<ParseColorsCallback<ParseColorsCallbackResult>>;
38
40
  /**
39
41
  * Extend properties for element
40
42
  */
@@ -50,9 +52,16 @@ interface ExtendedTagElementWithColors extends ExtendedTagElement {
50
52
  * cleanupInlineStyle() or cleanupSVG(), otherwise results might be inaccurate
51
53
  */
52
54
  declare function parseColors(svg: SVG, options?: ParseColorsOptions): Promise<FindColorsResult>;
55
+ /**
56
+ * Find colors in icon, synchronous version
57
+ *
58
+ * Clean up icon before running this function to convert style to attributes using
59
+ * cleanupInlineStyle() or cleanupSVG(), otherwise results might be inaccurate
60
+ */
61
+ declare function parseColorsSync(svg: SVG, options?: ParseColorsSyncOptions): FindColorsResult;
53
62
  /**
54
63
  * Check if color is empty, such as 'none' or 'transparent'
55
64
  */
56
65
  declare function isEmptyColor(color: Color): boolean;
57
66
 
58
- export { ExtendedTagElementWithColors, FindColorsResult, ParseColorOptionsDefaultColorCallback, ParseColorsOptions, isEmptyColor, parseColors };
67
+ export { ExtendedTagElementWithColors, FindColorsResult, ParseColorOptionsDefaultColorCallback, ParseColorsOptions, ParseColorsSyncOptions, isEmptyColor, parseColors, parseColorsSync };