diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts
index f64da919c69d..f7f3cc55bc9e 100644
--- a/src/compiler/compile/nodes/Element.ts
+++ b/src/compiler/compile/nodes/Element.ts
@@ -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: 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: element should have an href attribute`
+ });
+ }
}
}
diff --git a/test/validator/samples/a11y-anchor-is-valid/input.svelte b/test/validator/samples/a11y-anchor-is-valid/input.svelte
index 6d0f77a308c2..8933bd75608f 100644
--- a/test/validator/samples/a11y-anchor-is-valid/input.svelte
+++ b/test/validator/samples/a11y-anchor-is-valid/input.svelte
@@ -1,4 +1,8 @@
not actually a link
invalid
invalid
-invalid
\ No newline at end of file
+invalid
+invalid
+invalid
+valid
+valid
\ No newline at end of file
diff --git a/test/validator/samples/a11y-anchor-is-valid/warnings.json b/test/validator/samples/a11y-anchor-is-valid/warnings.json
index 9438a74f5b6e..f04c6f1593aa 100644
--- a/test/validator/samples/a11y-anchor-is-valid/warnings.json
+++ b/test/validator/samples/a11y-anchor-is-valid/warnings.json
@@ -58,5 +58,35 @@
"character": 102
},
"pos": 77
+ },
+ {
+ "code": "a11y-missing-attribute",
+ "message": "A11y: 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: element should have an href attribute",
+ "start": {
+ "line": 6,
+ "column": 0,
+ "character": 138
+ },
+ "end": {
+ "line": 6,
+ "column": 20,
+ "character": 158
+ },
+ "pos": 138
}
]