forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-require-iframe-title.js
More file actions
143 lines (136 loc) · 5.3 KB
/
template-require-iframe-title.js
File metadata and controls
143 lines (136 loc) · 5.3 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
// Mustache path nodes that produce no accessible name. Booleans, null, undefined
// all coerce to empty-ish strings; numeric literals ("42") are accepted by HTML
// but provide no useful title for assistive tech.
function isInvalidTitleLiteral(path) {
if (!path) {
return false;
}
if (path.type === 'GlimmerBooleanLiteral') {
return true;
}
if (path.type === 'GlimmerNullLiteral' || path.type === 'GlimmerUndefinedLiteral') {
return true;
}
if (path.type === 'GlimmerNumberLiteral') {
return true;
}
return false;
}
/** @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: [],
messages: {
// Four messageIds (missingTitle, emptyTitle, dynamicFalseTitle,
// duplicateTitle) for richer diagnostic detail.
missingTitle: '<iframe> elements must have a unique title property.',
emptyTitle: '<iframe> elements must have a unique title property.',
dynamicFalseTitle: '<iframe> elements must have a unique title property.',
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) {
// 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 value = titleAttr.value.chars.trim();
if (value.length === 0) {
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 (isInvalidTitleLiteral(titleAttr.value.path)) {
context.report({ node, messageId: 'dynamicFalseTitle' });
}
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' &&
isInvalidTitleLiteral(parts[0].path)
) {
context.report({ node, messageId: 'dynamicFalseTitle' });
}
break;
}
default: {
break;
}
}
}
},
};
},
};