svgo-v2 2.8.0
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/LICENSE +21 -0
- package/README.md +294 -0
- package/bin/svgo +10 -0
- package/dist/svgo.browser.js +1 -0
- package/lib/css-tools.js +239 -0
- package/lib/parser.js +259 -0
- package/lib/path.js +347 -0
- package/lib/stringifier.js +326 -0
- package/lib/style.js +283 -0
- package/lib/svgo/coa.js +517 -0
- package/lib/svgo/config.js +138 -0
- package/lib/svgo/css-class-list.js +72 -0
- package/lib/svgo/css-select-adapter.d.ts +2 -0
- package/lib/svgo/css-select-adapter.js +120 -0
- package/lib/svgo/css-style-declaration.js +232 -0
- package/lib/svgo/jsAPI.d.ts +2 -0
- package/lib/svgo/jsAPI.js +443 -0
- package/lib/svgo/plugins.js +109 -0
- package/lib/svgo/tools.js +137 -0
- package/lib/svgo-node.js +106 -0
- package/lib/svgo.js +83 -0
- package/lib/types.ts +172 -0
- package/lib/xast.js +102 -0
- package/package.json +130 -0
- package/plugins/_applyTransforms.js +335 -0
- package/plugins/_collections.js +2168 -0
- package/plugins/_path.js +816 -0
- package/plugins/_transforms.js +379 -0
- package/plugins/addAttributesToSVGElement.js +87 -0
- package/plugins/addClassesToSVGElement.js +87 -0
- package/plugins/cleanupAttrs.js +55 -0
- package/plugins/cleanupEnableBackground.js +75 -0
- package/plugins/cleanupIDs.js +297 -0
- package/plugins/cleanupListOfValues.js +154 -0
- package/plugins/cleanupNumericValues.js +113 -0
- package/plugins/collapseGroups.js +135 -0
- package/plugins/convertColors.js +152 -0
- package/plugins/convertEllipseToCircle.js +39 -0
- package/plugins/convertPathData.js +1023 -0
- package/plugins/convertShapeToPath.js +175 -0
- package/plugins/convertStyleToAttrs.js +132 -0
- package/plugins/convertTransform.js +432 -0
- package/plugins/inlineStyles.js +379 -0
- package/plugins/mergePaths.js +104 -0
- package/plugins/mergeStyles.js +93 -0
- package/plugins/minifyStyles.js +148 -0
- package/plugins/moveElemsAttrsToGroup.js +130 -0
- package/plugins/moveGroupAttrsToElems.js +62 -0
- package/plugins/plugins.js +56 -0
- package/plugins/prefixIds.js +241 -0
- package/plugins/preset-default.js +80 -0
- package/plugins/removeAttributesBySelector.js +99 -0
- package/plugins/removeAttrs.js +159 -0
- package/plugins/removeComments.js +31 -0
- package/plugins/removeDesc.js +41 -0
- package/plugins/removeDimensions.js +43 -0
- package/plugins/removeDoctype.js +42 -0
- package/plugins/removeEditorsNSData.js +68 -0
- package/plugins/removeElementsByAttr.js +78 -0
- package/plugins/removeEmptyAttrs.js +33 -0
- package/plugins/removeEmptyContainers.js +58 -0
- package/plugins/removeEmptyText.js +57 -0
- package/plugins/removeHiddenElems.js +318 -0
- package/plugins/removeMetadata.js +29 -0
- package/plugins/removeNonInheritableGroupAttrs.js +38 -0
- package/plugins/removeOffCanvasPaths.js +138 -0
- package/plugins/removeRasterImages.js +33 -0
- package/plugins/removeScriptElement.js +29 -0
- package/plugins/removeStyleElement.js +29 -0
- package/plugins/removeTitle.js +29 -0
- package/plugins/removeUnknownsAndDefaults.js +218 -0
- package/plugins/removeUnusedNS.js +61 -0
- package/plugins/removeUselessDefs.js +65 -0
- package/plugins/removeUselessStrokeAndFill.js +144 -0
- package/plugins/removeViewBox.js +51 -0
- package/plugins/removeXMLNS.js +30 -0
- package/plugins/removeXMLProcInst.js +30 -0
- package/plugins/reusePaths.js +113 -0
- package/plugins/sortAttrs.js +113 -0
- package/plugins/sortDefsChildren.js +60 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const collections = require('./_collections.js');
|
|
4
|
+
|
|
5
|
+
exports.type = 'visitor';
|
|
6
|
+
exports.name = 'convertColors';
|
|
7
|
+
exports.active = true;
|
|
8
|
+
exports.description = 'converts colors: rgb() to #rrggbb and #rrggbb to #rgb';
|
|
9
|
+
|
|
10
|
+
const rNumber = '([+-]?(?:\\d*\\.\\d+|\\d+\\.?)%?)';
|
|
11
|
+
const rComma = '\\s*,\\s*';
|
|
12
|
+
const regRGB = new RegExp(
|
|
13
|
+
'^rgb\\(\\s*' + rNumber + rComma + rNumber + rComma + rNumber + '\\s*\\)$'
|
|
14
|
+
);
|
|
15
|
+
const regHEX = /^#(([a-fA-F0-9])\2){3}$/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Convert [r, g, b] to #rrggbb.
|
|
19
|
+
*
|
|
20
|
+
* @see https://gist.github.com/983535
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* rgb2hex([255, 255, 255]) // '#ffffff'
|
|
24
|
+
*
|
|
25
|
+
* @author Jed Schmidt
|
|
26
|
+
*
|
|
27
|
+
* @type {(rgb: Array<number>) => string}
|
|
28
|
+
*/
|
|
29
|
+
const convertRgbToHex = ([r, g, b]) => {
|
|
30
|
+
// combine the octets into a 32-bit integer as: [1][r][g][b]
|
|
31
|
+
const hexNumber =
|
|
32
|
+
// operator precedence is (+) > (<<) > (|)
|
|
33
|
+
((((256 + // [1][0]
|
|
34
|
+
r) << // [1][r]
|
|
35
|
+
8) | // [1][r][0]
|
|
36
|
+
g) << // [1][r][g]
|
|
37
|
+
8) | // [1][r][g][0]
|
|
38
|
+
b;
|
|
39
|
+
// serialize [1][r][g][b] to a hex string, and
|
|
40
|
+
// remove the 1 to get the number with 0s intact
|
|
41
|
+
return '#' + hexNumber.toString(16).slice(1).toUpperCase();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Convert different colors formats in element attributes to hex.
|
|
46
|
+
*
|
|
47
|
+
* @see https://www.w3.org/TR/SVG11/types.html#DataTypeColor
|
|
48
|
+
* @see https://www.w3.org/TR/SVG11/single-page.html#types-ColorKeywords
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* Convert color name keyword to long hex:
|
|
52
|
+
* fuchsia ➡ #ff00ff
|
|
53
|
+
*
|
|
54
|
+
* Convert rgb() to long hex:
|
|
55
|
+
* rgb(255, 0, 255) ➡ #ff00ff
|
|
56
|
+
* rgb(50%, 100, 100%) ➡ #7f64ff
|
|
57
|
+
*
|
|
58
|
+
* Convert long hex to short hex:
|
|
59
|
+
* #aabbcc ➡ #abc
|
|
60
|
+
*
|
|
61
|
+
* Convert hex to short name
|
|
62
|
+
* #000080 ➡ navy
|
|
63
|
+
*
|
|
64
|
+
* @author Kir Belevich
|
|
65
|
+
*
|
|
66
|
+
* @type {import('../lib/types').Plugin<{
|
|
67
|
+
* currentColor?: boolean | string | RegExp,
|
|
68
|
+
* names2hex?: boolean,
|
|
69
|
+
* rgb2hex?: boolean,
|
|
70
|
+
* shorthex?: boolean,
|
|
71
|
+
* shortname?: boolean,
|
|
72
|
+
* }>}
|
|
73
|
+
*/
|
|
74
|
+
exports.fn = (_root, params) => {
|
|
75
|
+
const {
|
|
76
|
+
currentColor = false,
|
|
77
|
+
names2hex = true,
|
|
78
|
+
rgb2hex = true,
|
|
79
|
+
shorthex = true,
|
|
80
|
+
shortname = true,
|
|
81
|
+
} = params;
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
element: {
|
|
85
|
+
enter: (node) => {
|
|
86
|
+
for (const [name, value] of Object.entries(node.attributes)) {
|
|
87
|
+
if (collections.colorsProps.includes(name)) {
|
|
88
|
+
let val = value;
|
|
89
|
+
|
|
90
|
+
// convert colors to currentColor
|
|
91
|
+
if (currentColor) {
|
|
92
|
+
let matched;
|
|
93
|
+
if (typeof currentColor === 'string') {
|
|
94
|
+
matched = val === currentColor;
|
|
95
|
+
} else if (currentColor instanceof RegExp) {
|
|
96
|
+
matched = currentColor.exec(val) != null;
|
|
97
|
+
} else {
|
|
98
|
+
matched = val !== 'none';
|
|
99
|
+
}
|
|
100
|
+
if (matched) {
|
|
101
|
+
val = 'currentColor';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// convert color name keyword to long hex
|
|
106
|
+
if (names2hex) {
|
|
107
|
+
const colorName = val.toLowerCase();
|
|
108
|
+
if (collections.colorsNames[colorName] != null) {
|
|
109
|
+
val = collections.colorsNames[colorName];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// convert rgb() to long hex
|
|
114
|
+
if (rgb2hex) {
|
|
115
|
+
let match = val.match(regRGB);
|
|
116
|
+
if (match != null) {
|
|
117
|
+
let nums = match.slice(1, 4).map((m) => {
|
|
118
|
+
let n;
|
|
119
|
+
if (m.indexOf('%') > -1) {
|
|
120
|
+
n = Math.round(parseFloat(m) * 2.55);
|
|
121
|
+
} else {
|
|
122
|
+
n = Number(m);
|
|
123
|
+
}
|
|
124
|
+
return Math.max(0, Math.min(n, 255));
|
|
125
|
+
});
|
|
126
|
+
val = convertRgbToHex(nums);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// convert long hex to short hex
|
|
131
|
+
if (shorthex) {
|
|
132
|
+
let match = val.match(regHEX);
|
|
133
|
+
if (match != null) {
|
|
134
|
+
val = '#' + match[0][1] + match[0][3] + match[0][5];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// convert hex to short name
|
|
139
|
+
if (shortname) {
|
|
140
|
+
const colorName = val.toLowerCase();
|
|
141
|
+
if (collections.colorsShortNames[colorName] != null) {
|
|
142
|
+
val = collections.colorsShortNames[colorName];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
node.attributes[name] = val;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.name = 'convertEllipseToCircle';
|
|
4
|
+
exports.type = 'visitor';
|
|
5
|
+
exports.active = true;
|
|
6
|
+
exports.description = 'converts non-eccentric <ellipse>s to <circle>s';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts non-eccentric <ellipse>s to <circle>s.
|
|
10
|
+
*
|
|
11
|
+
* @see https://www.w3.org/TR/SVG11/shapes.html
|
|
12
|
+
*
|
|
13
|
+
* @author Taylor Hunt
|
|
14
|
+
*
|
|
15
|
+
* @type {import('../lib/types').Plugin<void>}
|
|
16
|
+
*/
|
|
17
|
+
exports.fn = () => {
|
|
18
|
+
return {
|
|
19
|
+
element: {
|
|
20
|
+
enter: (node) => {
|
|
21
|
+
if (node.name === 'ellipse') {
|
|
22
|
+
const rx = node.attributes.rx || '0';
|
|
23
|
+
const ry = node.attributes.ry || '0';
|
|
24
|
+
if (
|
|
25
|
+
rx === ry ||
|
|
26
|
+
rx === 'auto' ||
|
|
27
|
+
ry === 'auto' // SVG2
|
|
28
|
+
) {
|
|
29
|
+
node.name = 'circle';
|
|
30
|
+
const radius = rx === 'auto' ? ry : rx;
|
|
31
|
+
delete node.attributes.rx;
|
|
32
|
+
delete node.attributes.ry;
|
|
33
|
+
node.attributes.r = radius;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|