Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,29 @@ export default class Element extends Node {

// handle special cases
if (this.name === 'a') {
const attribute = attribute_map.get('href') || attribute_map.get('xlink:href');

if (attribute) {
const value = attribute.get_static_value();

if (value === '' || value === '#' || /^\W*javascript:/i.test(value)) {
component.warn(attribute, {
const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
const id_attribute = attribute_map.get('id');
const name_attribute = attribute_map.get('name');

if (href_attribute) {
const href_value = href_attribute.get_static_value();

if (href_value === '' || href_value === '#' || /^\W*javascript:/i.test(href_value)) {
component.warn(href_attribute, {
code: `a11y-invalid-attribute`,
message: `A11y: '${value}' is not a valid ${attribute.name} attribute`
message: `A11y: '${href_value}' is not a valid ${href_attribute.name} attribute`
});
}
} else {
component.warn(this, {
code: `a11y-missing-attribute`,
message: `A11y: <a> element should have an href attribute`
});
const id_attribute_valid = id_attribute && id_attribute.get_static_value() !== '';
const name_attribute_valid = name_attribute && name_attribute.get_static_value() !== '';

if (!id_attribute_valid && !name_attribute_valid) {
component.warn(this, {
code: `a11y-missing-attribute`,
message: `A11y: <a> element should have an href attribute`
});
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion test/validator/samples/a11y-anchor-is-valid/input.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<a>not actually a link</a>
<a href=''>invalid</a>
<a href='#'>invalid</a>
<a href="javascript:void(0)">invalid</a>
<a href='javascript:void(0)'>invalid</a>
<a name=''>invalid</a>
<a id=''>invalid</a>
<a name='fragment'>valid</a>
<a id='fragment'>valid</a>
30 changes: 30 additions & 0 deletions test/validator/samples/a11y-anchor-is-valid/warnings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,35 @@
"character": 102
},
"pos": 77
},
{
"code": "a11y-missing-attribute",
"message": "A11y: <a> element should have an href attribute",
"start": {
"line": 5,
"column": 0,
"character": 115
},
"end": {
"line": 5,
"column": 22,
"character": 137
},
"pos": 115
},
{
"code": "a11y-missing-attribute",
"message": "A11y: <a> element should have an href attribute",
"start": {
"line": 6,
"column": 0,
"character": 138
},
"end": {
"line": 6,
"column": 20,
"character": 158
},
"pos": 138
}
]