Skip to content

Commit

Permalink
Allow xlink:href attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
emilos committed Jan 5, 2018
1 parent 6b956fb commit 5659d10
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/validate/html/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,21 @@ export default function a11y(
}
}

function shouldHaveValidHref (attribute) {
const href = attributeMap.get(attribute);
const value = getStaticAttributeValue(node, attribute);
if (value === '' || value === '#') {
validator.warn(`A11y: '${value}' is not a valid ${attribute} attribute`, href.start);
}
}

if (node.name === 'a') {
// anchor-is-valid
const href = attributeMap.get('href');
if (attributeMap.has('href')) {
const value = getStaticAttributeValue(node, 'href');
if (value === '' || value === '#') {
validator.warn(`A11y: '${value}' is not a valid href attribute`, href.start);
}
// anchor-is-valid
shouldHaveValidHref('href')
} else if (attributeMap.has('xlink:href')) {
// anchor-in-svg-is-valid
shouldHaveValidHref('xlink:href')
} else {
validator.warn(`A11y: <a> element should have an href attribute`, node.start);
}
Expand Down
3 changes: 3 additions & 0 deletions test/validator/samples/a11y-anchor-in-svg-is-valid/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg><text><a>not actually a link</a></text></svg>
<svg><text><a xlink:href=''>not actually a link</a></text></svg>
<svg><text><a xlink:href='#'>not actually a link</a></text></svg>
26 changes: 26 additions & 0 deletions test/validator/samples/a11y-anchor-in-svg-is-valid/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"message": "A11y: <a> element should have an href attribute",
"loc": {
"line": 1,
"column": 11
},
"pos": 11
},
{
"message": "A11y: '' is not a valid xlink:href attribute",
"loc": {
"line": 2,
"column": 14
},
"pos": 65
},
{
"message": "A11y: '#' is not a valid xlink:href attribute",
"loc": {
"line": 3,
"column": 14
},
"pos": 130
}
]

0 comments on commit 5659d10

Please sign in to comment.