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
89 lines (85 loc) · 2.93 KB
/
template-require-iframe-title.js
File metadata and controls
89 lines (85 loc) · 2.93 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
/** @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: {
missingTitle: '<iframe> elements must have a unique title property.',
},
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) {
const knownTitles = [];
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: 'missingTitle' });
} else {
// Check for duplicate titles
const existingIdx = knownTitles.findIndex(([val]) => val === value);
if (existingIdx === -1) {
knownTitles.push([value, node]);
} else {
context.report({ node, messageId: 'missingTitle' });
}
}
break;
}
case 'GlimmerMustacheStatement': {
// title={{false}} → BooleanLiteral false is invalid
if (titleAttr.value.path?.type === 'GlimmerBooleanLiteral') {
context.report({ node, messageId: 'missingTitle' });
}
break;
}
case 'GlimmerConcatStatement': {
// title="{{false}}" → ConcatStatement with single BooleanLiteral part
const parts = titleAttr.value.parts || [];
if (
parts.length === 1 &&
parts[0].type === 'GlimmerMustacheStatement' &&
parts[0].path?.type === 'GlimmerBooleanLiteral'
) {
context.report({ node, messageId: 'missingTitle' });
}
break;
}
default: {
break;
}
}
}
},
};
},
};