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.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +294 -0
  3. package/bin/svgo +10 -0
  4. package/dist/svgo.browser.js +1 -0
  5. package/lib/css-tools.js +239 -0
  6. package/lib/parser.js +259 -0
  7. package/lib/path.js +347 -0
  8. package/lib/stringifier.js +326 -0
  9. package/lib/style.js +283 -0
  10. package/lib/svgo/coa.js +517 -0
  11. package/lib/svgo/config.js +138 -0
  12. package/lib/svgo/css-class-list.js +72 -0
  13. package/lib/svgo/css-select-adapter.d.ts +2 -0
  14. package/lib/svgo/css-select-adapter.js +120 -0
  15. package/lib/svgo/css-style-declaration.js +232 -0
  16. package/lib/svgo/jsAPI.d.ts +2 -0
  17. package/lib/svgo/jsAPI.js +443 -0
  18. package/lib/svgo/plugins.js +109 -0
  19. package/lib/svgo/tools.js +137 -0
  20. package/lib/svgo-node.js +106 -0
  21. package/lib/svgo.js +83 -0
  22. package/lib/types.ts +172 -0
  23. package/lib/xast.js +102 -0
  24. package/package.json +130 -0
  25. package/plugins/_applyTransforms.js +335 -0
  26. package/plugins/_collections.js +2168 -0
  27. package/plugins/_path.js +816 -0
  28. package/plugins/_transforms.js +379 -0
  29. package/plugins/addAttributesToSVGElement.js +87 -0
  30. package/plugins/addClassesToSVGElement.js +87 -0
  31. package/plugins/cleanupAttrs.js +55 -0
  32. package/plugins/cleanupEnableBackground.js +75 -0
  33. package/plugins/cleanupIDs.js +297 -0
  34. package/plugins/cleanupListOfValues.js +154 -0
  35. package/plugins/cleanupNumericValues.js +113 -0
  36. package/plugins/collapseGroups.js +135 -0
  37. package/plugins/convertColors.js +152 -0
  38. package/plugins/convertEllipseToCircle.js +39 -0
  39. package/plugins/convertPathData.js +1023 -0
  40. package/plugins/convertShapeToPath.js +175 -0
  41. package/plugins/convertStyleToAttrs.js +132 -0
  42. package/plugins/convertTransform.js +432 -0
  43. package/plugins/inlineStyles.js +379 -0
  44. package/plugins/mergePaths.js +104 -0
  45. package/plugins/mergeStyles.js +93 -0
  46. package/plugins/minifyStyles.js +148 -0
  47. package/plugins/moveElemsAttrsToGroup.js +130 -0
  48. package/plugins/moveGroupAttrsToElems.js +62 -0
  49. package/plugins/plugins.js +56 -0
  50. package/plugins/prefixIds.js +241 -0
  51. package/plugins/preset-default.js +80 -0
  52. package/plugins/removeAttributesBySelector.js +99 -0
  53. package/plugins/removeAttrs.js +159 -0
  54. package/plugins/removeComments.js +31 -0
  55. package/plugins/removeDesc.js +41 -0
  56. package/plugins/removeDimensions.js +43 -0
  57. package/plugins/removeDoctype.js +42 -0
  58. package/plugins/removeEditorsNSData.js +68 -0
  59. package/plugins/removeElementsByAttr.js +78 -0
  60. package/plugins/removeEmptyAttrs.js +33 -0
  61. package/plugins/removeEmptyContainers.js +58 -0
  62. package/plugins/removeEmptyText.js +57 -0
  63. package/plugins/removeHiddenElems.js +318 -0
  64. package/plugins/removeMetadata.js +29 -0
  65. package/plugins/removeNonInheritableGroupAttrs.js +38 -0
  66. package/plugins/removeOffCanvasPaths.js +138 -0
  67. package/plugins/removeRasterImages.js +33 -0
  68. package/plugins/removeScriptElement.js +29 -0
  69. package/plugins/removeStyleElement.js +29 -0
  70. package/plugins/removeTitle.js +29 -0
  71. package/plugins/removeUnknownsAndDefaults.js +218 -0
  72. package/plugins/removeUnusedNS.js +61 -0
  73. package/plugins/removeUselessDefs.js +65 -0
  74. package/plugins/removeUselessStrokeAndFill.js +144 -0
  75. package/plugins/removeViewBox.js +51 -0
  76. package/plugins/removeXMLNS.js +30 -0
  77. package/plugins/removeXMLProcInst.js +30 -0
  78. package/plugins/reusePaths.js +113 -0
  79. package/plugins/sortAttrs.js +113 -0
  80. 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
+ };