anchor-is-valid

pull/815/head
Rich Harris 7 years ago
parent bacbaef868
commit 6133976fec

@ -21,10 +21,18 @@ export default function a11y(
}); });
if (node.name === 'a') { 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: <a> element should have an href attribute`, node.start); validator.warn(`A11y: <a> element should have an href attribute`, node.start);
} }
// anchor-has-content
if (!node.children.length) { if (!node.children.length) {
validator.warn(`A11y: <a> element should have child content`, node.start); validator.warn(`A11y: <a> element should have child content`, node.start);
} }
@ -48,3 +56,10 @@ 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;
}

@ -1,8 +0,0 @@
[{
"message": "A11y: <a> element should have an href attribute",
"loc": {
"line": 1,
"column": 0
},
"pos": 0
}]

@ -0,0 +1,3 @@
<a>not actually a link</a>
<a href=''>invalid</a>
<a href='#'>invalid</a>

@ -0,0 +1,26 @@
[
{
"message": "A11y: <a> 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
}
]
Loading…
Cancel
Save