diff --git a/src/validate/html/a11y.ts b/src/validate/html/a11y.ts
index 70beea0ac3..359b575670 100644
--- a/src/validate/html/a11y.ts
+++ b/src/validate/html/a11y.ts
@@ -21,10 +21,18 @@ export default function a11y(
});
if (node.name === 'a') {
- if (!attributeMap.has('href')) {
+ // anchor-is-valid
+ const href = attributeMap.get('href');
+ if (href) {
+ const value = getValue(href);
+ if (value === '' || value === '#') {
+ validator.warn(`A11y: '${value}' is not a valid href attribute`, href.start);
+ }
+ } else {
validator.warn(`A11y: element should have an href attribute`, node.start);
}
+ // anchor-has-content
if (!node.children.length) {
validator.warn(`A11y: element should have child content`, node.start);
}
@@ -47,4 +55,11 @@ export default function a11y(
}
}
}
+}
+
+function getValue(attribute: Node) {
+ if (attribute.value.length === 0) return '';
+ if (attribute.value.length === 1 && attribute.value[0].type === 'Text') return attribute.value[0].data;
+
+ return null;
}
\ No newline at end of file
diff --git a/test/validator/samples/a11y-a-without-href/input.html b/test/validator/samples/a11y-a-without-href/input.html
deleted file mode 100644
index f7ebe13def..0000000000
--- a/test/validator/samples/a11y-a-without-href/input.html
+++ /dev/null
@@ -1 +0,0 @@
-not actually a link
\ No newline at end of file
diff --git a/test/validator/samples/a11y-a-without-href/warnings.json b/test/validator/samples/a11y-a-without-href/warnings.json
deleted file mode 100644
index bf5f650ba5..0000000000
--- a/test/validator/samples/a11y-a-without-href/warnings.json
+++ /dev/null
@@ -1,8 +0,0 @@
-[{
- "message": "A11y: element should have an href attribute",
- "loc": {
- "line": 1,
- "column": 0
- },
- "pos": 0
-}]
\ No newline at end of file
diff --git a/test/validator/samples/a11y-anchor-is-valid/input.html b/test/validator/samples/a11y-anchor-is-valid/input.html
new file mode 100644
index 0000000000..7b14a80c9f
--- /dev/null
+++ b/test/validator/samples/a11y-anchor-is-valid/input.html
@@ -0,0 +1,3 @@
+not actually a link
+invalid
+invalid
\ 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
new file mode 100644
index 0000000000..c63418f1bf
--- /dev/null
+++ b/test/validator/samples/a11y-anchor-is-valid/warnings.json
@@ -0,0 +1,26 @@
+[
+ {
+ "message": "A11y: element should have an href attribute",
+ "loc": {
+ "line": 1,
+ "column": 0
+ },
+ "pos": 0
+ },
+ {
+ "message": "A11y: '' is not a valid href attribute",
+ "loc": {
+ "line": 2,
+ "column": 3
+ },
+ "pos": 30
+ },
+ {
+ "message": "A11y: '#' is not a valid href attribute",
+ "loc": {
+ "line": 3,
+ "column": 3
+ },
+ "pos": 53
+ }
+]