Skip to content

Commit

Permalink
Add filtering of punctuation-only texts in applicability of R69
Browse files Browse the repository at this point in the history
  • Loading branch information
rcj-siteimprove committed Jan 16, 2024
1 parent 698761c commit 11f6aa3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/alfa-rules/src/sia-r69/rule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rule } from "@siteimprove/alfa-act";
import { Element, Text } from "@siteimprove/alfa-dom";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Criterion } from "@siteimprove/alfa-wcag";
import { Page } from "@siteimprove/alfa-web";

Expand All @@ -11,6 +12,8 @@ import { hasSufficientContrast } from "../common/expectation/contrast";

import { Scope, Stability, Version } from "../tags";

const { not } = Predicate;

export default Rule.Atomic.of<
Page,
Text,
Expand All @@ -23,7 +26,9 @@ export default Rule.Atomic.of<
evaluate({ device, document }) {
return {
applicability() {
return nonDisabledTexts(document, device);
return nonDisabledTexts(document, device).filter(
not(isOnlyPunctuation),
);
},

expectations(target) {
Expand All @@ -32,3 +37,7 @@ export default Rule.Atomic.of<
};
},
});

function isOnlyPunctuation(text: Text): boolean {
return text.data.replace(/\p{P}|\p{S}|\p{Cf}/gu, "").length === 0;
}
34 changes: 34 additions & 0 deletions packages/alfa-rules/test/sia-r69/rule.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,40 @@ test(`evaluate() is inapplicable to the text that is part of a label of a disabl
t.deepEqual(await evaluate(R69, { document }), [inapplicable(R69)]);
});

test("evaluate() is inapplicable to text that would otherwise pass if it was not only punctuation", async (t) => {
const target = h.text(",./;'[]\\-=?<>:\"{}|_+!@#$%^&*()");

const document = h.document([
<html
style={{
backgroundImage: "url('foo.png')",
color: "#000",
}}
>
{target}
</html>,
]);

t.deepEqual(await evaluate(R69, { document }), [inapplicable(R69)]);
});

test("evaluate() is inapplicable to text that would otherwise fail if it was not only punctuation", async (t) => {
const target = h.text(",./;'[]\\-=?<>:\"{}|_+!@#$%^&*()");

const document = h.document([
<html
style={{
backgroundImage: "url('foo.png')",
color: "#000",
}}
>
{target}
</html>,
]);

t.deepEqual(await evaluate(R69, { document }), [inapplicable(R69)]);
});

test("evaluate() passes when a background color with sufficient contrast is input", async (t) => {
const target = h.text("Hello world");

Expand Down

0 comments on commit 11f6aa3

Please sign in to comment.