@iconify/tools 3.0.5 → 4.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 (79) hide show
  1. package/lib/colors/detect.cjs +1 -1
  2. package/lib/colors/detect.d.ts +1 -0
  3. package/lib/colors/detect.mjs +2 -2
  4. package/lib/colors/parse.cjs +131 -275
  5. package/lib/colors/parse.d.ts +8 -15
  6. package/lib/colors/parse.mjs +135 -278
  7. package/lib/colors/validate.cjs +2 -11
  8. package/lib/colors/validate.d.ts +5 -11
  9. package/lib/colors/validate.mjs +4 -12
  10. package/lib/export/directory.d.ts +1 -0
  11. package/lib/export/icon-package.d.ts +4 -1
  12. package/lib/export/json-package.cjs +1 -0
  13. package/lib/export/json-package.d.ts +2 -0
  14. package/lib/export/json-package.mjs +1 -0
  15. package/lib/icon-set/index.cjs +1 -1
  16. package/lib/icon-set/index.d.ts +2 -1
  17. package/lib/icon-set/index.mjs +1 -1
  18. package/lib/icon-set/match.d.ts +1 -0
  19. package/lib/icon-set/merge.d.ts +1 -0
  20. package/lib/icon-set/modified.d.ts +1 -0
  21. package/lib/icon-set/tags.d.ts +1 -0
  22. package/lib/import/directory.d.ts +1 -0
  23. package/lib/import/figma/index.d.ts +1 -0
  24. package/lib/import/figma/nodes.d.ts +1 -0
  25. package/lib/import/figma/query.d.ts +1 -0
  26. package/lib/import/figma/types/nodes.d.ts +1 -0
  27. package/lib/import/figma/types/options.d.ts +1 -0
  28. package/lib/import/figma/types/result.d.ts +1 -0
  29. package/lib/index.cjs +4 -6
  30. package/lib/index.d.ts +7 -5
  31. package/lib/index.mjs +6 -5
  32. package/lib/misc/cheerio.d.ts +4 -2
  33. package/lib/optimise/figma.cjs +175 -0
  34. package/lib/optimise/figma.d.ts +11 -0
  35. package/lib/optimise/figma.mjs +173 -0
  36. package/lib/optimise/flags.cjs +2 -2
  37. package/lib/optimise/flags.d.ts +2 -1
  38. package/lib/optimise/flags.mjs +2 -2
  39. package/lib/optimise/global-style.cjs +6 -6
  40. package/lib/optimise/global-style.d.ts +2 -1
  41. package/lib/optimise/global-style.mjs +6 -6
  42. package/lib/optimise/mask.cjs +110 -0
  43. package/lib/optimise/mask.d.ts +24 -0
  44. package/lib/optimise/mask.mjs +108 -0
  45. package/lib/optimise/origin.d.ts +1 -0
  46. package/lib/optimise/scale.d.ts +1 -0
  47. package/lib/optimise/svgo.d.ts +1 -0
  48. package/lib/svg/analyse/error.d.ts +2 -0
  49. package/lib/svg/analyse/types.d.ts +4 -1
  50. package/lib/svg/analyse.cjs +1 -1
  51. package/lib/svg/analyse.d.ts +2 -0
  52. package/lib/svg/analyse.mjs +3 -3
  53. package/lib/svg/cleanup/attribs.cjs +1 -1
  54. package/lib/svg/cleanup/attribs.d.ts +1 -0
  55. package/lib/svg/cleanup/attribs.mjs +2 -2
  56. package/lib/svg/cleanup/bad-tags.cjs +1 -1
  57. package/lib/svg/cleanup/bad-tags.d.ts +1 -0
  58. package/lib/svg/cleanup/bad-tags.mjs +2 -2
  59. package/lib/svg/cleanup/inline-style.cjs +1 -1
  60. package/lib/svg/cleanup/inline-style.d.ts +1 -0
  61. package/lib/svg/cleanup/inline-style.mjs +2 -2
  62. package/lib/svg/cleanup/root-style.cjs +1 -1
  63. package/lib/svg/cleanup/root-style.d.ts +1 -0
  64. package/lib/svg/cleanup/root-style.mjs +2 -2
  65. package/lib/svg/cleanup/root-svg.d.ts +1 -0
  66. package/lib/svg/cleanup/svgo-style.cjs +1 -1
  67. package/lib/svg/cleanup/svgo-style.d.ts +1 -0
  68. package/lib/svg/cleanup/svgo-style.mjs +2 -2
  69. package/lib/svg/cleanup.d.ts +1 -0
  70. package/lib/svg/index.cjs +13 -3
  71. package/lib/svg/index.d.ts +2 -1
  72. package/lib/svg/index.mjs +1 -1
  73. package/lib/svg/parse-style.cjs +178 -235
  74. package/lib/svg/parse-style.d.ts +4 -9
  75. package/lib/svg/parse-style.mjs +180 -236
  76. package/lib/svg/parse.cjs +26 -58
  77. package/lib/svg/parse.d.ts +4 -10
  78. package/lib/svg/parse.mjs +27 -58
  79. package/package.json +25 -13
@@ -1,3 +1,4 @@
1
+ import * as cheerio from 'cheerio';
1
2
  import { IconifyIcon } from '@iconify/types';
2
3
  export { IconifyIcon } from '@iconify/types';
3
4
  import { IconifyIconCustomisations } from '@iconify/utils/lib/customisations/defaults';
@@ -14,7 +15,7 @@ interface ViewBox {
14
15
  * SVG class, used to manipulate icon content.
15
16
  */
16
17
  declare class SVG {
17
- $svg: cheerio.Root;
18
+ $svg: cheerio.CheerioAPI;
18
19
  viewBox: ViewBox;
19
20
  /**
20
21
  * Constructor
package/lib/svg/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import cheerio from 'cheerio';
1
+ import * as cheerio from 'cheerio';
2
2
  import { iconToSVG, trimSVG } from '@iconify/utils';
3
3
 
4
4
  class SVG {
@@ -9,156 +9,147 @@ require('../css/parser/error.cjs');
9
9
  require('../css/parser/strings.cjs');
10
10
  require('../css/parser/text.cjs');
11
11
 
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
- }
12
+ function assertNotOldCode(value) {
13
+ if (value instanceof Promise) {
14
+ throw new Error("parseSVGStyle does not support async callbacks");
15
+ }
16
+ }
17
+ function parseSVGStyle(svg, callback) {
18
+ svg_parse.parseSVG(svg, (item) => {
19
+ const tagName = item.tagName;
20
+ const $element = item.$element;
21
+ function parseStyleItem() {
22
+ const content = $element.text();
23
+ if (typeof content !== "string") {
24
+ $element.remove();
25
+ return;
39
26
  }
40
- done2();
41
- };
42
- const nextToken = () => {
43
- const token = tokens.shift();
44
- if (token === void 0) {
45
- return parsedTokens();
27
+ const tokens = css_parser_tokens.getTokens(content);
28
+ if (!(tokens instanceof Array)) {
29
+ throw new Error("Error parsing style");
46
30
  }
47
- switch (token.type) {
48
- case "selector":
49
- selectorStart.push(newTokens.length);
50
- newTokens.push(token);
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;
31
+ let changed2 = false;
32
+ const selectorStart = [];
33
+ let newTokens = [];
34
+ while (tokens.length) {
35
+ const token = tokens.shift();
36
+ if (!token) {
37
+ break;
38
+ }
39
+ switch (token.type) {
40
+ case "selector":
41
+ selectorStart.push(newTokens.length);
42
+ newTokens.push(token);
43
+ break;
44
+ case "close":
45
+ selectorStart.pop();
46
+ newTokens.push(token);
47
+ break;
48
+ case "at-rule": {
49
+ selectorStart.push(newTokens.length);
50
+ const prop = token.rule;
51
+ const value = token.value;
52
+ const isAnimation = prop === "keyframes" || prop.slice(0, 1) === "-" && prop.split("-").pop() === "keyframes";
53
+ const childTokens = [];
54
+ const animationRules = /* @__PURE__ */ Object.create(null);
55
+ let depth = 1;
56
+ let index = 0;
57
+ let isFrom = false;
58
+ while (depth > 0) {
59
+ const childToken = tokens[index];
60
+ index++;
61
+ if (!childToken) {
62
+ throw new Error(
63
+ "Something went wrong parsing CSS"
64
+ );
78
65
  }
79
- case "selector": {
80
- depth++;
81
- if (isAnimation) {
82
- const rule = childToken.code;
83
- if (rule === "from" || rule === "0%") {
84
- isFrom = true;
66
+ childTokens.push(childToken);
67
+ switch (childToken.type) {
68
+ case "close": {
69
+ depth--;
70
+ isFrom = false;
71
+ break;
72
+ }
73
+ case "selector": {
74
+ depth++;
75
+ if (isAnimation) {
76
+ const rule = childToken.code;
77
+ if (rule === "from" || rule === "0%") {
78
+ isFrom = true;
79
+ }
85
80
  }
81
+ break;
86
82
  }
87
- break;
88
- }
89
- case "at-rule": {
90
- depth++;
91
- if (isAnimation) {
92
- throw new Error(
93
- "Nested at-rule in keyframes ???"
94
- );
83
+ case "at-rule": {
84
+ depth++;
85
+ if (isAnimation) {
86
+ throw new Error(
87
+ "Nested at-rule in keyframes ???"
88
+ );
89
+ }
90
+ break;
95
91
  }
96
- break;
97
- }
98
- case "rule": {
99
- if (isAnimation && isFrom) {
100
- animationRules[childToken.prop] = childToken.value;
92
+ case "rule": {
93
+ if (isAnimation && isFrom) {
94
+ animationRules[childToken.prop] = childToken.value;
95
+ }
96
+ break;
101
97
  }
102
- break;
103
98
  }
104
99
  }
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)
125
- },
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);
100
+ const skipCount = childTokens.length;
101
+ const result = callback(
102
+ isAnimation ? {
103
+ type: "keyframes",
104
+ prop,
105
+ value,
106
+ token,
107
+ childTokens,
108
+ from: animationRules,
109
+ prevTokens: newTokens,
110
+ nextTokens: tokens.slice(0)
111
+ } : {
112
+ type: "at-rule",
113
+ prop,
114
+ value,
115
+ token,
116
+ childTokens,
117
+ prevTokens: newTokens,
118
+ nextTokens: tokens.slice(0)
119
+ }
120
+ );
121
+ if (result !== void 0) {
122
+ assertNotOldCode(result);
123
+ if (isAnimation) {
124
+ if (result !== value) {
125
+ changed2 = true;
126
+ token.value = result;
145
127
  }
146
- } else {
147
- changed2 = true;
128
+ newTokens.push(token);
148
129
  for (let i = 0; i < skipCount; i++) {
149
130
  tokens.shift();
150
131
  }
132
+ newTokens = newTokens.concat(childTokens);
133
+ } else {
134
+ if (result !== value) {
135
+ throw new Error(
136
+ "Changing value for at-rule is not supported"
137
+ );
138
+ }
139
+ newTokens.push(token);
140
+ }
141
+ } else {
142
+ changed2 = true;
143
+ for (let i = 0; i < skipCount; i++) {
144
+ tokens.shift();
151
145
  }
152
- nextToken();
153
146
  }
154
- );
155
- return;
156
- }
157
- case "rule": {
158
- const value = token.value;
159
- const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
160
- callback(
161
- {
147
+ break;
148
+ }
149
+ case "rule": {
150
+ const value = token.value;
151
+ const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
152
+ const result = callback({
162
153
  type: "global",
163
154
  prop: token.prop,
164
155
  value,
@@ -179,122 +170,74 @@ function parseItem(item, callback, done) {
179
170
  ),
180
171
  prevTokens: newTokens,
181
172
  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 {
173
+ });
174
+ if (result !== void 0) {
175
+ assertNotOldCode(result);
176
+ if (result !== value) {
191
177
  changed2 = true;
178
+ token.value = result;
192
179
  }
193
- nextToken();
180
+ newTokens.push(token);
181
+ } else {
182
+ changed2 = true;
194
183
  }
195
- );
196
- return;
184
+ break;
185
+ }
197
186
  }
198
187
  }
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);
188
+ if (changed2) {
189
+ const tree = css_parser_tree.tokensTree(
190
+ newTokens.filter((token) => token !== null)
191
+ );
192
+ if (!tree.length) {
193
+ $element.remove();
194
+ } else {
195
+ const newContent = css_parser_export.tokensToString(tree);
196
+ item.$element.text("\n" + newContent);
197
+ }
223
198
  }
224
199
  }
225
- done();
226
- };
227
- const nextProp = () => {
228
- const prop = propsQueue.shift();
229
- if (prop === void 0) {
230
- return parsedProps();
200
+ if (tagName === "style") {
201
+ parseStyleItem();
202
+ return;
203
+ }
204
+ const attribs = item.element.attribs;
205
+ if (attribs.style === void 0) {
206
+ return;
207
+ }
208
+ const parsedStyle = css_parse.parseInlineStyle(attribs.style);
209
+ if (parsedStyle === null) {
210
+ $element.removeAttr("style");
211
+ return;
231
212
  }
232
- const value = parsedStyle[prop];
233
- callback(
234
- {
213
+ let changed = false;
214
+ for (const prop in parsedStyle) {
215
+ const value = parsedStyle[prop];
216
+ const result = callback({
235
217
  type: "inline",
236
218
  prop,
237
219
  value,
238
220
  item
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
- }
221
+ });
222
+ assertNotOldCode(result);
223
+ if (result !== value) {
224
+ changed = true;
225
+ if (result === void 0) {
226
+ delete parsedStyle[prop];
227
+ } else {
228
+ parsedStyle[prop] = result;
248
229
  }
249
- nextProp();
250
- }
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
230
  }
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");
292
- }
231
+ }
232
+ if (changed) {
233
+ const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
234
+ if (!newStyle.length) {
235
+ $element.removeAttr("style");
236
+ } else {
237
+ $element.attr("style", newStyle);
293
238
  }
294
- );
239
+ }
295
240
  });
296
- isSync = false;
297
241
  }
298
242
 
299
243
  exports.parseSVGStyle = parseSVGStyle;
300
- exports.parseSVGStyleSync = parseSVGStyleSync;
@@ -1,6 +1,7 @@
1
1
  import { SVG } from './index.js';
2
2
  import { CSSRuleToken, CSSToken, CSSAtRuleToken } from '../css/parser/types.js';
3
3
  import { ParseSVGCallbackItem } from './parse.js';
4
+ import 'cheerio';
4
5
  import '@iconify/types';
5
6
  import '@iconify/utils/lib/customisations/defaults';
6
7
  import '../misc/cheerio.js';
@@ -45,19 +46,13 @@ type ParseSVGStyleCallbackResult = string | undefined;
45
46
  /**
46
47
  * Callback function
47
48
  */
48
- type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult | Promise<ParseSVGStyleCallbackResult>;
49
- type ParseSVGStyleCallbackSync = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult;
49
+ type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult;
50
50
  /**
51
51
  * Parse styles in SVG
52
52
  *
53
53
  * This function finds CSS in SVG, parses it, calls callback for each rule.
54
54
  * Callback should return new value (string) or undefined to remove rule.
55
- * Callback can be asynchronous.
56
55
  */
57
- declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): Promise<void>;
58
- /**
59
- * Synchronous version
60
- */
61
- declare function parseSVGStyleSync(svg: SVG, callback: ParseSVGStyleCallbackSync): void;
56
+ declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): void;
62
57
 
63
- export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, ParseSVGStyleCallbackSync, parseSVGStyle, parseSVGStyleSync };
58
+ export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, parseSVGStyle };