Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convertColors plugin ignore currentColor in 'mask' elements #2032

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion plugins/convertColors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { colorsNames, colorsProps, colorsShortNames } from './_collections.js';
import { includesUrlReference } from '../lib/svgo/tools.js';

/**
* @typedef {import('../lib/types.js').XastNode} XastNode
SethFalco marked this conversation as resolved.
Show resolved Hide resolved
*/

export const name = 'convertColors';
export const description =
'converts colors: rgb() to #rrggbb and #rrggbb to #rgb';
Expand Down Expand Up @@ -39,6 +43,12 @@ const convertRgbToHex = ([r, g, b]) => {
return '#' + hexNumber.toString(16).slice(1).toUpperCase();
};

/**
* @type {(node: XastNode) => boolean}
*/
const isMask = (node) =>
node && node.type === 'element' && node.name === 'mask';
salvan13 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Convert different colors formats in element attributes to hex.
*
Expand Down Expand Up @@ -73,15 +83,20 @@ export const fn = (_root, params) => {
shortname = true,
} = params;

let maskCounter = 0;

return {
element: {
enter: (node) => {
if (isMask(node)) {
maskCounter++;
}
for (const [name, value] of Object.entries(node.attributes)) {
if (colorsProps.has(name)) {
let val = value;

// convert colors to currentColor
if (currentColor) {
if (currentColor && maskCounter === 0) {
let matched;
if (typeof currentColor === 'string') {
matched = val === currentColor;
Expand Down Expand Up @@ -148,6 +163,11 @@ export const fn = (_root, params) => {
}
}
},
exit: (node) => {
if (isMask(node)) {
maskCounter--;
}
},
},
};
};
41 changes: 41 additions & 0 deletions test/plugins/convertColors.06.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Do not apply currentColor to masks.

===

<svg xmlns="http://www.w3.org/2000/svg">
<path fill="white"/>
<mask id="mask1" fill="white" />
<mask id="mask2">
<path fill="rgba(255,255,255,0.75)"/>
</mask>
<mask id="mask3">
<g>
<path fill="white"/>
<path stroke="black"/>
</g>
<mask id="inner-mask" fill="rgba(0,0,0,.5)"></mask>
</mask>
<path fill="red"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg">
<path fill="currentcolor"/>
<mask id="mask1" fill="#fff"/>
<mask id="mask2">
<path fill="rgba(255,255,255,0.75)"/>
</mask>
<mask id="mask3">
<g>
<path fill="#fff"/>
<path stroke="#000"/>
</g>
<mask id="inner-mask" fill="rgba(0,0,0,.5)"/>
</mask>
<path fill="currentcolor"/>
</svg>

@@@

{ "currentColor": true }