-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtemplate-require-iframe-title.js
More file actions
195 lines (185 loc) · 7.08 KB
/
template-require-iframe-title.js
File metadata and controls
195 lines (185 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Non-string literal AST nodes (boolean/null/undefined/number) don't represent
// a meaningful author-provided title. Even though they would coerce to strings
// at runtime (e.g. `true` → "true", `42` → "42"), those strings do not describe
// the frame's content — the rule rejects the literal forms.
const INVALID_LITERAL_TYPES = new Set([
'GlimmerBooleanLiteral',
'GlimmerNullLiteral',
'GlimmerUndefinedLiteral',
'GlimmerNumberLiteral',
]);
function isInvalidTitleLiteralPath(path) {
return INVALID_LITERAL_TYPES.has(path?.type);
}
function getInvalidLiteralType(path) {
if (!path) {
return undefined;
}
switch (path.type) {
case 'GlimmerBooleanLiteral': {
return 'boolean';
}
case 'GlimmerNullLiteral': {
return 'null';
}
case 'GlimmerUndefinedLiteral': {
return 'undefined';
}
case 'GlimmerNumberLiteral': {
return 'number';
}
default: {
return undefined;
}
}
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require iframe elements to have a title attribute',
category: 'Accessibility',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-iframe-title.md',
templateMode: 'both',
},
schema: [
{
type: 'object',
properties: {
allowWhitespaceOnlyTitle: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
// Four messageIds (missingTitle, emptyTitle, invalidTitleLiteral,
// duplicateTitle) for richer diagnostic detail.
missingTitle: '<iframe> elements must have a unique title property.',
emptyTitle: '<iframe> elements must have a unique title property.',
invalidTitleLiteral:
'<iframe title> must be a non-empty string. Got a {{literalType}} literal, which does not describe the frame contents.',
duplicateTitleFirst: 'This title is not unique. #{{index}}',
duplicateTitleOther:
'<iframe> elements must have a unique title property. Value title="{{title}}" already used for different iframe. #{{index}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/require-iframe-title.js',
docs: 'docs/rule/require-iframe-title.md',
tests: 'test/unit/rules/require-iframe-title-test.js',
},
},
create(context) {
// Whitespace-only `title=" "` is technically spec-compliant: ACCNAME
// 1.2 step 2I (Tooltip) does not whitespace-trim like step 2D
// (aria-label) does, so a 3-space accessible name is assigned. That is
// useless in practice but not a spec violation. Default behavior flags
// it as authoring hygiene; set `allowWhitespaceOnlyTitle: true` to
// align with spec/peer behavior.
const allowWhitespaceOnlyTitle = Boolean(context.options[0]?.allowWhitespaceOnlyTitle);
// Each entry: { value, node, index }
// - value: trimmed title string
// - node: original element node for the first occurrence
// - index: duplicate-group index (1-based), assigned lazily on collision
const knownTitles = [];
let nextDuplicateIndex = 1;
return {
GlimmerElementNode(node) {
if (node.tag !== 'iframe') {
return;
}
// Skip if aria-hidden or hidden
const hasAriaHidden = node.attributes?.some((a) => a.name === 'aria-hidden');
const hasHidden = node.attributes?.some((a) => a.name === 'hidden');
if (hasAriaHidden || hasHidden) {
return;
}
// Check for title attribute
const titleAttr = node.attributes?.find((a) => a.name === 'title');
if (!titleAttr) {
context.report({ node, messageId: 'missingTitle' });
return;
}
if (titleAttr.value) {
switch (titleAttr.value.type) {
case 'GlimmerTextNode': {
const raw = titleAttr.value.chars;
const value = raw.trim();
if (value.length === 0) {
// Empty-string title always fails: no accessible name for screen readers.
// Whitespace-only titles are controlled by the `allowWhitespaceOnlyTitle`
// option (default false).
if (raw.length === 0 || !allowWhitespaceOnlyTitle) {
context.report({ node, messageId: 'emptyTitle' });
}
} else {
// Check for duplicate titles. Reports BOTH the first and the
// current occurrence on every collision, sharing a `#N` index
// so users can correlate them. For three or more duplicates
// the first occurrence is therefore re-reported once per
// collision.
const existing = knownTitles.find((entry) => entry.value === value);
if (existing) {
if (existing.index === null) {
existing.index = nextDuplicateIndex++;
}
const index = existing.index;
// Report on the first occurrence on every collision.
context.report({
node: existing.node,
messageId: 'duplicateTitleFirst',
data: { index: String(index) },
});
// Report on the current (duplicate) occurrence.
context.report({
node,
messageId: 'duplicateTitleOther',
data: { title: titleAttr.value.chars, index: String(index) },
});
} else {
knownTitles.push({ value, node, index: null });
}
}
break;
}
case 'GlimmerMustacheStatement': {
// title={{false}} / title={{null}} / title={{undefined}} / title={{42}}
// — any literal that doesn't produce a meaningful accessible name.
if (isInvalidTitleLiteralPath(titleAttr.value.path)) {
context.report({
node,
messageId: 'invalidTitleLiteral',
data: { literalType: getInvalidLiteralType(titleAttr.value.path) },
});
}
break;
}
case 'GlimmerConcatStatement': {
// title="{{false}}" / "{{undefined}}" / etc. — ConcatStatement
// with a single literal part that doesn't produce a name.
const parts = titleAttr.value.parts || [];
if (
parts.length === 1 &&
parts[0].type === 'GlimmerMustacheStatement' &&
isInvalidTitleLiteralPath(parts[0].path)
) {
context.report({
node,
messageId: 'invalidTitleLiteral',
data: { literalType: getInvalidLiteralType(parts[0].path) },
});
}
break;
}
default: {
break;
}
}
}
},
};
},
};