From 5659d10d7ccde929a23a7ae22a56df59a5f850ed Mon Sep 17 00:00:00 2001 From: Emil Ajdyna Date: Fri, 5 Jan 2018 20:15:21 +0100 Subject: [PATCH] Allow xlink:href attribute --- src/validate/html/a11y.ts | 19 +++++++++----- .../a11y-anchor-in-svg-is-valid/input.html | 3 +++ .../a11y-anchor-in-svg-is-valid/warnings.json | 26 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 test/validator/samples/a11y-anchor-in-svg-is-valid/input.html create mode 100644 test/validator/samples/a11y-anchor-in-svg-is-valid/warnings.json diff --git a/src/validate/html/a11y.ts b/src/validate/html/a11y.ts index 71df223d5ec7..4dbf249fff1e 100644 --- a/src/validate/html/a11y.ts +++ b/src/validate/html/a11y.ts @@ -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: element should have an href attribute`, node.start); } diff --git a/test/validator/samples/a11y-anchor-in-svg-is-valid/input.html b/test/validator/samples/a11y-anchor-in-svg-is-valid/input.html new file mode 100644 index 000000000000..48a5599aa204 --- /dev/null +++ b/test/validator/samples/a11y-anchor-in-svg-is-valid/input.html @@ -0,0 +1,3 @@ +not actually a link +not actually a link +not actually a link diff --git a/test/validator/samples/a11y-anchor-in-svg-is-valid/warnings.json b/test/validator/samples/a11y-anchor-in-svg-is-valid/warnings.json new file mode 100644 index 000000000000..1e65bc4986e7 --- /dev/null +++ b/test/validator/samples/a11y-anchor-in-svg-is-valid/warnings.json @@ -0,0 +1,26 @@ +[ + { + "message": "A11y: 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 + } +]