Skip to content

Commit

Permalink
Merge pull request #2607 from EmilTholin/figcaption_figure_first_elem…
Browse files Browse the repository at this point in the history
…ent_ancestor

Check if a figcaption's first element ancestor is a figure
  • Loading branch information
Rich-Harris authored May 4, 2019
2 parents 0542825 + 6ebd72f commit 16dbb8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,21 @@ export default class Element extends Node {
}

if (this.name === 'figcaption') {
if (this.parent.name !== 'figure') {
let { parent } = this;
let is_figure_parent = false;

while (parent) {
if (parent.name === 'figure') {
is_figure_parent = true;
break;
}
if (parent.type === 'Element') {
break;
}
parent = parent.parent;
}

if (!is_figure_parent) {
this.component.warn(this, {
code: `a11y-structure`,
message: `A11y: <figcaption> must be an immediate child of <figure>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let caption = 'a foo in its natural habitat';
</script>

<figure>
<img src='foo.jpg' alt='a picture of a foo'>
{#if caption}
<figcaption>{caption}</figcaption>
{/if}
</figure>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit 16dbb8f

Please sign in to comment.