Skip to content

Commit 787ddc4

Browse files
Merge pull request #2722 from johanrd/refactor/presentational-children-from-aria-query
refactor(template-require-presentational-children): source role list from aria-query
2 parents 8ec2dc9 + ec16ce4 commit 787ddc4

2 files changed

Lines changed: 64 additions & 18 deletions

File tree

lib/rules/template-require-presentational-children.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
// Roles that require all descendants to be presentational
2-
// https://w3c.github.io/aria-practices/#children_presentational
3-
const ROLES_REQUIRING_PRESENTATIONAL_CHILDREN = new Set([
4-
'button',
5-
'checkbox',
6-
'img',
7-
'meter',
8-
'menuitemcheckbox',
9-
'menuitemradio',
10-
'option',
11-
'progressbar',
12-
'radio',
13-
'scrollbar',
14-
'separator',
15-
'slider',
16-
'switch',
17-
'tab',
18-
]);
1+
const { roles } = require('aria-query');
2+
3+
// Roles where descendants are presentational per the ARIA "Children
4+
// Presentational" rule. Derived from aria-query (role.childrenPresentational).
5+
// https://www.w3.org/TR/wai-aria-1.2/#childrenArePresentational
6+
const ROLES_REQUIRING_PRESENTATIONAL_CHILDREN = new Set(
7+
[...roles.keys()].filter((role) => roles.get(role).childrenPresentational)
8+
);
199

2010
// Tags that do not have semantic meaning
2111
const NON_SEMANTIC_TAGS = new Set([

tests/lib/rules/template-require-presentational-children.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ ruleTester.run('template-require-presentational-children', rule, {
3535
<title>Title here</title>
3636
<circle cx="10" cy="10" r="10"></circle>
3737
</svg></template>`,
38+
// SKIPPED_TAGS: <svg> is always skipped, even when its role has
39+
// childrenPresentational (e.g. graphics-symbol via Graphics-ARIA).
40+
`<template>
41+
<svg role="graphics-symbol">
42+
<circle cx="10" cy="10" r="10"></circle>
43+
</svg></template>`,
3844
`<template>
3945
<MyButton role="tab">
4046
<:default>Button text</:default>
@@ -77,6 +83,28 @@ ruleTester.run('template-require-presentational-children', rule, {
7783
},
7884
],
7985
},
86+
// doc-pagebreak: DPUB-ARIA role with childrenPresentational.
87+
{
88+
code: '<template><div role="doc-pagebreak"><h2>pg</h2></div></template>',
89+
output: null,
90+
errors: [
91+
{
92+
message:
93+
'<div> has a role of doc-pagebreak, it cannot have semantic descendants like <h2>',
94+
},
95+
],
96+
},
97+
// graphics-symbol: Graphics-ARIA role with childrenPresentational; flags on non-svg host.
98+
{
99+
code: '<template><div role="graphics-symbol"><text>X</text></div></template>',
100+
output: null,
101+
errors: [
102+
{
103+
message:
104+
'<div> has a role of graphics-symbol, it cannot have semantic descendants like <text>',
105+
},
106+
],
107+
},
80108
],
81109
});
82110

@@ -109,6 +137,12 @@ hbsRuleTester.run('template-require-presentational-children', rule, {
109137
<title>Title here</title>
110138
<circle cx="10" cy="10" r="10"></circle>
111139
</svg>`,
140+
// SKIPPED_TAGS: <svg> is always skipped, even when its role has
141+
// childrenPresentational (e.g. graphics-symbol via Graphics-ARIA).
142+
`
143+
<svg role="graphics-symbol">
144+
<circle cx="10" cy="10" r="10"></circle>
145+
</svg>`,
112146
`
113147
<MyButton role="tab">
114148
<:default>Button text</:default>
@@ -150,5 +184,27 @@ hbsRuleTester.run('template-require-presentational-children', rule, {
150184
},
151185
],
152186
},
187+
// doc-pagebreak: DPUB-ARIA role with childrenPresentational.
188+
{
189+
code: '<div role="doc-pagebreak"><h2>pg</h2></div>',
190+
output: null,
191+
errors: [
192+
{
193+
message:
194+
'<div> has a role of doc-pagebreak, it cannot have semantic descendants like <h2>',
195+
},
196+
],
197+
},
198+
// graphics-symbol: Graphics-ARIA role with childrenPresentational; flags on non-svg host.
199+
{
200+
code: '<div role="graphics-symbol"><text>X</text></div>',
201+
output: null,
202+
errors: [
203+
{
204+
message:
205+
'<div> has a role of graphics-symbol, it cannot have semantic descendants like <text>',
206+
},
207+
],
208+
},
153209
],
154210
});

0 commit comments

Comments
 (0)