@iconify/tools 2.0.8 → 2.0.12

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 (49) hide show
  1. package/lib/colors/parse.d.ts +19 -3
  2. package/lib/colors/parse.js +109 -40
  3. package/lib/colors/parse.mjs +86 -39
  4. package/lib/colors/validate.js +5 -2
  5. package/lib/colors/validate.mjs +3 -1
  6. package/lib/icon-set/index.js +4 -21
  7. package/lib/icon-set/index.mjs +6 -21
  8. package/lib/icon-set/merge.js +0 -1
  9. package/lib/icon-set/merge.mjs +1 -2
  10. package/lib/icon-set/types.d.ts +3 -1
  11. package/lib/index.d.ts +2 -0
  12. package/lib/index.js +5 -1
  13. package/lib/index.mjs +4 -0
  14. package/lib/optimise/flags.js +9 -0
  15. package/lib/optimise/flags.mjs +8 -0
  16. package/lib/optimise/global-style.d.ts +5 -0
  17. package/lib/optimise/global-style.js +158 -0
  18. package/lib/optimise/global-style.mjs +129 -0
  19. package/lib/optimise/svgo.js +1 -1
  20. package/lib/optimise/svgo.mjs +0 -1
  21. package/lib/svg/analyse/error.d.ts +5 -0
  22. package/lib/svg/analyse/error.js +22 -0
  23. package/lib/svg/analyse/error.mjs +16 -0
  24. package/lib/svg/analyse/types.d.ts +89 -0
  25. package/lib/svg/analyse/types.js +2 -0
  26. package/lib/svg/analyse/types.mjs +0 -0
  27. package/lib/svg/analyse.d.ts +8 -0
  28. package/lib/svg/analyse.js +352 -0
  29. package/lib/svg/analyse.mjs +302 -0
  30. package/lib/svg/cleanup/attribs.d.ts +1 -1
  31. package/lib/svg/cleanup/attribs.js +8 -0
  32. package/lib/svg/cleanup/attribs.mjs +8 -1
  33. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  34. package/lib/svg/cleanup/bad-tags.js +0 -2
  35. package/lib/svg/cleanup/bad-tags.mjs +0 -3
  36. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  37. package/lib/svg/cleanup/root-svg.d.ts +1 -1
  38. package/lib/svg/cleanup/root-svg.js +4 -2
  39. package/lib/svg/cleanup/root-svg.mjs +3 -3
  40. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  41. package/lib/svg/data/attributes.js +1 -1
  42. package/lib/svg/data/attributes.mjs +1 -1
  43. package/lib/svg/data/tags.d.ts +15 -7
  44. package/lib/svg/data/tags.js +20 -9
  45. package/lib/svg/data/tags.mjs +11 -6
  46. package/lib/svg/parse-style.d.ts +7 -7
  47. package/lib/svg/parse-style.js +27 -7
  48. package/lib/svg/parse-style.mjs +26 -7
  49. package/package.json +18 -2
@@ -2,11 +2,14 @@
2
2
  import {
3
3
  badAttributes,
4
4
  badAttributePrefixes,
5
- badSoftwareAttributes
5
+ badSoftwareAttributes,
6
+ tagSpecificPresentationalAttributes
6
7
  } from "../data/attributes.mjs";
8
+ import { defsTag } from "../data/tags.mjs";
7
9
  import { parseSVG } from "../parse.mjs";
8
10
  async function removeBadAttributes(svg) {
9
11
  await parseSVG(svg, (item) => {
12
+ const tagName = item.tagName;
10
13
  const attribs = item.element.attribs;
11
14
  const $element = item.$element;
12
15
  Object.keys(attribs).forEach((attr) => {
@@ -14,6 +17,10 @@ async function removeBadAttributes(svg) {
14
17
  $element.removeAttr(attr);
15
18
  return;
16
19
  }
20
+ if (defsTag.has(tagName) && !tagSpecificPresentationalAttributes[tagName].has(attr)) {
21
+ $element.removeAttr(attr);
22
+ return;
23
+ }
17
24
  const nsParts = attr.split(":");
18
25
  if (nsParts.length > 1) {
19
26
  const namespace = nsParts.shift();
@@ -1,4 +1,4 @@
1
- import type { SVG } from '..';
1
+ import type { SVG } from '../../svg';
2
2
  /**
3
3
  * Test for bag tags
4
4
  */
@@ -13,8 +13,6 @@ requiredParentTags.set(new Set(['feMerge']), tags_1.feMergeChildTags);
13
13
  requiredParentTags.set(tags_1.feLightningTags, tags_1.feLightningChildTags);
14
14
  // Filter tags must be children of <filter>
15
15
  requiredParentTags.set(tags_1.filterTag, tags_1.filterChildTags);
16
- // Tags that must be inside <defs>: gradients, <pattern>, <marker>
17
- requiredParentTags.set(tags_1.defsTag, tags_1.tagsInsideDefs);
18
16
  // <stop> must be inside gradient
19
17
  requiredParentTags.set(tags_1.gradientTags, tags_1.gradientChildTags);
20
18
  // <mpath> must be inside <animateMotion>
@@ -4,7 +4,6 @@ import {
4
4
  allValidTags,
5
5
  animateMotionChildTags,
6
6
  badTags,
7
- defsTag,
8
7
  feComponentTransferChildTag,
9
8
  feLightningChildTags,
10
9
  feLightningTags,
@@ -13,7 +12,6 @@ import {
13
12
  filterTag,
14
13
  gradientChildTags,
15
14
  gradientTags,
16
- tagsInsideDefs,
17
15
  unsupportedTags
18
16
  } from "../data/tags.mjs";
19
17
  var requiredParentTags = new Map();
@@ -21,7 +19,6 @@ requiredParentTags.set(new Set(["feComponentTransfer"]), feComponentTransferChil
21
19
  requiredParentTags.set(new Set(["feMerge"]), feMergeChildTags);
22
20
  requiredParentTags.set(feLightningTags, feLightningChildTags);
23
21
  requiredParentTags.set(filterTag, filterChildTags);
24
- requiredParentTags.set(defsTag, tagsInsideDefs);
25
22
  requiredParentTags.set(gradientTags, gradientChildTags);
26
23
  requiredParentTags.set(new Set(["animateMotion"]), animateMotionChildTags);
27
24
  async function checkBadTags(svg) {
@@ -1,4 +1,4 @@
1
- import type { SVG } from '..';
1
+ import type { SVG } from '../../svg';
2
2
  /**
3
3
  * Expand inline style
4
4
  */
@@ -1,4 +1,4 @@
1
- import type { SVG } from '..';
1
+ import type { SVG } from '../../svg';
2
2
  /**
3
3
  * Clean up SVG
4
4
  */
@@ -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) {
@@ -93,7 +93,9 @@ async function cleanupSVGRoot(svg) {
93
93
  return;
94
94
  }
95
95
  const tagName = child.tagName;
96
- if (tagName === 'style' || tags_1.maskAndSymbolTags.has(tagName)) {
96
+ if (tagName === 'style' ||
97
+ tags_1.reusableElementsWithPalette.has(tagName) ||
98
+ tags_1.maskTags.has(tagName)) {
97
99
  // Do not wrap these elements
98
100
  return;
99
101
  }
@@ -9,7 +9,7 @@ import {
9
9
  tagSpecificNonPresentationalAttributes,
10
10
  tagSpecificPresentationalAttributes
11
11
  } from "../data/attributes.mjs";
12
- import { maskAndSymbolTags } from "../data/tags.mjs";
12
+ import { maskTags, reusableElementsWithPalette } from "../data/tags.mjs";
13
13
  async function cleanupSVGRoot(svg) {
14
14
  const cheerio = svg.$svg;
15
15
  const $root = svg.$svg(":root");
@@ -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) {
@@ -75,7 +75,7 @@ async function cleanupSVGRoot(svg) {
75
75
  return;
76
76
  }
77
77
  const tagName2 = child.tagName;
78
- if (tagName2 === "style" || maskAndSymbolTags.has(tagName2)) {
78
+ if (tagName2 === "style" || reusableElementsWithPalette.has(tagName2) || maskTags.has(tagName2)) {
79
79
  return;
80
80
  }
81
81
  $child.appendTo($wrapper);
@@ -1,4 +1,4 @@
1
- import type { SVG } from '..';
1
+ import type { SVG } from '../../svg';
2
2
  /**
3
3
  * Expand inline style
4
4
  */
@@ -221,7 +221,7 @@ exports.tagSpecificPresentationalAttributes = {
221
221
  svg: new Set(['width', 'height', ...exports.presentationalAttributes]),
222
222
  // Defnitions, containers and masks
223
223
  clipPath: new Set([...exports.presentationalAttributes]),
224
- defs: new Set([...exports.presentationalAttributes]),
224
+ defs: new Set([]),
225
225
  g: new Set([...exports.presentationalAttributes]),
226
226
  mask: new Set(['x', 'y', 'width', 'height', ...exports.presentationalAttributes]),
227
227
  symbol: new Set(['x', 'y', 'width', 'height', ...exports.presentationalAttributes]),
@@ -156,7 +156,7 @@ var tagSpecificAnimatedAttributes = {
156
156
  var tagSpecificPresentationalAttributes = {
157
157
  svg: new Set(["width", "height", ...presentationalAttributes]),
158
158
  clipPath: new Set([...presentationalAttributes]),
159
- defs: new Set([...presentationalAttributes]),
159
+ defs: new Set([]),
160
160
  g: new Set([...presentationalAttributes]),
161
161
  mask: new Set(["x", "y", "width", "height", ...presentationalAttributes]),
162
162
  symbol: new Set(["x", "y", "width", "height", ...presentationalAttributes]),
@@ -29,9 +29,13 @@ export declare const styleTag: Set<string>;
29
29
  */
30
30
  export declare const defsTag: Set<string>;
31
31
  /**
32
- * Masks: colors are ignored, child elements must have id
32
+ * Masks: colors are ignored, elements must have id
33
33
  */
34
- export declare const maskAndSymbolTags: Set<string>;
34
+ export declare const maskTags: Set<string>;
35
+ /**
36
+ * Symbol
37
+ */
38
+ export declare const symbolTag: Set<string>;
35
39
  /**
36
40
  * SVG shapes
37
41
  */
@@ -45,7 +49,7 @@ export declare const useTag: Set<string>;
45
49
  */
46
50
  export declare const groupTag: Set<string>;
47
51
  /**
48
- * Marker, must be inside <defs>
52
+ * Marker, should be inside <defs>
49
53
  */
50
54
  export declare const markerTag: Set<string>;
51
55
  /**
@@ -54,7 +58,7 @@ export declare const markerTag: Set<string>;
54
58
  export declare const animateTags: Set<string>;
55
59
  export declare const animateMotionChildTags: Set<string>;
56
60
  /**
57
- * Gradients, must be inside <defs>
61
+ * Gradients, should be inside <defs>
58
62
  */
59
63
  export declare const gradientTags: Set<string>;
60
64
  /**
@@ -62,7 +66,7 @@ export declare const gradientTags: Set<string>;
62
66
  */
63
67
  export declare const gradientChildTags: Set<string>;
64
68
  /**
65
- * Pattern, must be inside <defs>
69
+ * Pattern, should be inside <defs>
66
70
  */
67
71
  export declare const patternTag: Set<string>;
68
72
  /**
@@ -76,9 +80,13 @@ export declare const feLightningChildTags: Set<string>;
76
80
  export declare const feMergeChildTags: Set<string>;
77
81
  /***** Combination of tags *****/
78
82
  /**
79
- * Tags that can be used only inside <defs>
83
+ * Reusable elements that use colors
84
+ *
85
+ * Most are used via color attributes like `fill`
86
+ * Some are used via custom attributes like `marker-start`
87
+ * Filter is used via `filter`
80
88
  */
81
- export declare const tagsInsideDefs: Set<string>;
89
+ export declare const reusableElementsWithPalette: Set<string>;
82
90
  /**
83
91
  * All supported tags
84
92
  */
@@ -4,7 +4,7 @@
4
4
  * Icons cannot have anything that requires external resources, anything that renders inconsistently.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.allValidTags = exports.tagsInsideDefs = exports.feMergeChildTags = exports.feLightningChildTags = exports.feComponentTransferChildTag = exports.filterChildTags = exports.feLightningTags = exports.filterTag = exports.patternTag = exports.gradientChildTags = exports.gradientTags = exports.animateMotionChildTags = exports.animateTags = exports.markerTag = exports.groupTag = exports.useTag = exports.shapeTags = exports.maskAndSymbolTags = exports.defsTag = exports.styleTag = exports.unsupportedTags = exports.badTags = void 0;
7
+ exports.allValidTags = exports.reusableElementsWithPalette = exports.feMergeChildTags = exports.feLightningChildTags = exports.feComponentTransferChildTag = exports.filterChildTags = exports.feLightningTags = exports.filterTag = exports.patternTag = exports.gradientChildTags = exports.gradientTags = exports.animateMotionChildTags = exports.animateTags = exports.markerTag = exports.groupTag = exports.useTag = exports.shapeTags = exports.symbolTag = exports.maskTags = exports.defsTag = exports.styleTag = exports.unsupportedTags = exports.badTags = void 0;
8
8
  /**
9
9
  * Bad tags
10
10
  *
@@ -57,9 +57,13 @@ exports.styleTag = new Set(['style']);
57
57
  */
58
58
  exports.defsTag = new Set(['defs']);
59
59
  /**
60
- * Masks: colors are ignored, child elements must have id
60
+ * Masks: colors are ignored, elements must have id
61
61
  */
62
- exports.maskAndSymbolTags = new Set(['clipPath', 'mask', 'symbol']);
62
+ exports.maskTags = new Set(['clipPath', 'mask']);
63
+ /**
64
+ * Symbol
65
+ */
66
+ exports.symbolTag = new Set(['symbol']);
63
67
  /**
64
68
  * SVG shapes
65
69
  */
@@ -81,7 +85,7 @@ exports.useTag = new Set(['use']);
81
85
  */
82
86
  exports.groupTag = new Set(['g']);
83
87
  /**
84
- * Marker, must be inside <defs>
88
+ * Marker, should be inside <defs>
85
89
  */
86
90
  exports.markerTag = new Set(['marker']);
87
91
  /**
@@ -96,7 +100,7 @@ exports.animateTags = new Set([
96
100
  ]);
97
101
  exports.animateMotionChildTags = new Set(['mpath']);
98
102
  /**
99
- * Gradients, must be inside <defs>
103
+ * Gradients, should be inside <defs>
100
104
  */
101
105
  exports.gradientTags = new Set(['linearGradient', 'radialGradient']);
102
106
  /**
@@ -104,7 +108,7 @@ exports.gradientTags = new Set(['linearGradient', 'radialGradient']);
104
108
  */
105
109
  exports.gradientChildTags = new Set(['stop']);
106
110
  /**
107
- * Pattern, must be inside <defs>
111
+ * Pattern, should be inside <defs>
108
112
  */
109
113
  exports.patternTag = new Set(['pattern']);
110
114
  /**
@@ -146,12 +150,18 @@ exports.feLightningChildTags = new Set([
146
150
  exports.feMergeChildTags = new Set(['feMergeNode']);
147
151
  /***** Combination of tags *****/
148
152
  /**
149
- * Tags that can be used only inside <defs>
153
+ * Reusable elements that use colors
154
+ *
155
+ * Most are used via color attributes like `fill`
156
+ * Some are used via custom attributes like `marker-start`
157
+ * Filter is used via `filter`
150
158
  */
151
- exports.tagsInsideDefs = new Set([
159
+ exports.reusableElementsWithPalette = new Set([
152
160
  ...exports.gradientTags,
153
161
  ...exports.patternTag,
154
162
  ...exports.markerTag,
163
+ ...exports.symbolTag,
164
+ ...exports.filterTag,
155
165
  ]);
156
166
  /**
157
167
  * All supported tags
@@ -159,7 +169,8 @@ exports.tagsInsideDefs = new Set([
159
169
  exports.allValidTags = new Set([
160
170
  ...exports.styleTag,
161
171
  ...exports.defsTag,
162
- ...exports.maskAndSymbolTags,
172
+ ...exports.maskTags,
173
+ ...exports.symbolTag,
163
174
  ...exports.shapeTags,
164
175
  ...exports.useTag,
165
176
  ...exports.groupTag,
@@ -22,7 +22,8 @@ var badTags = new Set([
22
22
  var unsupportedTags = new Set(["metadata", "desc", "title"]);
23
23
  var styleTag = new Set(["style"]);
24
24
  var defsTag = new Set(["defs"]);
25
- var maskAndSymbolTags = new Set(["clipPath", "mask", "symbol"]);
25
+ var maskTags = new Set(["clipPath", "mask"]);
26
+ var symbolTag = new Set(["symbol"]);
26
27
  var shapeTags = new Set([
27
28
  "circle",
28
29
  "ellipse",
@@ -80,15 +81,18 @@ var feLightningChildTags = new Set([
80
81
  "feDistantLight"
81
82
  ]);
82
83
  var feMergeChildTags = new Set(["feMergeNode"]);
83
- var tagsInsideDefs = new Set([
84
+ var reusableElementsWithPalette = new Set([
84
85
  ...gradientTags,
85
86
  ...patternTag,
86
- ...markerTag
87
+ ...markerTag,
88
+ ...symbolTag,
89
+ ...filterTag
87
90
  ]);
88
91
  var allValidTags = new Set([
89
92
  ...styleTag,
90
93
  ...defsTag,
91
- ...maskAndSymbolTags,
94
+ ...maskTags,
95
+ ...symbolTag,
92
96
  ...shapeTags,
93
97
  ...useTag,
94
98
  ...groupTag,
@@ -120,11 +124,12 @@ export {
120
124
  gradientTags,
121
125
  groupTag,
122
126
  markerTag,
123
- maskAndSymbolTags,
127
+ maskTags,
124
128
  patternTag,
129
+ reusableElementsWithPalette,
125
130
  shapeTags,
126
131
  styleTag,
127
- tagsInsideDefs,
132
+ symbolTag,
128
133
  unsupportedTags,
129
134
  useTag
130
135
  };
@@ -1,4 +1,5 @@
1
1
  import type { SVG } from '.';
2
+ import type { CSSRuleToken, CSSToken } from '../css/parser/types';
2
3
  import { ParseSVGCallbackItem } from './parse';
3
4
  /**
4
5
  * Item in callback
@@ -13,6 +14,11 @@ interface ParseSVGStyleCallbackItemInline extends ParseSVGStyleCallbackItemCommo
13
14
  }
14
15
  interface ParseSVGStyleCallbackItemGlobal extends ParseSVGStyleCallbackItemCommon {
15
16
  type: 'global';
17
+ token: CSSRuleToken;
18
+ selectors: string[];
19
+ selectorTokens: CSSToken[];
20
+ prevTokens: (CSSToken | null)[];
21
+ nextTokens: CSSToken[];
16
22
  }
17
23
  export declare type ParseSVGStyleCallbackItem = ParseSVGStyleCallbackItemInline | ParseSVGStyleCallbackItemGlobal;
18
24
  /**
@@ -23,12 +29,6 @@ export declare type ParseSVGStyleCallbackResult = string | undefined;
23
29
  * Callback function
24
30
  */
25
31
  export declare type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult | Promise<ParseSVGStyleCallbackResult>;
26
- /**
27
- * Options
28
- */
29
- interface ParseSVGStyleOptions {
30
- skipMasks?: boolean;
31
- }
32
32
  /**
33
33
  * Parse styles in SVG
34
34
  *
@@ -36,5 +36,5 @@ interface ParseSVGStyleOptions {
36
36
  * Callback should return new value (string) or undefined to remove rule.
37
37
  * Callback can be asynchronous.
38
38
  */
39
- export declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback, options?: ParseSVGStyleOptions): Promise<void>;
39
+ export declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): Promise<void>;
40
40
  export {};
@@ -5,7 +5,6 @@ const parse_1 = require("../css/parse");
5
5
  const export_1 = require("../css/parser/export");
6
6
  const tokens_1 = require("../css/parser/tokens");
7
7
  const tree_1 = require("../css/parser/tree");
8
- const tags_1 = require("./data/tags");
9
8
  const parse_2 = require("./parse");
10
9
  /**
11
10
  * Parse styles in SVG
@@ -14,7 +13,7 @@ const parse_2 = require("./parse");
14
13
  * Callback should return new value (string) or undefined to remove rule.
15
14
  * Callback can be asynchronous.
16
15
  */
17
- async function parseSVGStyle(svg, callback, options = {}) {
16
+ async function parseSVGStyle(svg, callback) {
18
17
  return (0, parse_2.parseSVG)(svg, async (item) => {
19
18
  const tagName = item.tagName;
20
19
  const $element = item.$element;
@@ -32,18 +31,43 @@ async function parseSVGStyle(svg, callback, options = {}) {
32
31
  }
33
32
  // Parse all tokens
34
33
  let changed = false;
34
+ const selectorStart = [];
35
35
  const newTokens = [];
36
36
  for (let i = 0; i < tokens.length; i++) {
37
37
  const token = tokens[i];
38
+ switch (token.type) {
39
+ case 'selector':
40
+ case 'at-rule':
41
+ selectorStart.push(newTokens.length);
42
+ break;
43
+ case 'close':
44
+ selectorStart.pop();
45
+ break;
46
+ }
38
47
  if (token.type !== 'rule') {
39
48
  newTokens.push(token);
40
49
  continue;
41
50
  }
42
51
  const value = token.value;
52
+ const selectorTokens = selectorStart
53
+ .map((index) => newTokens[index])
54
+ .filter((item) => item !== null);
43
55
  let result = callback({
44
56
  type: 'global',
45
57
  prop: token.prop,
46
58
  value,
59
+ token,
60
+ selectorTokens,
61
+ selectors: selectorTokens.reduce((prev, current) => {
62
+ switch (current.type) {
63
+ case 'selector': {
64
+ return prev.concat(current.selectors);
65
+ }
66
+ }
67
+ return prev;
68
+ }, []),
69
+ prevTokens: newTokens,
70
+ nextTokens: tokens.slice(i + 1),
47
71
  });
48
72
  if (result instanceof Promise) {
49
73
  result = await result;
@@ -64,7 +88,7 @@ async function parseSVGStyle(svg, callback, options = {}) {
64
88
  return;
65
89
  }
66
90
  // Update style
67
- const tree = (0, tree_1.tokensTree)(newTokens);
91
+ const tree = (0, tree_1.tokensTree)(newTokens.filter((token) => token !== null));
68
92
  if (!tree.length) {
69
93
  // Empty
70
94
  $element.remove();
@@ -74,10 +98,6 @@ async function parseSVGStyle(svg, callback, options = {}) {
74
98
  item.$element.text(newContent);
75
99
  return;
76
100
  }
77
- // Skip masks
78
- if (options.skipMasks && tags_1.maskAndSymbolTags.has(tagName)) {
79
- return;
80
- }
81
101
  // Parse style
82
102
  const attribs = item.element.attribs;
83
103
  if (attribs.style === void 0) {
@@ -3,9 +3,8 @@ import { parseInlineStyle } from "../css/parse.mjs";
3
3
  import { tokensToString } from "../css/parser/export.mjs";
4
4
  import { getTokens } from "../css/parser/tokens.mjs";
5
5
  import { tokensTree } from "../css/parser/tree.mjs";
6
- import { maskAndSymbolTags } from "./data/tags.mjs";
7
6
  import { parseSVG } from "./parse.mjs";
8
- async function parseSVGStyle(svg, callback, options = {}) {
7
+ async function parseSVGStyle(svg, callback) {
9
8
  return parseSVG(svg, async (item) => {
10
9
  const tagName = item.tagName;
11
10
  const $element = item.$element;
@@ -20,18 +19,41 @@ async function parseSVGStyle(svg, callback, options = {}) {
20
19
  throw new Error("Error parsing style");
21
20
  }
22
21
  let changed2 = false;
22
+ const selectorStart = [];
23
23
  const newTokens = [];
24
24
  for (let i = 0; i < tokens.length; i++) {
25
25
  const token = tokens[i];
26
+ switch (token.type) {
27
+ case "selector":
28
+ case "at-rule":
29
+ selectorStart.push(newTokens.length);
30
+ break;
31
+ case "close":
32
+ selectorStart.pop();
33
+ break;
34
+ }
26
35
  if (token.type !== "rule") {
27
36
  newTokens.push(token);
28
37
  continue;
29
38
  }
30
39
  const value = token.value;
40
+ const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
31
41
  let result = callback({
32
42
  type: "global",
33
43
  prop: token.prop,
34
- value
44
+ value,
45
+ token,
46
+ selectorTokens,
47
+ selectors: selectorTokens.reduce((prev, current) => {
48
+ switch (current.type) {
49
+ case "selector": {
50
+ return prev.concat(current.selectors);
51
+ }
52
+ }
53
+ return prev;
54
+ }, []),
55
+ prevTokens: newTokens,
56
+ nextTokens: tokens.slice(i + 1)
35
57
  });
36
58
  if (result instanceof Promise) {
37
59
  result = await result;
@@ -49,7 +71,7 @@ async function parseSVGStyle(svg, callback, options = {}) {
49
71
  if (!changed2) {
50
72
  return;
51
73
  }
52
- const tree = tokensTree(newTokens);
74
+ const tree = tokensTree(newTokens.filter((token) => token !== null));
53
75
  if (!tree.length) {
54
76
  $element.remove();
55
77
  return;
@@ -58,9 +80,6 @@ async function parseSVGStyle(svg, callback, options = {}) {
58
80
  item.$element.text(newContent);
59
81
  return;
60
82
  }
61
- if (options.skipMasks && maskAndSymbolTags.has(tagName)) {
62
- return;
63
- }
64
83
  const attribs = item.element.attribs;
65
84
  if (attribs.style === void 0) {
66
85
  return;
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.8",
5
+ "version": "2.0.12",
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.23",
26
26
  "@types/cheerio": "^0.22.30",
27
27
  "@types/node-fetch": "^2.5.12",
28
28
  "@types/svgo": "^2.6.0",
@@ -312,6 +312,10 @@
312
312
  "require": "./lib/optimise/flags.js",
313
313
  "import": "./lib/optimise/flags.mjs"
314
314
  },
315
+ "./lib/optimise/global-style": {
316
+ "require": "./lib/optimise/global-style.js",
317
+ "import": "./lib/optimise/global-style.mjs"
318
+ },
315
319
  "./lib/optimise/scale": {
316
320
  "require": "./lib/optimise/scale.js",
317
321
  "import": "./lib/optimise/scale.mjs"
@@ -320,6 +324,18 @@
320
324
  "require": "./lib/optimise/svgo.js",
321
325
  "import": "./lib/optimise/svgo.mjs"
322
326
  },
327
+ "./lib/svg/analyse": {
328
+ "require": "./lib/svg/analyse.js",
329
+ "import": "./lib/svg/analyse.mjs"
330
+ },
331
+ "./lib/svg/analyse/error": {
332
+ "require": "./lib/svg/analyse/error.js",
333
+ "import": "./lib/svg/analyse/error.mjs"
334
+ },
335
+ "./lib/svg/analyse/types": {
336
+ "require": "./lib/svg/analyse/types.js",
337
+ "import": "./lib/svg/analyse/types.mjs"
338
+ },
323
339
  "./lib/svg/cleanup": {
324
340
  "require": "./lib/svg/cleanup.js",
325
341
  "import": "./lib/svg/cleanup.mjs"