Skip to content

Commit

Permalink
fix figcaption a11y warning - #991
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 10, 2017
1 parent cf94217 commit f33b883
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/validate/html/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,15 @@ export default function a11y(
if (parent.name !== 'figure') {
validator.warn(`A11y: <figcaption> must be an immediate child of <figure>`, node.start);
} else {
const index = parent.children.indexOf(node);
if (index !== 0 && index !== parent.children.length - 1) {
const children = parent.children.filter(node => {
if (node.type === 'Comment') return false;
if (node.type === 'Text') return /\S/.test(node.data);
return true;
});

const index = children.indexOf(node);

if (index !== 0 && index !== children.length - 1) {
validator.warn(`A11y: <figcaption> must be first or last child of <figure>`, node.start);
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/validator/samples/a11y-figcaption-right-place/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<figure>
<img src='foo.jpg' alt='a picture of a foo'>

<figcaption>
a foo in its natural habitat
</figcaption>
</figure>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit f33b883

Please sign in to comment.