Skip to content

Commit 6747e3a

Browse files
authored
feat(convertcolors): add param to convert colors to common case (#1692)
1 parent 9334b08 commit 6747e3a

File tree

12 files changed

+83
-22
lines changed

12 files changed

+83
-22
lines changed

docs/03-plugins/convert-colors.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ svgo:
55
defaultPlugin: true
66
parameters:
77
currentColor:
8-
description: If to convert all instances of a color to <code>currentColor</code>. This means to inherit the active foreground color, for example in HTML5 this would be the <a href="https://developer.mozilla.org/docs/Web/CSS/color" target="_blank"><code>color</code></a> property in CSS.
8+
description: If to convert all instances of a color to <code>currentcolor</code>. This means to inherit the active foreground color, for example in HTML5 this would be the <a href="https://developer.mozilla.org/docs/Web/CSS/color" target="_blank"><code>color</code></a> property in CSS.
99
default: false
1010
names2hex:
1111
description: If to convert color names to the hex equivalent.
1212
default: true
1313
rgb2hex:
14-
description: If to convert RGB colors to the hex equivalent, does ignores RGBA.
14+
description: If to convert RGB colors to the hex equivalent, ignores RGBA.
1515
default: true
16+
convertCase:
17+
description: Convert all color values to either upper or lower case by setting this to <code>'upper'</code> or <code>'lower'</code> respectively to improve compression. Set to <code>false</code> to disable this behavior.
18+
default: 'lower'
1619
shorthex:
1720
description: If to convert 6 character hex colors to the 3 character equivalent where possible.
1821
default: true

docs/03-plugins/remove-attrs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ svgo:
1010
description: The pattern syntax used by this plugin is <code>element:attribute:value</code>, this changes the delimiter from <code>:</code> to another string.
1111
default: ':'
1212
preserveCurrentColor:
13-
description: If to ignore the attribute if it's set to <code>currentColor</code>.
13+
description: If to ignore the attribute when it's set to <code>currentcolor</code>.
1414
default: false
1515
---
1616

plugins/convertColors.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { colorsNames, colorsProps, colorsShortNames } from './_collections.js';
2+
import { includesUrlReference } from '../lib/svgo/tools.js';
23

34
export const name = 'convertColors';
45
export const description =
@@ -67,6 +68,7 @@ export const fn = (_root, params) => {
6768
currentColor = false,
6869
names2hex = true,
6970
rgb2hex = true,
71+
convertCase = 'lower',
7072
shorthex = true,
7173
shortname = true,
7274
} = params;
@@ -89,7 +91,7 @@ export const fn = (_root, params) => {
8991
matched = val !== 'none';
9092
}
9193
if (matched) {
92-
val = 'currentColor';
94+
val = 'currentcolor';
9395
}
9496
}
9597

@@ -118,9 +120,17 @@ export const fn = (_root, params) => {
118120
}
119121
}
120122

123+
if (convertCase && !includesUrlReference(val)) {
124+
if (convertCase === 'lower') {
125+
val = val.toLowerCase();
126+
} else if (convertCase === 'upper') {
127+
val = val.toUpperCase();
128+
}
129+
}
130+
121131
// convert long hex to short hex
122132
if (shorthex) {
123-
let match = val.match(regHEX);
133+
let match = regHEX.exec(val);
124134
if (match != null) {
125135
val = '#' + match[0][1] + match[0][3] + match[0][5];
126136
}

plugins/plugins-types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type DefaultPlugins = {
2929
currentColor?: boolean | string | RegExp;
3030
names2hex?: boolean;
3131
rgb2hex?: boolean;
32+
convertCase?: false | 'lower' | 'upper';
3233
shorthex?: boolean;
3334
shortname?: boolean;
3435
};

plugins/removeAttrs.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,11 @@ export const fn = (root, params) => {
122122
if (list[0].test(node.name)) {
123123
// loop attributes
124124
for (const [name, value] of Object.entries(node.attributes)) {
125+
const isCurrentColor = value.toLowerCase() === 'currentcolor';
125126
const isFillCurrentColor =
126-
preserveCurrentColor &&
127-
name == 'fill' &&
128-
value == 'currentColor';
127+
preserveCurrentColor && name == 'fill' && isCurrentColor;
129128
const isStrokeCurrentColor =
130-
preserveCurrentColor &&
131-
name == 'stroke' &&
132-
value == 'currentColor';
129+
preserveCurrentColor && name == 'stroke' && isCurrentColor;
133130
if (
134131
!isFillCurrentColor &&
135132
!isStrokeCurrentColor &&

test/coa/testSvg/test.1.svg

Lines changed: 1 addition & 1 deletion
Loading

test/coa/testSvg/test.svg

Lines changed: 1 addition & 1 deletion
Loading

test/plugins/convertColors.01.svg.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<g color="#000"/>
1313
<g color="#000"/>
1414
<path fill="#404040"/>
15-
<path fill="#DCDDDE"/>
16-
<path fill="#0064FF"/>
15+
<path fill="#dcddde"/>
16+
<path fill="#0064ff"/>
1717
</svg>

test/plugins/convertColors.04.svg.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
@@@
1212

1313
<svg xmlns="http://www.w3.org/2000/svg">
14-
<g color="currentColor"/>
15-
<g color="currentColor"/>
14+
<g color="currentcolor"/>
15+
<g color="currentcolor"/>
1616
<g color="none"/>
17-
<path fill="currentColor"/>
18-
<path fill="currentColor"/>
19-
<path fill="currentColor"/>
17+
<path fill="currentcolor"/>
18+
<path fill="currentcolor"/>
19+
<path fill="currentcolor"/>
2020
<path fill="none"/>
2121
</svg>
2222

2323
@@@
2424

25-
{ "currentColor": true }
25+
{ "currentColor": true }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Do not touch the casing of URL references in color attributes.
2+
3+
===
4+
5+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 150">
6+
<linearGradient id="Aa">
7+
<stop stop-color="ReD" offset="5%"/>
8+
</linearGradient>
9+
<text x="0" y="32" fill="gold">uwu</text>
10+
<text x="0" y="64" fill="GOLD">owo</text>
11+
<text x="0" y="96" fill="url(#Aa)">eue</text>
12+
</svg>
13+
14+
@@@
15+
16+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 150">
17+
<linearGradient id="Aa">
18+
<stop stop-color="red" offset="5%"/>
19+
</linearGradient>
20+
<text x="0" y="32" fill="gold">uwu</text>
21+
<text x="0" y="64" fill="gold">owo</text>
22+
<text x="0" y="96" fill="url(#Aa)">eue</text>
23+
</svg>

0 commit comments

Comments
 (0)