@iconify/tools 2.2.6 → 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.
- package/README.md +8 -2
- package/lib/colors/detect.cjs +4 -4
- package/lib/colors/detect.d.ts +1 -1
- package/lib/colors/detect.mjs +5 -5
- package/lib/colors/parse.cjs +275 -129
- package/lib/colors/parse.d.ts +14 -5
- package/lib/colors/parse.mjs +278 -133
- package/lib/colors/validate.cjs +11 -2
- package/lib/colors/validate.d.ts +10 -2
- package/lib/colors/validate.mjs +12 -4
- package/lib/download/api/download.cjs +4 -4
- package/lib/download/api/download.mjs +4 -4
- package/lib/download/api/index.cjs +0 -1
- package/lib/download/api/index.mjs +0 -1
- package/lib/download/git/index.cjs +1 -1
- package/lib/download/git/index.mjs +1 -1
- package/lib/download/git/reset.cjs +1 -1
- package/lib/download/git/reset.mjs +1 -1
- package/lib/download/github/hash.cjs +0 -1
- package/lib/download/github/hash.mjs +0 -1
- package/lib/download/github/index.cjs +1 -1
- package/lib/download/github/index.mjs +1 -1
- package/lib/download/gitlab/hash.cjs +0 -1
- package/lib/download/gitlab/hash.mjs +0 -1
- package/lib/download/gitlab/index.cjs +1 -1
- package/lib/download/gitlab/index.mjs +1 -1
- package/lib/download/helpers/unzip.cjs +5 -1
- package/lib/download/index.cjs +1 -1
- package/lib/download/index.mjs +1 -1
- package/lib/download/npm/index.cjs +1 -1
- package/lib/download/npm/index.mjs +1 -1
- package/lib/icon-set/index.cjs +18 -0
- package/lib/icon-set/index.d.ts +7 -1
- package/lib/icon-set/index.mjs +18 -0
- package/lib/icon-set/tags.cjs +2 -2
- package/lib/icon-set/tags.d.ts +1 -1
- package/lib/icon-set/tags.mjs +2 -2
- package/lib/icon-set/types.d.ts +5 -4
- package/lib/import/directory.cjs +101 -28
- package/lib/import/directory.d.ts +12 -5
- package/lib/import/directory.mjs +103 -31
- package/lib/import/figma/index.cjs +1 -2
- package/lib/import/figma/index.mjs +1 -2
- package/lib/import/figma/query.cjs +0 -1
- package/lib/import/figma/query.mjs +0 -1
- package/lib/index.cjs +7 -1
- package/lib/index.d.ts +6 -6
- package/lib/index.mjs +7 -7
- package/lib/misc/scan.cjs +61 -8
- package/lib/misc/scan.d.ts +12 -6
- package/lib/misc/scan.mjs +62 -10
- package/lib/svg/analyse.cjs +2 -2
- package/lib/svg/analyse.d.ts +1 -1
- package/lib/svg/analyse.mjs +4 -4
- package/lib/svg/cleanup/attribs.cjs +2 -2
- package/lib/svg/cleanup/attribs.d.ts +1 -1
- package/lib/svg/cleanup/attribs.mjs +3 -3
- package/lib/svg/cleanup/bad-tags.cjs +2 -2
- package/lib/svg/cleanup/bad-tags.d.ts +1 -1
- package/lib/svg/cleanup/bad-tags.mjs +3 -3
- package/lib/svg/cleanup/inline-style.cjs +2 -2
- package/lib/svg/cleanup/inline-style.d.ts +1 -1
- package/lib/svg/cleanup/inline-style.mjs +3 -3
- package/lib/svg/cleanup/root-style.cjs +1 -1
- package/lib/svg/cleanup/root-style.d.ts +1 -5
- package/lib/svg/cleanup/root-style.mjs +2 -2
- package/lib/svg/cleanup/svgo-style.cjs +2 -2
- package/lib/svg/cleanup/svgo-style.d.ts +1 -1
- package/lib/svg/cleanup/svgo-style.mjs +3 -3
- package/lib/svg/cleanup.cjs +6 -6
- package/lib/svg/cleanup.d.ts +1 -1
- package/lib/svg/cleanup.mjs +6 -6
- package/lib/svg/index.cjs +5 -1
- package/lib/svg/parse-style.cjs +149 -89
- package/lib/svg/parse-style.d.ts +6 -1
- package/lib/svg/parse-style.mjs +150 -91
- package/lib/svg/parse.cjs +56 -18
- package/lib/svg/parse.d.ts +8 -2
- package/lib/svg/parse.mjs +56 -19
- package/package.json +15 -17
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
|
-
|
|
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
|
-
|
|
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.
|
package/lib/colors/detect.cjs
CHANGED
|
@@ -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
|
-
|
|
20
|
+
function detectIconSetPalette(iconSet) {
|
|
21
21
|
let palette;
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
32
|
+
colors_parse.parseColorsSync(svg, {
|
|
33
33
|
callback: (attr, colorStr, color) => {
|
|
34
34
|
if (!color) {
|
|
35
35
|
iconPalette = null;
|
package/lib/colors/detect.d.ts
CHANGED
|
@@ -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):
|
|
13
|
+
declare function detectIconSetPalette(iconSet: IconSet): boolean | null;
|
|
14
14
|
|
|
15
15
|
export { detectIconSetPalette };
|
package/lib/colors/detect.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
18
|
+
function detectIconSetPalette(iconSet) {
|
|
19
19
|
let palette;
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
30
|
+
parseColorsSync(svg, {
|
|
31
31
|
callback: (attr, colorStr, color) => {
|
|
32
32
|
if (!color) {
|
|
33
33
|
iconPalette = null;
|
package/lib/colors/parse.cjs
CHANGED
|
@@ -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
|
-
|
|
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,
|
|
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 =
|
|
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
|
-
|
|
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 (!
|
|
94
|
+
if (!callback) {
|
|
95
95
|
addColorToItem(prop, defaultValue, item);
|
|
96
|
-
return value;
|
|
96
|
+
return done(value);
|
|
97
97
|
}
|
|
98
|
-
|
|
99
|
-
prop,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
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
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
if (
|
|
198
|
-
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const
|
|
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
|
-
|
|
268
|
-
) : defaultColor;
|
|
269
|
-
findColor(defaultColorValue, true);
|
|
270
|
-
cheerio(element).attr(
|
|
271
|
-
prop,
|
|
272
|
-
colors.colorToString(defaultColorValue)
|
|
322
|
+
elements
|
|
273
323
|
);
|
|
274
|
-
|
|
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
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
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;
|
package/lib/colors/parse.d.ts
CHANGED
|
@@ -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,
|
|
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) =>
|
|
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
|
|
35
|
-
callback?:
|
|
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 };
|