From b657a112ee4279e1d631fa98038f73a5bac0039f Mon Sep 17 00:00:00 2001 From: Takeshi Kurosawa Date: Mon, 5 Feb 2024 16:05:49 +0900 Subject: [PATCH 1/3] feat(aria-prohibited-attr): add support fallback role --- .../aria/aria-prohibited-attr-evaluate.js | 8 +++- test/checks/aria/aria-prohibited-attr.js | 33 +++++++++++++ .../virtual-rules/aria-prohibited-attr.js | 48 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/lib/checks/aria/aria-prohibited-attr-evaluate.js b/lib/checks/aria/aria-prohibited-attr-evaluate.js index e6e2f9da7d..03bbc0cb14 100644 --- a/lib/checks/aria/aria-prohibited-attr-evaluate.js +++ b/lib/checks/aria/aria-prohibited-attr-evaluate.js @@ -33,7 +33,11 @@ export default function ariaProhibitedAttrEvaluate( ) { const elementsAllowedAriaLabel = options?.elementsAllowedAriaLabel || []; const { nodeName } = virtualNode.props; - const role = getRole(virtualNode, { chromium: true }); + const role = getRole(virtualNode, { + chromium: true, + // this check allows fallback roles. For example, `
` is legal. + fallback: true + }); const prohibitedList = listProhibitedAttrs( role, @@ -51,7 +55,7 @@ export default function ariaProhibitedAttrEvaluate( return false; } - let messageKey = virtualNode.hasAttr('role') ? 'hasRole' : 'noRole'; + let messageKey = role !== null ? 'hasRole' : 'noRole'; messageKey += prohibited.length > 1 ? 'Plural' : 'Singular'; this.data({ role, nodeName, messageKey, prohibited }); diff --git a/test/checks/aria/aria-prohibited-attr.js b/test/checks/aria/aria-prohibited-attr.js index 4ae70f6bb0..af9558a7ac 100644 --- a/test/checks/aria/aria-prohibited-attr.js +++ b/test/checks/aria/aria-prohibited-attr.js @@ -138,4 +138,37 @@ describe('aria-prohibited-attr', function () { var params = checkSetup(''); assert.isFalse(checkEvaluate.apply(checkContext, params)); }); + + it('should not allow elements that have an invalid role', function () { + var params = checkSetup( + '
' + ); + assert.isTrue(checkEvaluate.apply(checkContext, params)); + assert.deepEqual(checkContext._data, { + nodeName: 'div', + role: null, + messageKey: 'noRoleSingular', + prohibited: ['aria-label'] + }); + }); + + it('should allow elements that have fallback roles', function () { + var params = checkSetup( + '
' + ); + assert.isFalse(checkEvaluate.apply(checkContext, params)); + }); + + it('should not allow elements that have multiple invalid roles', function () { + var params = checkSetup( + '
' + ); + assert.isTrue(checkEvaluate.apply(checkContext, params)); + assert.deepEqual(checkContext._data, { + nodeName: 'div', + role: null, + messageKey: 'noRoleSingular', + prohibited: ['aria-label'] + }); + }); }); diff --git a/test/integration/virtual-rules/aria-prohibited-attr.js b/test/integration/virtual-rules/aria-prohibited-attr.js index ccacf57b33..4f7fa95891 100644 --- a/test/integration/virtual-rules/aria-prohibited-attr.js +++ b/test/integration/virtual-rules/aria-prohibited-attr.js @@ -58,4 +58,52 @@ describe('aria-prohibited-attr virtual-rule', () => { assert.lengthOf(results.violations, 1); assert.lengthOf(results.incomplete, 0); }); + + it('should fail for invalid role', () => { + const vNode = new axe.SerialVirtualNode({ + nodeName: 'div', + attributes: { + role: 'foo', + 'aria-label': 'foo' + } + }); + vNode.children = []; + + const results = axe.runVirtualRule('aria-prohibited-attr', vNode); + + assert.lengthOf(results.passes, 0); + assert.lengthOf(results.violations, 1); + assert.lengthOf(results.incomplete, 0); + }); + + it('should pass for fallback roles', () => { + const results = axe.runVirtualRule('aria-prohibited-attr', { + nodeName: 'div', + attributes: { + role: 'foo dialog', + 'aria-label': 'foo' + } + }); + + assert.lengthOf(results.passes, 1); + assert.lengthOf(results.violations, 0); + assert.lengthOf(results.incomplete, 0); + }); + + it('should fail for multiple invalid roles', () => { + const vNode = new axe.SerialVirtualNode({ + nodeName: 'div', + attributes: { + role: 'foo bar', + 'aria-label': 'foo' + } + }); + vNode.children = []; + + const results = axe.runVirtualRule('aria-prohibited-attr', vNode); + + assert.lengthOf(results.passes, 0); + assert.lengthOf(results.violations, 1); + assert.lengthOf(results.incomplete, 0); + }); }); From 56273c8e6d9f5274c56e1a97f3f83c4070f0b1c7 Mon Sep 17 00:00:00 2001 From: Takeshi Kurosawa Date: Tue, 5 Mar 2024 19:55:27 +0900 Subject: [PATCH 2/3] chore(aria-prohibited-attr): fix English grammer Co-authored-by: Wilco Fiers --- test/checks/aria/aria-prohibited-attr.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/checks/aria/aria-prohibited-attr.js b/test/checks/aria/aria-prohibited-attr.js index af9558a7ac..116f31fc0d 100644 --- a/test/checks/aria/aria-prohibited-attr.js +++ b/test/checks/aria/aria-prohibited-attr.js @@ -139,7 +139,7 @@ describe('aria-prohibited-attr', function () { assert.isFalse(checkEvaluate.apply(checkContext, params)); }); - it('should not allow elements that have an invalid role', function () { + it('should not allow aria-label on divs that have an invalid role', function () { var params = checkSetup( '
' ); @@ -152,14 +152,14 @@ describe('aria-prohibited-attr', function () { }); }); - it('should allow elements that have fallback roles', function () { + it('should allow aria-label on divs with a valid fallback role', function () { var params = checkSetup( '
' ); assert.isFalse(checkEvaluate.apply(checkContext, params)); }); - it('should not allow elements that have multiple invalid roles', function () { + it('should not allow aria-label on divs with no valid fallback roles', function () { var params = checkSetup( '
' ); From b405042a5b58e6a2b88192538d6119678c44bf7e Mon Sep 17 00:00:00 2001 From: Takeshi Kurosawa Date: Tue, 5 Mar 2024 20:13:19 +0900 Subject: [PATCH 3/3] test(aria-prohibited-attr): add integration/rules tests --- .../aria-prohibited-attr/aria-prohibited-attr.html | 3 +++ .../aria-prohibited-attr/aria-prohibited-attr.json | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.html b/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.html index ec354260a3..3be7f40ea3 100644 --- a/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.html +++ b/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.html @@ -3,6 +3,7 @@
Foo
+
@@ -35,6 +36,8 @@
+
+
Foo
Foo
diff --git a/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.json b/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.json index f6f9e7717b..9ac7375cbe 100644 --- a/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.json +++ b/test/integration/rules/aria-prohibited-attr/aria-prohibited-attr.json @@ -1,7 +1,14 @@ { "description": "aria-prohibited-attr tests", "rule": "aria-prohibited-attr", - "passes": [["#pass1"], ["#pass2"], ["#pass3"], ["#pass4"], ["#pass5"]], + "passes": [ + ["#pass1"], + ["#pass2"], + ["#pass3"], + ["#pass4"], + ["#pass5"], + ["#pass6"] + ], "incomplete": [["#incomplete1"], ["#incomplete2"], ["#incomplete3"]], "violations": [ ["#fail1"], @@ -32,6 +39,8 @@ ["#fail26"], ["#fail27"], ["#fail28"], - ["#fail29"] + ["#fail29"], + ["#fail30"], + ["#fail31"] ] }