@iconify/tools 2.0.9 → 2.0.13

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 (59) 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/download/git/index.js +12 -0
  7. package/lib/download/git/index.mjs +7 -0
  8. package/lib/download/git/reset.d.ts +4 -0
  9. package/lib/download/git/reset.js +16 -0
  10. package/lib/download/git/reset.mjs +13 -0
  11. package/lib/export/json-package.js +6 -1
  12. package/lib/export/json-package.mjs +4 -1
  13. package/lib/icon-set/index.js +11 -25
  14. package/lib/icon-set/index.mjs +13 -25
  15. package/lib/icon-set/merge.js +0 -1
  16. package/lib/icon-set/merge.mjs +1 -2
  17. package/lib/icon-set/props.d.ts +1 -1
  18. package/lib/icon-set/props.js +3 -2
  19. package/lib/icon-set/props.mjs +2 -2
  20. package/lib/icon-set/types.d.ts +3 -1
  21. package/lib/import/figma/nodes.js +3 -5
  22. package/lib/import/figma/nodes.mjs +3 -5
  23. package/lib/index.d.ts +3 -0
  24. package/lib/index.js +7 -1
  25. package/lib/index.mjs +6 -0
  26. package/lib/optimise/flags.js +9 -0
  27. package/lib/optimise/flags.mjs +8 -0
  28. package/lib/optimise/global-style.d.ts +5 -0
  29. package/lib/optimise/global-style.js +158 -0
  30. package/lib/optimise/global-style.mjs +129 -0
  31. package/lib/svg/analyse/error.d.ts +5 -0
  32. package/lib/svg/analyse/error.js +22 -0
  33. package/lib/svg/analyse/error.mjs +16 -0
  34. package/lib/svg/analyse/types.d.ts +89 -0
  35. package/lib/svg/analyse/types.js +2 -0
  36. package/lib/svg/analyse/types.mjs +0 -0
  37. package/lib/svg/analyse.d.ts +8 -0
  38. package/lib/svg/analyse.js +352 -0
  39. package/lib/svg/analyse.mjs +302 -0
  40. package/lib/svg/cleanup/attribs.d.ts +1 -1
  41. package/lib/svg/cleanup/attribs.js +8 -0
  42. package/lib/svg/cleanup/attribs.mjs +8 -1
  43. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  44. package/lib/svg/cleanup/bad-tags.js +0 -2
  45. package/lib/svg/cleanup/bad-tags.mjs +0 -3
  46. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  47. package/lib/svg/cleanup/root-svg.d.ts +1 -1
  48. package/lib/svg/cleanup/root-svg.js +4 -2
  49. package/lib/svg/cleanup/root-svg.mjs +3 -3
  50. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  51. package/lib/svg/data/attributes.js +1 -1
  52. package/lib/svg/data/attributes.mjs +1 -1
  53. package/lib/svg/data/tags.d.ts +15 -7
  54. package/lib/svg/data/tags.js +20 -9
  55. package/lib/svg/data/tags.mjs +11 -6
  56. package/lib/svg/parse-style.d.ts +7 -7
  57. package/lib/svg/parse-style.js +27 -7
  58. package/lib/svg/parse-style.mjs +26 -7
  59. package/package.json +22 -2
@@ -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.9",
5
+ "version": "2.0.13",
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",
@@ -120,6 +120,10 @@
120
120
  "require": "./lib/download/git/index.js",
121
121
  "import": "./lib/download/git/index.mjs"
122
122
  },
123
+ "./lib/download/git/reset": {
124
+ "require": "./lib/download/git/reset.js",
125
+ "import": "./lib/download/git/reset.mjs"
126
+ },
123
127
  "./lib/download/github/hash": {
124
128
  "require": "./lib/download/github/hash.js",
125
129
  "import": "./lib/download/github/hash.mjs"
@@ -312,6 +316,10 @@
312
316
  "require": "./lib/optimise/flags.js",
313
317
  "import": "./lib/optimise/flags.mjs"
314
318
  },
319
+ "./lib/optimise/global-style": {
320
+ "require": "./lib/optimise/global-style.js",
321
+ "import": "./lib/optimise/global-style.mjs"
322
+ },
315
323
  "./lib/optimise/scale": {
316
324
  "require": "./lib/optimise/scale.js",
317
325
  "import": "./lib/optimise/scale.mjs"
@@ -320,6 +328,18 @@
320
328
  "require": "./lib/optimise/svgo.js",
321
329
  "import": "./lib/optimise/svgo.mjs"
322
330
  },
331
+ "./lib/svg/analyse": {
332
+ "require": "./lib/svg/analyse.js",
333
+ "import": "./lib/svg/analyse.mjs"
334
+ },
335
+ "./lib/svg/analyse/error": {
336
+ "require": "./lib/svg/analyse/error.js",
337
+ "import": "./lib/svg/analyse/error.mjs"
338
+ },
339
+ "./lib/svg/analyse/types": {
340
+ "require": "./lib/svg/analyse/types.js",
341
+ "import": "./lib/svg/analyse/types.mjs"
342
+ },
323
343
  "./lib/svg/cleanup": {
324
344
  "require": "./lib/svg/cleanup.js",
325
345
  "import": "./lib/svg/cleanup.mjs"