0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

anchor-is-valid

This commit is contained in:
Rich Harris 2017-09-03 15:01:15 -04:00
parent bacbaef868
commit 6133976fec
5 changed files with 45 additions and 10 deletions

View File

@ -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: <a> element should have an href attribute`, node.start);
}
// anchor-has-content
if (!node.children.length) {
validator.warn(`A11y: <a> 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;
}

View File

@ -1 +0,0 @@
<a>not actually a link</a>

View File

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

View File

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

View File

@ -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
}
]