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:
parent
bacbaef868
commit
6133976fec
@ -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;
|
||||
}
|
@ -1 +0,0 @@
|
||||
<a>not actually a link</a>
|
@ -1,8 +0,0 @@
|
||||
[{
|
||||
"message": "A11y: <a> element should have an href attribute",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"pos": 0
|
||||
}]
|
3
test/validator/samples/a11y-anchor-is-valid/input.html
Normal file
3
test/validator/samples/a11y-anchor-is-valid/input.html
Normal file
@ -0,0 +1,3 @@
|
||||
<a>not actually a link</a>
|
||||
<a href=''>invalid</a>
|
||||
<a href='#'>invalid</a>
|
26
test/validator/samples/a11y-anchor-is-valid/warnings.json
Normal file
26
test/validator/samples/a11y-anchor-is-valid/warnings.json
Normal 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
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue
Block a user