Skip to content

Commit d5424c6

Browse files
committed
fix: treat <img> alt attribute as content for a11y labelling purposes
1 parent 505e8ca commit d5424c6

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

.changeset/nervous-chefs-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: treat `<img>` alt attribute as content for a11y labelling purposes

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,13 @@ function has_content(element) {
11611161
}
11621162

11631163
if (node.type === 'RegularElement' || node.type === 'SvelteElement') {
1164+
if (
1165+
node.name === 'img' &&
1166+
node.attributes.some((node) => node.type === 'Attribute' && node.name === 'alt')
1167+
) {
1168+
return true;
1169+
}
1170+
11641171
if (!has_content(node)) {
11651172
continue;
11661173
}

packages/svelte/tests/validator/samples/a11y-consider-explicit-label/input.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99

1010
<button>Click me</button>
1111
<a href="/#">Link text</a>
12+
<a href="/#"><img src="./icon.svg" alt="Link text"></a>

0 commit comments

Comments
 (0)