Skip to content

Commit 07242f9

Browse files
authored
a11y: do not warn about <a name> / <a id> (#4739)
1 parent 4ac2ea3 commit 07242f9

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/compiler/compile/nodes/Element.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,22 +423,29 @@ export default class Element extends Node {
423423

424424
// handle special cases
425425
if (this.name === 'a') {
426-
const attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
427-
428-
if (attribute) {
429-
const value = attribute.get_static_value();
430-
431-
if (value === '' || value === '#' || /^\W*javascript:/i.test(value)) {
432-
component.warn(attribute, {
426+
const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
427+
const id_attribute = attribute_map.get('id');
428+
const name_attribute = attribute_map.get('name');
429+
430+
if (href_attribute) {
431+
const href_value = href_attribute.get_static_value();
432+
433+
if (href_value === '' || href_value === '#' || /^\W*javascript:/i.test(href_value)) {
434+
component.warn(href_attribute, {
433435
code: `a11y-invalid-attribute`,
434-
message: `A11y: '${value}' is not a valid ${attribute.name} attribute`
436+
message: `A11y: '${href_value}' is not a valid ${href_attribute.name} attribute`
435437
});
436438
}
437439
} else {
438-
component.warn(this, {
439-
code: `a11y-missing-attribute`,
440-
message: `A11y: <a> element should have an href attribute`
441-
});
440+
const id_attribute_valid = id_attribute && id_attribute.get_static_value() !== '';
441+
const name_attribute_valid = name_attribute && name_attribute.get_static_value() !== '';
442+
443+
if (!id_attribute_valid && !name_attribute_valid) {
444+
component.warn(this, {
445+
code: `a11y-missing-attribute`,
446+
message: `A11y: <a> element should have an href attribute`
447+
});
448+
}
442449
}
443450
}
444451

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<a>not actually a link</a>
22
<a href=''>invalid</a>
33
<a href='#'>invalid</a>
4-
<a href="javascript:void(0)">invalid</a>
4+
<a href='javascript:void(0)'>invalid</a>
5+
<a name=''>invalid</a>
6+
<a id=''>invalid</a>
7+
<a name='fragment'>valid</a>
8+
<a id='fragment'>valid</a>

test/validator/samples/a11y-anchor-is-valid/warnings.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,35 @@
5858
"character": 102
5959
},
6060
"pos": 77
61+
},
62+
{
63+
"code": "a11y-missing-attribute",
64+
"message": "A11y: <a> element should have an href attribute",
65+
"start": {
66+
"line": 5,
67+
"column": 0,
68+
"character": 115
69+
},
70+
"end": {
71+
"line": 5,
72+
"column": 22,
73+
"character": 137
74+
},
75+
"pos": 115
76+
},
77+
{
78+
"code": "a11y-missing-attribute",
79+
"message": "A11y: <a> element should have an href attribute",
80+
"start": {
81+
"line": 6,
82+
"column": 0,
83+
"character": 138
84+
},
85+
"end": {
86+
"line": 6,
87+
"column": 20,
88+
"character": 158
89+
},
90+
"pos": 138
6191
}
6292
]

0 commit comments

Comments
 (0)