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

fix(color-contrast): process non-rgb color functions #4092

Merged
merged 4 commits into from Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/commons/color/parse-text-shadows.js
Expand Up @@ -16,9 +16,8 @@ export default function parseTextShadows(textShadow) {

while (str) {
const colorMatch =
str.match(/^rgba?\([0-9,.\s]+\)/i) ||
str.match(/^[a-z]+/i) ||
str.match(/^#[0-9a-f]+/i);
// match a color name or function (e.g. `oklch(39.2% 0.4 0 / 0.5)`) or a hex value
str.match(/^[a-z]+(\([^)]+\))?/i) || str.match(/^#[0-9a-f]+/i);
WilcoFiers marked this conversation as resolved.
Show resolved Hide resolved
const pixelMatch = str.match(/^([0-9.-]+)px/i) || str.match(/^(0)/);

if (colorMatch) {
Expand Down Expand Up @@ -48,7 +47,7 @@ export default function parseTextShadows(textShadow) {
shadows.push(current);
str = str.substr(1).trim();
} else {
throw new Error(`Unable to process text-shadows: ${textShadow}`);
throw new Error(`Unable to process text-shadows: ${str}`);
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/commons/color/parse-text-shadows.js
Expand Up @@ -33,6 +33,35 @@ describe('axe.commons.color.parseTextShadows', () => {
});
});

it('accepts named colors', () => {
const shadows = parseTextShadows('red 1px 2px 3px, blue 4px 5px 6px');
assert.deepEqual(shadows[0], {
pixels: [1, 2, 3],
colorStr: 'red'
});
assert.deepEqual(shadows[1], {
pixels: [4, 5, 6],
colorStr: 'blue'
});
});

it('accepts different color functions', () => {
const shadows = parseTextShadows(`
rgb(0, 202, 148) 0px 0px,
hsl(165.58deg 100% 35.07% / .5) 0px 0px,
lch(65 49.19 167.45) 0px 0px,
oklch(60% 0.57 181) 0px 0px,
lab(65 -48.01 10.69) 0px 0px,
color(xyz-d65 0.26 0.44 0.35 / 1) 0px 0px,
`);
assert.equal(shadows[0].colorStr, 'rgb(0, 202, 148)');
assert.equal(shadows[1].colorStr, 'hsl(165.58deg 100% 35.07% / .5)');
assert.equal(shadows[2].colorStr, 'lch(65 49.19 167.45)');
assert.equal(shadows[3].colorStr, 'oklch(60% 0.57 181)');
assert.equal(shadows[4].colorStr, 'lab(65 -48.01 10.69)');
assert.equal(shadows[5].colorStr, 'color(xyz-d65 0.26 0.44 0.35 / 1)');
});

it('sets default blur to 0 when omitted', () => {
const shadows = parseTextShadows('1px 2px #000, 4px 5px #fff');
assert.deepEqual(shadows[0], {
Expand Down
11 changes: 11 additions & 0 deletions test/integration/rules/color-contrast/color-contrast.html
Expand Up @@ -448,3 +448,14 @@
<div id="ignore12" style="background: #000; color: #333">hello</div>
</div>
</div>

<div
id="pass23"
style="
color: #000;
background: #737373;
text-shadow: color(display-p3 1 1 1 / 1) 0 0 3px;
"
>
Hello world
</div>
3 changes: 2 additions & 1 deletion test/integration/rules/color-contrast/color-contrast.json
Expand Up @@ -36,7 +36,8 @@
["#pass19"],
["#pass20"],
["#pass21"],
["#pass22"]
["#pass22"],
["#pass23"]
],
"incomplete": [
["#canttell1"],
Expand Down