@iconify/tools 2.0.14 → 2.0.15
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/lib/colors/attribs.cjs +30 -0
- package/lib/colors/attribs.d.ts +13 -10
- package/lib/colors/attribs.mjs +7 -13
- package/lib/colors/parse.cjs +269 -0
- package/lib/colors/parse.d.ts +15 -11
- package/lib/colors/parse.mjs +27 -31
- package/lib/colors/validate.cjs +52 -0
- package/lib/colors/validate.d.ts +11 -4
- package/lib/colors/validate.mjs +19 -6
- package/lib/css/parse.cjs +26 -0
- package/lib/css/parse.d.ts +3 -1
- package/lib/css/parse.mjs +8 -6
- package/lib/css/parser/error.cjs +25 -0
- package/lib/css/parser/error.d.ts +4 -2
- package/lib/css/parser/error.mjs +2 -4
- package/lib/css/parser/export.cjs +48 -0
- package/lib/css/parser/export.d.ts +5 -2
- package/lib/css/parser/export.mjs +4 -6
- package/lib/css/parser/strings.cjs +77 -0
- package/lib/css/parser/strings.d.ts +6 -3
- package/lib/css/parser/strings.mjs +4 -6
- package/lib/css/parser/text.cjs +135 -0
- package/lib/css/parser/text.d.ts +8 -5
- package/lib/css/parser/text.mjs +3 -8
- package/lib/css/parser/tokens.cjs +184 -0
- package/lib/css/parser/tokens.d.ts +6 -3
- package/lib/css/parser/tokens.mjs +7 -13
- package/lib/css/parser/tree.cjs +42 -0
- package/lib/css/parser/tree.d.ts +5 -2
- package/lib/css/parser/tree.mjs +2 -4
- package/lib/css/parser/types.cjs +2 -0
- package/lib/css/parser/types.d.ts +13 -11
- package/lib/css/parser/types.mjs +1 -0
- package/lib/download/api/cache.cjs +86 -0
- package/lib/download/api/cache.d.ts +8 -5
- package/lib/download/api/cache.mjs +14 -19
- package/lib/download/api/download.cjs +28 -0
- package/lib/download/api/download.d.ts +5 -2
- package/lib/download/api/download.mjs +8 -9
- package/lib/download/api/index.cjs +51 -0
- package/lib/download/api/index.d.ts +5 -2
- package/lib/download/api/index.mjs +8 -6
- package/lib/download/api/types.cjs +2 -0
- package/lib/download/api/types.d.ts +4 -2
- package/lib/download/api/types.mjs +1 -0
- package/lib/download/git/branch.cjs +22 -0
- package/lib/download/git/branch.d.ts +5 -2
- package/lib/download/git/branch.mjs +5 -5
- package/lib/download/git/hash.cjs +15 -0
- package/lib/download/git/hash.d.ts +5 -2
- package/lib/download/git/hash.mjs +5 -5
- package/lib/download/git/index.cjs +76 -0
- package/lib/download/git/index.d.ts +10 -8
- package/lib/download/git/index.mjs +41 -26
- package/lib/download/git/reset.cjs +41 -0
- package/lib/download/git/reset.d.ts +3 -1
- package/lib/download/git/reset.mjs +29 -5
- package/lib/download/github/hash.cjs +32 -0
- package/lib/download/github/hash.d.ts +5 -2
- package/lib/download/github/hash.mjs +10 -7
- package/lib/download/github/index.cjs +98 -0
- package/lib/download/github/index.d.ts +11 -9
- package/lib/download/github/index.mjs +23 -17
- package/lib/download/github/types.cjs +2 -0
- package/lib/download/github/types.d.ts +3 -1
- package/lib/download/github/types.mjs +1 -0
- package/lib/download/gitlab/hash.cjs +32 -0
- package/lib/download/gitlab/hash.d.ts +5 -2
- package/lib/download/gitlab/hash.mjs +10 -6
- package/lib/download/gitlab/index.cjs +98 -0
- package/lib/download/gitlab/index.d.ts +11 -9
- package/lib/download/gitlab/index.mjs +24 -18
- package/lib/download/gitlab/types.cjs +7 -0
- package/lib/download/gitlab/types.d.ts +4 -2
- package/lib/download/gitlab/types.mjs +3 -5
- package/lib/download/helpers/untar.cjs +14 -0
- package/lib/download/helpers/untar.d.ts +3 -1
- package/lib/download/helpers/untar.mjs +4 -5
- package/lib/download/helpers/unzip.cjs +19 -0
- package/lib/download/helpers/unzip.d.ts +3 -1
- package/lib/download/helpers/unzip.mjs +6 -7
- package/lib/download/index.cjs +64 -0
- package/lib/download/index.d.ts +18 -13
- package/lib/download/index.mjs +46 -19
- package/lib/download/npm/index.cjs +82 -0
- package/lib/download/npm/index.d.ts +10 -8
- package/lib/download/npm/index.mjs +16 -13
- package/lib/download/npm/types.cjs +2 -0
- package/lib/download/npm/types.d.ts +3 -1
- package/lib/download/npm/types.mjs +1 -0
- package/lib/download/npm/version.cjs +23 -0
- package/lib/download/npm/version.d.ts +7 -4
- package/lib/download/npm/version.mjs +8 -10
- package/lib/download/types/modified.cjs +2 -0
- package/lib/download/types/modified.d.ts +3 -1
- package/lib/download/types/modified.mjs +1 -0
- package/lib/download/types/sources.cjs +2 -0
- package/lib/download/types/sources.d.ts +4 -2
- package/lib/download/types/sources.mjs +1 -0
- package/lib/export/directory.cjs +45 -0
- package/lib/export/directory.d.ts +11 -4
- package/lib/export/directory.mjs +8 -8
- package/lib/export/helpers/custom-files.cjs +28 -0
- package/lib/export/helpers/custom-files.d.ts +4 -2
- package/lib/export/helpers/custom-files.mjs +8 -9
- package/lib/export/helpers/prepare.cjs +36 -0
- package/lib/export/helpers/prepare.d.ts +5 -3
- package/lib/export/helpers/prepare.mjs +7 -9
- package/lib/export/helpers/types-version.cjs +22 -0
- package/lib/export/helpers/types-version.d.ts +3 -1
- package/lib/export/helpers/types-version.mjs +15 -37
- package/lib/export/icon-package.cjs +59 -0
- package/lib/export/icon-package.d.ts +12 -5
- package/lib/export/icon-package.mjs +15 -16
- package/lib/export/json-package.cjs +133 -0
- package/lib/export/json-package.d.ts +12 -5
- package/lib/export/json-package.mjs +20 -21
- package/lib/icon-set/index.cjs +589 -0
- package/lib/icon-set/index.d.ts +9 -6
- package/lib/icon-set/index.mjs +40 -53
- package/lib/icon-set/match.cjs +58 -0
- package/lib/icon-set/match.d.ts +10 -3
- package/lib/icon-set/match.mjs +5 -6
- package/lib/icon-set/merge.cjs +83 -0
- package/lib/icon-set/merge.d.ts +9 -2
- package/lib/icon-set/merge.mjs +15 -10
- package/lib/icon-set/props.cjs +28 -0
- package/lib/icon-set/props.d.ts +8 -4
- package/lib/icon-set/props.mjs +7 -10
- package/lib/icon-set/types.cjs +2 -0
- package/lib/icon-set/types.d.ts +19 -17
- package/lib/icon-set/types.mjs +1 -0
- package/lib/import/directory.cjs +76 -0
- package/lib/import/directory.d.ts +11 -5
- package/lib/import/directory.mjs +36 -11
- package/lib/import/figma/index.cjs +105 -0
- package/lib/import/figma/index.d.ts +15 -5
- package/lib/import/figma/index.mjs +40 -13
- package/lib/import/figma/nodes.cjs +86 -0
- package/lib/import/figma/nodes.d.ts +13 -4
- package/lib/import/figma/nodes.mjs +3 -11
- package/lib/import/figma/query.cjs +218 -0
- package/lib/import/figma/query.d.ts +18 -9
- package/lib/import/figma/query.mjs +9 -12
- package/lib/import/figma/types/api.cjs +2 -0
- package/lib/import/figma/types/api.d.ts +8 -7
- package/lib/import/figma/types/api.mjs +1 -0
- package/lib/import/figma/types/nodes.cjs +2 -0
- package/lib/import/figma/types/nodes.d.ts +16 -9
- package/lib/import/figma/types/nodes.mjs +1 -0
- package/lib/import/figma/types/options.cjs +2 -0
- package/lib/import/figma/types/options.d.ts +17 -10
- package/lib/import/figma/types/options.mjs +1 -0
- package/lib/import/figma/types/result.cjs +2 -0
- package/lib/import/figma/types/result.d.ts +12 -5
- package/lib/import/figma/types/result.mjs +1 -0
- package/lib/index.cjs +137 -0
- package/lib/index.d.ts +65 -44
- package/lib/index.mjs +83 -94
- package/lib/misc/bump-version.cjs +17 -0
- package/lib/misc/bump-version.d.ts +3 -1
- package/lib/misc/bump-version.mjs +2 -4
- package/lib/misc/compare-dirs.cjs +75 -0
- package/lib/misc/compare-dirs.d.ts +4 -2
- package/lib/misc/compare-dirs.mjs +12 -12
- package/lib/misc/exec.cjs +25 -0
- package/lib/misc/exec.d.ts +6 -4
- package/lib/misc/exec.mjs +4 -5
- package/lib/misc/keyword.cjs +19 -0
- package/lib/misc/keyword.d.ts +3 -1
- package/lib/misc/keyword.mjs +2 -4
- package/lib/misc/scan.cjs +51 -0
- package/lib/misc/scan.d.ts +7 -6
- package/lib/misc/scan.mjs +6 -7
- package/lib/misc/write-json.cjs +11 -0
- package/lib/misc/write-json.d.ts +3 -1
- package/lib/misc/write-json.mjs +5 -6
- package/lib/optimise/flags.cjs +252 -0
- package/lib/optimise/flags.d.ts +7 -2
- package/lib/optimise/flags.mjs +18 -19
- package/lib/optimise/global-style.cjs +154 -0
- package/lib/optimise/global-style.d.ts +7 -2
- package/lib/optimise/global-style.mjs +18 -15
- package/lib/optimise/scale.cjs +26 -0
- package/lib/optimise/scale.d.ts +7 -2
- package/lib/optimise/scale.mjs +5 -5
- package/lib/optimise/svgo.cjs +83 -0
- package/lib/optimise/svgo.d.ts +10 -6
- package/lib/optimise/svgo.mjs +6 -9
- package/lib/svg/analyse/error.cjs +18 -0
- package/lib/svg/analyse/error.d.ts +5 -2
- package/lib/svg/analyse/error.mjs +2 -4
- package/lib/svg/analyse/types.cjs +2 -0
- package/lib/svg/analyse/types.d.ts +11 -11
- package/lib/svg/analyse/types.mjs +1 -0
- package/lib/svg/analyse.cjs +291 -0
- package/lib/svg/analyse.d.ts +8 -3
- package/lib/svg/analyse.mjs +15 -30
- package/lib/svg/cleanup/attribs.cjs +41 -0
- package/lib/svg/cleanup/attribs.d.ts +7 -2
- package/lib/svg/cleanup/attribs.mjs +6 -12
- package/lib/svg/cleanup/bad-tags.cjs +51 -0
- package/lib/svg/cleanup/bad-tags.d.ts +7 -2
- package/lib/svg/cleanup/bad-tags.mjs +10 -25
- package/lib/svg/cleanup/inline-style.cjs +64 -0
- package/lib/svg/cleanup/inline-style.d.ts +7 -2
- package/lib/svg/cleanup/inline-style.mjs +14 -19
- package/lib/svg/cleanup/root-svg.cjs +81 -0
- package/lib/svg/cleanup/root-svg.d.ts +7 -2
- package/lib/svg/cleanup/root-svg.mjs +7 -18
- package/lib/svg/cleanup/svgo-style.cjs +38 -0
- package/lib/svg/cleanup/svgo-style.d.ts +7 -2
- package/lib/svg/cleanup/svgo-style.mjs +16 -11
- package/lib/svg/cleanup.cjs +32 -0
- package/lib/svg/cleanup.d.ts +7 -2
- package/lib/svg/cleanup.mjs +21 -9
- package/lib/svg/data/attributes.cjs +355 -0
- package/lib/svg/data/attributes.d.ts +27 -25
- package/lib/svg/data/attributes.mjs +90 -115
- package/lib/svg/data/tags.cjs +137 -0
- package/lib/svg/data/tags.d.ts +25 -23
- package/lib/svg/data/tags.mjs +25 -49
- package/lib/svg/index.cjs +130 -0
- package/lib/svg/index.d.ts +16 -5
- package/lib/svg/index.mjs +48 -14
- package/lib/svg/parse-style.cjs +134 -0
- package/lib/svg/parse-style.d.ts +12 -8
- package/lib/svg/parse-style.mjs +11 -9
- package/lib/svg/parse.cjs +42 -0
- package/lib/svg/parse.d.ts +11 -7
- package/lib/svg/parse.mjs +2 -4
- package/package.json +99 -94
- package/lib/colors/attribs.js +0 -26
- package/lib/colors/parse.js +0 -341
- package/lib/colors/validate.js +0 -48
- package/lib/css/parse.js +0 -23
- package/lib/css/parser/error.js +0 -27
- package/lib/css/parser/export.js +0 -69
- package/lib/css/parser/strings.js +0 -93
- package/lib/css/parser/text.js +0 -171
- package/lib/css/parser/tokens.js +0 -200
- package/lib/css/parser/tree.js +0 -44
- package/lib/css/parser/types.js +0 -2
- package/lib/download/api/cache.js +0 -108
- package/lib/download/api/download.js +0 -27
- package/lib/download/api/index.js +0 -52
- package/lib/download/api/types.js +0 -2
- package/lib/download/git/branch.js +0 -22
- package/lib/download/git/hash.js +0 -14
- package/lib/download/git/index.js +0 -75
- package/lib/download/git/reset.js +0 -16
- package/lib/download/github/hash.js +0 -28
- package/lib/download/github/index.js +0 -116
- package/lib/download/github/types.js +0 -2
- package/lib/download/gitlab/hash.js +0 -27
- package/lib/download/gitlab/index.js +0 -115
- package/lib/download/gitlab/types.js +0 -7
- package/lib/download/helpers/untar.js +0 -14
- package/lib/download/helpers/unzip.js +0 -18
- package/lib/download/index.js +0 -27
- package/lib/download/npm/index.js +0 -91
- package/lib/download/npm/types.js +0 -2
- package/lib/download/npm/version.js +0 -27
- package/lib/download/types/modified.js +0 -2
- package/lib/download/types/sources.js +0 -2
- package/lib/export/directory.js +0 -52
- package/lib/export/helpers/custom-files.js +0 -32
- package/lib/export/helpers/prepare.js +0 -49
- package/lib/export/helpers/types-version.js +0 -12
- package/lib/export/icon-package.js +0 -77
- package/lib/export/json-package.js +0 -156
- package/lib/icon-set/index.js +0 -749
- package/lib/icon-set/match.js +0 -66
- package/lib/icon-set/merge.js +0 -90
- package/lib/icon-set/props.js +0 -34
- package/lib/icon-set/types.js +0 -2
- package/lib/import/directory.js +0 -59
- package/lib/import/figma/index.js +0 -90
- package/lib/import/figma/nodes.js +0 -106
- package/lib/import/figma/query.js +0 -244
- package/lib/import/figma/types/api.js +0 -12
- package/lib/import/figma/types/nodes.js +0 -2
- package/lib/import/figma/types/options.js +0 -2
- package/lib/import/figma/types/result.js +0 -2
- package/lib/index.js +0 -103
- package/lib/misc/bump-version.js +0 -19
- package/lib/misc/compare-dirs.js +0 -84
- package/lib/misc/exec.js +0 -26
- package/lib/misc/keyword.js +0 -31
- package/lib/misc/scan.js +0 -55
- package/lib/misc/write-json.js +0 -11
- package/lib/optimise/flags.js +0 -312
- package/lib/optimise/global-style.js +0 -185
- package/lib/optimise/scale.js +0 -42
- package/lib/optimise/svgo.js +0 -99
- package/lib/svg/analyse/error.js +0 -22
- package/lib/svg/analyse/types.js +0 -2
- package/lib/svg/analyse.js +0 -352
- package/lib/svg/cleanup/attribs.js +0 -51
- package/lib/svg/cleanup/bad-tags.js +0 -65
- package/lib/svg/cleanup/inline-style.js +0 -77
- package/lib/svg/cleanup/root-svg.js +0 -107
- package/lib/svg/cleanup/svgo-style.js +0 -35
- package/lib/svg/cleanup.js +0 -24
- package/lib/svg/data/attributes.js +0 -403
- package/lib/svg/data/tags.js +0 -188
- package/lib/svg/index.js +0 -134
- package/lib/svg/parse-style.js +0 -151
- package/lib/svg/parse.js +0 -49
package/lib/svg/index.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SVG = void 0;
|
|
7
|
-
const cheerio_1 = __importDefault(require("cheerio"));
|
|
8
|
-
/**
|
|
9
|
-
* Remove whitespace
|
|
10
|
-
*/
|
|
11
|
-
function minify(str) {
|
|
12
|
-
return (str
|
|
13
|
-
// Replace new line only after one of allowed characters that are not part of common attributes
|
|
14
|
-
.replace(/(["';{}}><])\s*\n\s*/g, '$1')
|
|
15
|
-
// Keep one space in case it is inside attribute
|
|
16
|
-
.replace(/\s*\n\s*/g, ' ')
|
|
17
|
-
.trim());
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* SVG class, used to manipulate icon content.
|
|
21
|
-
*/
|
|
22
|
-
class SVG {
|
|
23
|
-
/**
|
|
24
|
-
* Constructor
|
|
25
|
-
*/
|
|
26
|
-
constructor(content) {
|
|
27
|
-
this.load(content);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Get SVG as string
|
|
31
|
-
*/
|
|
32
|
-
toString() {
|
|
33
|
-
const $root = this.$svg(':root');
|
|
34
|
-
const box = this.viewBox;
|
|
35
|
-
// Add missing viewBox attribute
|
|
36
|
-
if ($root.attr('viewBox') === void 0) {
|
|
37
|
-
$root.attr('viewBox', box.left + ' ' + box.top + ' ' + box.width + ' ' + box.height);
|
|
38
|
-
}
|
|
39
|
-
// Add missing width/height
|
|
40
|
-
if ($root.attr('width') === void 0) {
|
|
41
|
-
$root.attr('width', box.width + '');
|
|
42
|
-
}
|
|
43
|
-
if ($root.attr('height') === void 0) {
|
|
44
|
-
$root.attr('height', box.height + '');
|
|
45
|
-
}
|
|
46
|
-
return this.$svg.html();
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Get SVG as string without whitespaces
|
|
50
|
-
*/
|
|
51
|
-
toMinifiedString() {
|
|
52
|
-
return minify(this.toString());
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Get body
|
|
56
|
-
*/
|
|
57
|
-
getBody() {
|
|
58
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
59
|
-
return minify(this.$svg('svg').html());
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Load SVG
|
|
63
|
-
*
|
|
64
|
-
* @param {string} content
|
|
65
|
-
*/
|
|
66
|
-
load(content) {
|
|
67
|
-
// Remove junk
|
|
68
|
-
function remove(str1, str2, append) {
|
|
69
|
-
let start = 0;
|
|
70
|
-
while ((start = content.indexOf(str1, start)) !== -1) {
|
|
71
|
-
const end = content.indexOf(str2, start + str1.length);
|
|
72
|
-
if (end === -1) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
content =
|
|
76
|
-
content.slice(0, start) +
|
|
77
|
-
append +
|
|
78
|
-
content.slice(end + str2.length);
|
|
79
|
-
start = start + append.length;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
// Remove comments
|
|
83
|
-
remove('<!--', '-->', '');
|
|
84
|
-
// Remove doctype and XML declaration
|
|
85
|
-
remove('<?xml', '?>', '');
|
|
86
|
-
remove('<!DOCTYPE svg', '<svg', '<svg');
|
|
87
|
-
// Remove Adobe Illustrator junk
|
|
88
|
-
remove('xmlns:x="&ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;"', '', '');
|
|
89
|
-
remove('xml:space="preserve"', '', '');
|
|
90
|
-
// Remove empty <g> elements
|
|
91
|
-
content = content.replace(/<g>\s*<\/g>/g, '');
|
|
92
|
-
// Load content
|
|
93
|
-
this.$svg = cheerio_1.default.load(content.trim(), {
|
|
94
|
-
lowerCaseAttributeNames: false,
|
|
95
|
-
xmlMode: true,
|
|
96
|
-
});
|
|
97
|
-
// Check root
|
|
98
|
-
const $root = this.$svg(':root');
|
|
99
|
-
if ($root.length > 1 || $root.get(0).tagName !== 'svg') {
|
|
100
|
-
throw new Error('Invalid SVG file: bad root tag');
|
|
101
|
-
}
|
|
102
|
-
// Get dimensions and origin
|
|
103
|
-
const viewBox = $root.attr('viewBox');
|
|
104
|
-
if (viewBox !== void 0) {
|
|
105
|
-
const list = viewBox.split(' ');
|
|
106
|
-
this.viewBox = {
|
|
107
|
-
left: parseFloat(list[0]),
|
|
108
|
-
top: parseFloat(list[1]),
|
|
109
|
-
width: parseFloat(list[2]),
|
|
110
|
-
height: parseFloat(list[3]),
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
const width = $root.attr('width');
|
|
115
|
-
const height = $root.attr('height');
|
|
116
|
-
if (!width || !height) {
|
|
117
|
-
throw new Error('Invalid SVG file: missing dimensions');
|
|
118
|
-
}
|
|
119
|
-
this.viewBox = {
|
|
120
|
-
left: 0,
|
|
121
|
-
top: 0,
|
|
122
|
-
width: parseFloat(width),
|
|
123
|
-
height: parseFloat(height),
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
Object.keys(this.viewBox).forEach((key) => {
|
|
127
|
-
const attr = key;
|
|
128
|
-
if (isNaN(this.viewBox[attr])) {
|
|
129
|
-
throw new Error(`Invalid SVG file: invalid ${attr}`);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
exports.SVG = SVG;
|
package/lib/svg/parse-style.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseSVGStyle = void 0;
|
|
4
|
-
const parse_1 = require("../css/parse");
|
|
5
|
-
const export_1 = require("../css/parser/export");
|
|
6
|
-
const tokens_1 = require("../css/parser/tokens");
|
|
7
|
-
const tree_1 = require("../css/parser/tree");
|
|
8
|
-
const parse_2 = require("./parse");
|
|
9
|
-
/**
|
|
10
|
-
* Parse styles in SVG
|
|
11
|
-
*
|
|
12
|
-
* This function finds CSS in SVG, parses it, calls callback for each rule.
|
|
13
|
-
* Callback should return new value (string) or undefined to remove rule.
|
|
14
|
-
* Callback can be asynchronous.
|
|
15
|
-
*/
|
|
16
|
-
async function parseSVGStyle(svg, callback) {
|
|
17
|
-
return (0, parse_2.parseSVG)(svg, async (item) => {
|
|
18
|
-
const tagName = item.tagName;
|
|
19
|
-
const $element = item.$element;
|
|
20
|
-
if (tagName === 'style') {
|
|
21
|
-
// Style tag
|
|
22
|
-
const content = $element.html();
|
|
23
|
-
if (typeof content !== 'string') {
|
|
24
|
-
$element.remove();
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const tokens = (0, tokens_1.getTokens)(content);
|
|
28
|
-
if (!(tokens instanceof Array)) {
|
|
29
|
-
// Invalid style
|
|
30
|
-
throw new Error('Error parsing style');
|
|
31
|
-
}
|
|
32
|
-
// Parse all tokens
|
|
33
|
-
let changed = false;
|
|
34
|
-
const selectorStart = [];
|
|
35
|
-
const newTokens = [];
|
|
36
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
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
|
-
}
|
|
47
|
-
if (token.type !== 'rule') {
|
|
48
|
-
newTokens.push(token);
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
const value = token.value;
|
|
52
|
-
const selectorTokens = selectorStart
|
|
53
|
-
.map((index) => newTokens[index])
|
|
54
|
-
.filter((item) => item !== null);
|
|
55
|
-
let result = callback({
|
|
56
|
-
type: 'global',
|
|
57
|
-
prop: token.prop,
|
|
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),
|
|
71
|
-
});
|
|
72
|
-
if (result instanceof Promise) {
|
|
73
|
-
result = await result;
|
|
74
|
-
}
|
|
75
|
-
if (result !== void 0) {
|
|
76
|
-
if (result !== value) {
|
|
77
|
-
changed = true;
|
|
78
|
-
token.value = result;
|
|
79
|
-
}
|
|
80
|
-
newTokens.push(token);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
// Delete token
|
|
84
|
-
changed = true;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (!changed) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
// Update style
|
|
91
|
-
const tree = (0, tree_1.tokensTree)(newTokens.filter((token) => token !== null));
|
|
92
|
-
if (!tree.length) {
|
|
93
|
-
// Empty
|
|
94
|
-
$element.remove();
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const newContent = (0, export_1.tokensToString)(tree);
|
|
98
|
-
item.$element.text(newContent);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
// Parse style
|
|
102
|
-
const attribs = item.element.attribs;
|
|
103
|
-
if (attribs.style === void 0) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const parsedStyle = (0, parse_1.parseInlineStyle)(attribs.style);
|
|
107
|
-
if (parsedStyle === null) {
|
|
108
|
-
// Ignore style
|
|
109
|
-
$element.removeAttr('style');
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const props = Object.keys(parsedStyle);
|
|
113
|
-
let changed = false;
|
|
114
|
-
for (let i = 0; i < props.length; i++) {
|
|
115
|
-
const prop = props[i];
|
|
116
|
-
const value = parsedStyle[prop];
|
|
117
|
-
let result = callback({
|
|
118
|
-
type: 'inline',
|
|
119
|
-
prop,
|
|
120
|
-
value,
|
|
121
|
-
item,
|
|
122
|
-
});
|
|
123
|
-
if (result instanceof Promise) {
|
|
124
|
-
result = await result;
|
|
125
|
-
}
|
|
126
|
-
if (result !== value) {
|
|
127
|
-
changed = true;
|
|
128
|
-
if (result === void 0) {
|
|
129
|
-
delete parsedStyle[prop];
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
parsedStyle[prop] = result;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
// Update style
|
|
137
|
-
if (!changed) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
const newStyle = Object.keys(parsedStyle)
|
|
141
|
-
.map((key) => key + ':' + parsedStyle[key] + ';')
|
|
142
|
-
.join('');
|
|
143
|
-
if (!newStyle.length) {
|
|
144
|
-
$element.removeAttr('style');
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
$element.attr('style', newStyle);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
exports.parseSVGStyle = parseSVGStyle;
|
package/lib/svg/parse.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseSVG = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Parse SVG
|
|
6
|
-
*
|
|
7
|
-
* This function finds all elements in SVG and calls callback for each element.
|
|
8
|
-
* Callback can be asynchronous.
|
|
9
|
-
*/
|
|
10
|
-
async function parseSVG(svg, callback) {
|
|
11
|
-
async function checkNode(element, parents) {
|
|
12
|
-
if (element.type !== 'tag') {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
const $element = cheerio(element);
|
|
16
|
-
const tagName = element.tagName;
|
|
17
|
-
const item = {
|
|
18
|
-
tagName,
|
|
19
|
-
element,
|
|
20
|
-
$element,
|
|
21
|
-
svg,
|
|
22
|
-
parents,
|
|
23
|
-
testChildren: true,
|
|
24
|
-
removeNode: false,
|
|
25
|
-
};
|
|
26
|
-
// Run callback
|
|
27
|
-
const result = callback(item);
|
|
28
|
-
if (result instanceof Promise) {
|
|
29
|
-
await result;
|
|
30
|
-
}
|
|
31
|
-
// Test child nodes
|
|
32
|
-
const newParents = parents.slice(0);
|
|
33
|
-
newParents.unshift(item);
|
|
34
|
-
if (tagName !== 'style' && item.testChildren && !item.removeNode) {
|
|
35
|
-
const children = $element.children().toArray();
|
|
36
|
-
for (let i = 0; i < children.length; i++) {
|
|
37
|
-
await checkNode(children[i], newParents);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// Remove node
|
|
41
|
-
if (item.removeNode) {
|
|
42
|
-
$element.remove();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const cheerio = svg.$svg;
|
|
46
|
-
const $root = svg.$svg(':root');
|
|
47
|
-
await checkNode($root.get(0), []);
|
|
48
|
-
}
|
|
49
|
-
exports.parseSVG = parseSVG;
|