@iconify/tools 2.0.9 → 2.0.10

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.
@@ -1,5 +1,6 @@
1
1
  import type { Color } from '@iconify/utils/lib/colors/types';
2
2
  import type { SVG } from '../svg';
3
+ import { ParseSVGCallbackItem } from '../svg/parse';
3
4
  import { ColorAttributes } from './attribs';
4
5
  /**
5
6
  * Result
@@ -23,9 +24,17 @@ declare type ParseColorsCallback = (attr: ColorAttributes, colorString: string,
23
24
  /**
24
25
  * Options
25
26
  */
27
+ export declare type ParseColorOptionsDefaultColorCallback = (prop: string, item: ExtendedParseSVGCallbackItem) => Color;
26
28
  export interface ParseColorsOptions {
27
29
  callback?: ParseColorsCallback;
28
- defaultColor?: Color | string;
30
+ defaultColor?: Color | string | ParseColorOptionsDefaultColorCallback;
31
+ }
32
+ /**
33
+ * Extend properties for item
34
+ */
35
+ declare type ItemColors = Partial<Record<ColorAttributes, Color | string>>;
36
+ export interface ExtendedParseSVGCallbackItem extends ParseSVGCallbackItem {
37
+ colors?: ItemColors;
29
38
  }
30
39
  /**
31
40
  * Find colors in icon
@@ -95,6 +95,10 @@ async function parseColors(svg, options = {}) {
95
95
  // Resolve color
96
96
  const parsedColor = (0, colors_1.stringToColor)(value);
97
97
  const defaultValue = parsedColor || value;
98
+ // Ignore url()
99
+ if ((parsedColor === null || parsedColor === void 0 ? void 0 : parsedColor.type) === 'function' && parsedColor.func === 'url') {
100
+ return value;
101
+ }
98
102
  // Check if callback exists
99
103
  if (!options.callback) {
100
104
  addColorToItem(prop, defaultValue, item);
@@ -246,10 +250,13 @@ async function parseColors(svg, options = {}) {
246
250
  if (color === attribs_1.defaultBlackColor) {
247
251
  // Default black color: change it
248
252
  if (defaultColor) {
253
+ const defaultColorValue = typeof defaultColor === 'function'
254
+ ? defaultColor(prop, item)
255
+ : defaultColor;
249
256
  // Add color to results and change attribute
250
- findColor(defaultColor, true);
251
- $element.attr(prop, (0, colors_1.colorToString)(defaultColor));
252
- itemColors[prop] = defaultColor;
257
+ findColor(defaultColorValue, true);
258
+ $element.attr(prop, (0, colors_1.colorToString)(defaultColorValue));
259
+ itemColors[prop] = defaultColorValue;
253
260
  }
254
261
  else {
255
262
  result.hasUnsetColor = true;
@@ -77,6 +77,9 @@ async function parseColors(svg, options = {}) {
77
77
  }
78
78
  const parsedColor = stringToColor(value);
79
79
  const defaultValue = parsedColor || value;
80
+ if ((parsedColor == null ? void 0 : parsedColor.type) === "function" && parsedColor.func === "url") {
81
+ return value;
82
+ }
80
83
  if (!options.callback) {
81
84
  addColorToItem(prop, defaultValue, item);
82
85
  return value;
@@ -198,9 +201,10 @@ async function parseColors(svg, options = {}) {
198
201
  const color = getElementColor(prop, item);
199
202
  if (color === defaultBlackColor) {
200
203
  if (defaultColor) {
201
- findColor(defaultColor, true);
202
- $element.attr(prop, colorToString(defaultColor));
203
- itemColors[prop] = defaultColor;
204
+ const defaultColorValue = typeof defaultColor === "function" ? defaultColor(prop, item) : defaultColor;
205
+ findColor(defaultColorValue, true);
206
+ $element.attr(prop, colorToString(defaultColorValue));
207
+ itemColors[prop] = defaultColorValue;
204
208
  } else {
205
209
  result.hasUnsetColor = true;
206
210
  }
@@ -35,9 +35,12 @@ async function validateColors(svg, expectMonotone, options) {
35
35
  throw new Error('Unexpected color: ' + (0, colors_1.colorToString)(color));
36
36
  }
37
37
  return;
38
- // Do not allow other colors
39
38
  default:
40
- throw new Error('Unexpected color: ' + (0, colors_1.colorToString)(color));
39
+ // Allow url()
40
+ if (color.type !== 'function' || color.func !== 'url') {
41
+ // Do not allow other colors
42
+ throw new Error('Unexpected color: ' + (0, colors_1.colorToString)(color));
43
+ }
41
44
  }
42
45
  });
43
46
  return palette;
@@ -23,7 +23,9 @@ async function validateColors(svg, expectMonotone, options) {
23
23
  }
24
24
  return;
25
25
  default:
26
- throw new Error("Unexpected color: " + colorToString(color));
26
+ if (color.type !== "function" || color.func !== "url") {
27
+ throw new Error("Unexpected color: " + colorToString(color));
28
+ }
27
29
  }
28
30
  });
29
31
  return palette;
@@ -342,6 +342,7 @@ class IconSet {
342
342
  .forEach((item) => {
343
343
  const names = this.listCategory(item);
344
344
  if (names) {
345
+ names.sort((a, b) => a.localeCompare(b));
345
346
  categories[item.title] = names;
346
347
  }
347
348
  });
@@ -271,6 +271,7 @@ var IconSet = class {
271
271
  Array.from(this.categories).sort((a, b) => a.title.localeCompare(b.title)).forEach((item) => {
272
272
  const names2 = this.listCategory(item);
273
273
  if (names2) {
274
+ names2.sort((a, b) => a.localeCompare(b));
274
275
  categories[item.title] = names2;
275
276
  }
276
277
  });
@@ -77,7 +77,7 @@ async function cleanupSVGRoot(svg) {
77
77
  $root.removeAttr(attr);
78
78
  return;
79
79
  }
80
- console.log(`Removing unexpected attribute on SVG: ${attr}`);
80
+ console.warn(`Removing unexpected attribute on SVG: ${attr}`);
81
81
  $root.removeAttr(attr);
82
82
  });
83
83
  if (Object.keys(moveToChildren).length) {
@@ -60,7 +60,7 @@ async function cleanupSVGRoot(svg) {
60
60
  $root.removeAttr(attr);
61
61
  return;
62
62
  }
63
- console.log(`Removing unexpected attribute on SVG: ${attr}`);
63
+ console.warn(`Removing unexpected attribute on SVG: ${attr}`);
64
64
  $root.removeAttr(attr);
65
65
  });
66
66
  if (Object.keys(moveToChildren).length) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@iconify/tools",
3
3
  "description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
4
4
  "author": "Vjacheslav Trushkin",
5
- "version": "2.0.9",
5
+ "version": "2.0.10",
6
6
  "license": "MIT",
7
7
  "bugs": "https://github.com/iconify/tools/issues",
8
8
  "homepage": "https://github.com/iconify/tools",
@@ -22,7 +22,7 @@
22
22
  "test": "npm run test:jest && npm run test:jasmine"
23
23
  },
24
24
  "dependencies": {
25
- "@iconify/utils": "^1.0.21",
25
+ "@iconify/utils": "^1.0.22",
26
26
  "@types/cheerio": "^0.22.30",
27
27
  "@types/node-fetch": "^2.5.12",
28
28
  "@types/svgo": "^2.6.0",