@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
|
@@ -13,8 +13,8 @@ requiredParentTags.set(svg_data_tags.feLightningTags, svg_data_tags.feLightningC
|
|
|
13
13
|
requiredParentTags.set(svg_data_tags.filterTag, svg_data_tags.filterChildTags);
|
|
14
14
|
requiredParentTags.set(svg_data_tags.gradientTags, svg_data_tags.gradientChildTags);
|
|
15
15
|
requiredParentTags.set(/* @__PURE__ */ new Set(["animateMotion"]), svg_data_tags.animateMotionChildTags);
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
function checkBadTags(svg) {
|
|
17
|
+
svg_parse.parseSVGSync(svg, (item) => {
|
|
18
18
|
const tagName = item.tagName;
|
|
19
19
|
const $element = item.$element;
|
|
20
20
|
if (tagName === "svg") {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseSVGSync } from '../parse.mjs';
|
|
2
2
|
import { feComponentTransferChildTag, feMergeChildTags, feLightningTags, feLightningChildTags, filterTag, filterChildTags, gradientTags, gradientChildTags, animateMotionChildTags, unsupportedTags, badTags, allValidTags } from '../data/tags.mjs';
|
|
3
3
|
|
|
4
4
|
const requiredParentTags = /* @__PURE__ */ new Map();
|
|
@@ -11,8 +11,8 @@ requiredParentTags.set(feLightningTags, feLightningChildTags);
|
|
|
11
11
|
requiredParentTags.set(filterTag, filterChildTags);
|
|
12
12
|
requiredParentTags.set(gradientTags, gradientChildTags);
|
|
13
13
|
requiredParentTags.set(/* @__PURE__ */ new Set(["animateMotion"]), animateMotionChildTags);
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
function checkBadTags(svg) {
|
|
15
|
+
parseSVGSync(svg, (item) => {
|
|
16
16
|
const tagName = item.tagName;
|
|
17
17
|
const $element = item.$element;
|
|
18
18
|
if (tagName === "svg") {
|
|
@@ -9,8 +9,8 @@ require('../../css/parser/strings.cjs');
|
|
|
9
9
|
require('../../css/parser/text.cjs');
|
|
10
10
|
require('../data/tags.cjs');
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
function cleanupInlineStyle(svg) {
|
|
13
|
+
svg_parse.parseSVGSync(svg, (item) => {
|
|
14
14
|
const $element = item.$element;
|
|
15
15
|
const attribs = item.element.attribs;
|
|
16
16
|
const tagName = item.tagName;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { parseInlineStyle } from '../../css/parse.mjs';
|
|
2
2
|
import { badAttributes, tagSpecificNonPresentationalAttributes, tagSpecificAnimatedAttributes, tagSpecificPresentationalAttributes, tagSpecificInlineStyles, insideClipPathAttributes, badSoftwareAttributes, badAttributePrefixes } from '../data/attributes.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { parseSVGSync } from '../parse.mjs';
|
|
4
4
|
import '../../css/parser/tokens.mjs';
|
|
5
5
|
import '../../css/parser/error.mjs';
|
|
6
6
|
import '../../css/parser/strings.mjs';
|
|
7
7
|
import '../../css/parser/text.mjs';
|
|
8
8
|
import '../data/tags.mjs';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
function cleanupInlineStyle(svg) {
|
|
11
|
+
parseSVGSync(svg, (item) => {
|
|
12
12
|
const $element = item.$element;
|
|
13
13
|
const attribs = item.element.attribs;
|
|
14
14
|
const tagName = item.tagName;
|
|
@@ -11,7 +11,7 @@ require('../../css/parser/tree.cjs');
|
|
|
11
11
|
require('../parse.cjs');
|
|
12
12
|
|
|
13
13
|
function cleanupRootStyle(svg) {
|
|
14
|
-
|
|
14
|
+
svg_parseStyle.parseSVGStyleSync(svg, (item) => {
|
|
15
15
|
if (
|
|
16
16
|
// If global style
|
|
17
17
|
item.type === "global" && // If selector tokens contain at-rule
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { SVG } from '../index.js';
|
|
2
|
-
import { parseSVGStyle } from '../parse-style.js';
|
|
3
2
|
import '@iconify/types';
|
|
4
3
|
import '@iconify/utils/lib/customisations/defaults';
|
|
5
|
-
import '../../css/parser/types.js';
|
|
6
|
-
import '../parse.js';
|
|
7
|
-
import '../../misc/cheerio.js';
|
|
8
4
|
|
|
9
5
|
/**
|
|
10
6
|
* Clean up root style
|
|
11
7
|
*
|
|
12
8
|
* This function removes all at-rule tokens, such as `@font-face`, `@media`
|
|
13
9
|
*/
|
|
14
|
-
declare function cleanupRootStyle(svg: SVG):
|
|
10
|
+
declare function cleanupRootStyle(svg: SVG): void;
|
|
15
11
|
|
|
16
12
|
export { cleanupRootStyle };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseSVGStyleSync } from '../parse-style.mjs';
|
|
2
2
|
import '../../css/parse.mjs';
|
|
3
3
|
import '../../css/parser/tokens.mjs';
|
|
4
4
|
import '../../css/parser/error.mjs';
|
|
@@ -9,7 +9,7 @@ import '../../css/parser/tree.mjs';
|
|
|
9
9
|
import '../parse.mjs';
|
|
10
10
|
|
|
11
11
|
function cleanupRootStyle(svg) {
|
|
12
|
-
|
|
12
|
+
parseSVGStyleSync(svg, (item) => {
|
|
13
13
|
if (
|
|
14
14
|
// If global style
|
|
15
15
|
item.type === "global" && // If selector tokens contain at-rule
|
|
@@ -15,9 +15,9 @@ require('../parse.cjs');
|
|
|
15
15
|
require('svgo');
|
|
16
16
|
require('@iconify/utils/lib/svg/id');
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
function convertStyleToAttrs(svg) {
|
|
19
19
|
let hasStyle = false;
|
|
20
|
-
|
|
20
|
+
svg_parseStyle.parseSVGStyleSync(svg, (item) => {
|
|
21
21
|
const prop = item.prop;
|
|
22
22
|
if (
|
|
23
23
|
// Attributes / properties now allowed
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { badAttributes, badSoftwareAttributes, badAttributePrefixes } from '../data/attributes.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { parseSVGStyleSync } from '../parse-style.mjs';
|
|
3
3
|
import { runSVGO } from '../../optimise/svgo.mjs';
|
|
4
4
|
import '../data/tags.mjs';
|
|
5
5
|
import '../../css/parse.mjs';
|
|
@@ -13,9 +13,9 @@ import '../parse.mjs';
|
|
|
13
13
|
import 'svgo';
|
|
14
14
|
import '@iconify/utils/lib/svg/id';
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
function convertStyleToAttrs(svg) {
|
|
17
17
|
let hasStyle = false;
|
|
18
|
-
|
|
18
|
+
parseSVGStyleSync(svg, (item) => {
|
|
19
19
|
const prop = item.prop;
|
|
20
20
|
if (
|
|
21
21
|
// Attributes / properties now allowed
|
package/lib/svg/cleanup.cjs
CHANGED
|
@@ -21,13 +21,13 @@ require('../optimise/svgo.cjs');
|
|
|
21
21
|
require('svgo');
|
|
22
22
|
require('@iconify/utils/lib/svg/id');
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
function cleanupSVG(svg) {
|
|
25
|
+
svg_cleanup_inlineStyle.cleanupInlineStyle(svg);
|
|
26
|
+
svg_cleanup_svgoStyle.convertStyleToAttrs(svg);
|
|
27
27
|
svg_cleanup_rootSvg.cleanupSVGRoot(svg);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
svg_cleanup_badTags.checkBadTags(svg);
|
|
29
|
+
svg_cleanup_attribs.removeBadAttributes(svg);
|
|
30
|
+
svg_cleanup_rootStyle.cleanupRootStyle(svg);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
exports.cleanupSVG = cleanupSVG;
|
package/lib/svg/cleanup.d.ts
CHANGED
package/lib/svg/cleanup.mjs
CHANGED
|
@@ -19,13 +19,13 @@ import '../optimise/svgo.mjs';
|
|
|
19
19
|
import 'svgo';
|
|
20
20
|
import '@iconify/utils/lib/svg/id';
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
function cleanupSVG(svg) {
|
|
23
|
+
cleanupInlineStyle(svg);
|
|
24
|
+
convertStyleToAttrs(svg);
|
|
25
25
|
cleanupSVGRoot(svg);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
checkBadTags(svg);
|
|
27
|
+
removeBadAttributes(svg);
|
|
28
|
+
cleanupRootStyle(svg);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export { cleanupSVG };
|
package/lib/svg/index.cjs
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
const cheerio = require('cheerio');
|
|
4
4
|
const utils = require('@iconify/utils');
|
|
5
5
|
|
|
6
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
7
|
+
|
|
8
|
+
const cheerio__default = /*#__PURE__*/_interopDefaultCompat(cheerio);
|
|
9
|
+
|
|
6
10
|
class SVG {
|
|
7
11
|
/**
|
|
8
12
|
* Constructor
|
|
@@ -104,7 +108,7 @@ class SVG {
|
|
|
104
108
|
);
|
|
105
109
|
remove('xml:space="preserve"', "", "");
|
|
106
110
|
content = content.replace(/<g>\s*<\/g>/g, "");
|
|
107
|
-
this.$svg =
|
|
111
|
+
this.$svg = cheerio__default.load(content.trim(), {
|
|
108
112
|
lowerCaseAttributeNames: false,
|
|
109
113
|
xmlMode: true
|
|
110
114
|
});
|
package/lib/svg/parse-style.cjs
CHANGED
|
@@ -9,41 +9,58 @@ require('../css/parser/error.cjs');
|
|
|
9
9
|
require('../css/parser/strings.cjs');
|
|
10
10
|
require('../css/parser/text.cjs');
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
const 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(newContent);
|
|
38
|
+
}
|
|
21
39
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
40
|
+
done2();
|
|
41
|
+
};
|
|
42
|
+
const nextToken = () => {
|
|
43
|
+
const token = tokens.shift();
|
|
44
|
+
if (token === void 0) {
|
|
45
|
+
return parsedTokens();
|
|
25
46
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
const value = token.value;
|
|
45
|
-
const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
|
|
46
|
-
let result = callback({
|
|
47
|
+
switch (token.type) {
|
|
48
|
+
case "selector":
|
|
49
|
+
case "at-rule":
|
|
50
|
+
selectorStart.push(newTokens.length);
|
|
51
|
+
break;
|
|
52
|
+
case "close":
|
|
53
|
+
selectorStart.pop();
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (token.type !== "rule") {
|
|
57
|
+
newTokens.push(token);
|
|
58
|
+
return nextToken();
|
|
59
|
+
}
|
|
60
|
+
const value = token.value;
|
|
61
|
+
const selectorTokens = selectorStart.map((index) => newTokens[index]).filter((item2) => item2 !== null);
|
|
62
|
+
callback(
|
|
63
|
+
{
|
|
47
64
|
type: "global",
|
|
48
65
|
prop: token.prop,
|
|
49
66
|
value,
|
|
@@ -61,77 +78,120 @@ async function parseSVGStyle(svg, callback) {
|
|
|
61
78
|
[]
|
|
62
79
|
),
|
|
63
80
|
prevTokens: newTokens,
|
|
64
|
-
nextTokens: tokens.slice(
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
result
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
nextTokens: tokens.slice(0)
|
|
82
|
+
},
|
|
83
|
+
(result) => {
|
|
84
|
+
if (result !== void 0) {
|
|
85
|
+
if (result !== value) {
|
|
86
|
+
changed2 = true;
|
|
87
|
+
token.value = result;
|
|
88
|
+
}
|
|
89
|
+
newTokens.push(token);
|
|
90
|
+
} else {
|
|
71
91
|
changed2 = true;
|
|
72
|
-
token.value = result;
|
|
73
92
|
}
|
|
74
|
-
|
|
75
|
-
} else {
|
|
76
|
-
changed2 = true;
|
|
93
|
+
nextToken();
|
|
77
94
|
}
|
|
78
|
-
}
|
|
79
|
-
if (!changed2) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const tree = css_parser_tree.tokensTree(
|
|
83
|
-
newTokens.filter((token) => token !== null)
|
|
84
95
|
);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
};
|
|
97
|
+
nextToken();
|
|
98
|
+
}
|
|
99
|
+
if (tagName === "style") {
|
|
100
|
+
return parseStyleItem(done);
|
|
101
|
+
}
|
|
102
|
+
const attribs = item.element.attribs;
|
|
103
|
+
if (attribs.style === void 0) {
|
|
104
|
+
return done();
|
|
105
|
+
}
|
|
106
|
+
const parsedStyle = css_parse.parseInlineStyle(attribs.style);
|
|
107
|
+
if (parsedStyle === null) {
|
|
108
|
+
$element.removeAttr("style");
|
|
109
|
+
return done();
|
|
110
|
+
}
|
|
111
|
+
const propsQueue = Object.keys(parsedStyle);
|
|
112
|
+
let changed = false;
|
|
113
|
+
const parsedProps = () => {
|
|
114
|
+
if (changed) {
|
|
115
|
+
const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
|
|
116
|
+
if (!newStyle.length) {
|
|
117
|
+
$element.removeAttr("style");
|
|
118
|
+
} else {
|
|
119
|
+
$element.attr("style", newStyle);
|
|
88
120
|
}
|
|
89
|
-
const newContent = css_parser_export.tokensToString(tree);
|
|
90
|
-
item.$element.text(newContent);
|
|
91
|
-
return;
|
|
92
121
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
122
|
+
done();
|
|
123
|
+
};
|
|
124
|
+
const nextProp = () => {
|
|
125
|
+
const prop = propsQueue.shift();
|
|
126
|
+
if (prop === void 0) {
|
|
127
|
+
return parsedProps();
|
|
96
128
|
}
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
const props = Object.keys(parsedStyle);
|
|
103
|
-
let changed = false;
|
|
104
|
-
for (let i = 0; i < props.length; i++) {
|
|
105
|
-
const prop = props[i];
|
|
106
|
-
const value = parsedStyle[prop];
|
|
107
|
-
let result = callback({
|
|
129
|
+
const value = parsedStyle[prop];
|
|
130
|
+
callback(
|
|
131
|
+
{
|
|
108
132
|
type: "inline",
|
|
109
133
|
prop,
|
|
110
134
|
value,
|
|
111
135
|
item
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
result
|
|
136
|
+
},
|
|
137
|
+
(result) => {
|
|
138
|
+
if (result !== value) {
|
|
139
|
+
changed = true;
|
|
140
|
+
if (result === void 0) {
|
|
141
|
+
delete parsedStyle[prop];
|
|
142
|
+
} else {
|
|
143
|
+
parsedStyle[prop] = result;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
nextProp();
|
|
115
147
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
148
|
+
);
|
|
149
|
+
};
|
|
150
|
+
nextProp();
|
|
151
|
+
}
|
|
152
|
+
async function parseSVGStyle(svg, callback) {
|
|
153
|
+
return svg_parse.parseSVG(svg, (item) => {
|
|
154
|
+
return new Promise((fulfill, reject) => {
|
|
155
|
+
try {
|
|
156
|
+
parseItem(
|
|
157
|
+
item,
|
|
158
|
+
(styleItem, done) => {
|
|
159
|
+
try {
|
|
160
|
+
const result = callback(styleItem);
|
|
161
|
+
if (result instanceof Promise) {
|
|
162
|
+
result.then(done).catch(reject);
|
|
163
|
+
} else {
|
|
164
|
+
done(result);
|
|
165
|
+
}
|
|
166
|
+
} catch (err) {
|
|
167
|
+
reject(err);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
fulfill
|
|
171
|
+
);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
reject(err);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function parseSVGStyleSync(svg, callback) {
|
|
179
|
+
let isSync = true;
|
|
180
|
+
svg_parse.parseSVGSync(svg, (item) => {
|
|
181
|
+
parseItem(
|
|
182
|
+
item,
|
|
183
|
+
(styleItem, done) => {
|
|
184
|
+
done(callback(styleItem));
|
|
185
|
+
},
|
|
186
|
+
() => {
|
|
187
|
+
if (!isSync) {
|
|
188
|
+
throw new Error("parseSVGStyleSync callback was async");
|
|
122
189
|
}
|
|
123
190
|
}
|
|
124
|
-
|
|
125
|
-
if (!changed) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const newStyle = Object.keys(parsedStyle).map((key) => key + ":" + parsedStyle[key] + ";").join("");
|
|
129
|
-
if (!newStyle.length) {
|
|
130
|
-
$element.removeAttr("style");
|
|
131
|
-
} else {
|
|
132
|
-
$element.attr("style", newStyle);
|
|
133
|
-
}
|
|
191
|
+
);
|
|
134
192
|
});
|
|
193
|
+
isSync = false;
|
|
135
194
|
}
|
|
136
195
|
|
|
137
196
|
exports.parseSVGStyle = parseSVGStyle;
|
|
197
|
+
exports.parseSVGStyleSync = parseSVGStyleSync;
|
package/lib/svg/parse-style.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ type ParseSVGStyleCallbackResult = string | undefined;
|
|
|
33
33
|
* Callback function
|
|
34
34
|
*/
|
|
35
35
|
type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult | Promise<ParseSVGStyleCallbackResult>;
|
|
36
|
+
type ParseSVGStyleCallbackSync = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleCallbackResult;
|
|
36
37
|
/**
|
|
37
38
|
* Parse styles in SVG
|
|
38
39
|
*
|
|
@@ -41,5 +42,9 @@ type ParseSVGStyleCallback = (item: ParseSVGStyleCallbackItem) => ParseSVGStyleC
|
|
|
41
42
|
* Callback can be asynchronous.
|
|
42
43
|
*/
|
|
43
44
|
declare function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Synchronous version
|
|
47
|
+
*/
|
|
48
|
+
declare function parseSVGStyleSync(svg: SVG, callback: ParseSVGStyleCallbackSync): void;
|
|
44
49
|
|
|
45
|
-
export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, parseSVGStyle };
|
|
50
|
+
export { ParseSVGStyleCallback, ParseSVGStyleCallbackItem, ParseSVGStyleCallbackResult, ParseSVGStyleCallbackSync, parseSVGStyle, parseSVGStyleSync };
|