Skip to content

Commit 3666917

Browse files
grgr-dkrkmadalynrose
authored andcommitted
fix(gatsby-image): Added aria-hidden attribute to layout elements (#20593)
* fixed * fixed index and added testing
1 parent 90c0532 commit 3666917

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ exports[`<Image /> should have a transition-delay of 1sec 1`] = `
77
style="position: relative; overflow: hidden; display: inline; width: 100px; height: 100px;"
88
>
99
<div
10+
aria-hidden="true"
1011
style="background-color: lightgray; width: 100px; opacity: 1; height: 100px; transition-delay: 1000ms;"
1112
title="Title for the image"
1213
/>
1314
<img
1415
alt=""
16+
aria-hidden="true"
1517
class="placeholder"
1618
itemprop="item-prop-for-the-image"
1719
src="string_of_base64"
@@ -54,11 +56,13 @@ exports[`<Image /> should render fixed size images 1`] = `
5456
style="position: relative; overflow: hidden; display: inline; width: 100px; height: 100px;"
5557
>
5658
<div
59+
aria-hidden="true"
5760
style="background-color: lightgray; width: 100px; opacity: 1; height: 100px; transition-delay: 500ms;"
5861
title="Title for the image"
5962
/>
6063
<img
6164
alt=""
65+
aria-hidden="true"
6266
class="placeholder"
6367
itemprop="item-prop-for-the-image"
6468
src="string_of_base64"
@@ -101,14 +105,17 @@ exports[`<Image /> should render fluid images 1`] = `
101105
style="position: relative; overflow: hidden; display: inline;"
102106
>
103107
<div
108+
aria-hidden="true"
104109
style="width: 100%; padding-bottom: 66.66666666666667%;"
105110
/>
106111
<div
112+
aria-hidden="true"
107113
style="background-color: lightgray; position: absolute; top: 0px; bottom: 0px; opacity: 1; right: 0px; left: 0px; transition-delay: 500ms;"
108114
title="Title for the image"
109115
/>
110116
<img
111117
alt=""
118+
aria-hidden="true"
112119
class="placeholder"
113120
itemprop="item-prop-for-the-image"
114121
src="string_of_base64"
@@ -152,6 +159,7 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
152159
style="position: relative; overflow: hidden; display: inline; width: 100px; height: 100px;"
153160
>
154161
<div
162+
aria-hidden="true"
155163
style="background-color: lightgray; width: 100px; opacity: 1; height: 100px; transition-delay: 500ms;"
156164
title="Title for the image"
157165
/>
@@ -165,6 +173,7 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
165173
/>
166174
<img
167175
alt=""
176+
aria-hidden="true"
168177
class="placeholder"
169178
itemprop="item-prop-for-the-image"
170179
src="other_string_of_base64"
@@ -216,9 +225,11 @@ exports[`<Image /> should render multiple fluid image variants 1`] = `
216225
style="position: relative; overflow: hidden; display: inline;"
217226
>
218227
<div
228+
aria-hidden="true"
219229
style="width: 100%; padding-bottom: 50%;"
220230
/>
221231
<div
232+
aria-hidden="true"
222233
style="background-color: lightgray; position: absolute; top: 0px; bottom: 0px; opacity: 1; right: 0px; left: 0px; transition-delay: 500ms;"
223234
title="Title for the image"
224235
/>
@@ -232,6 +243,7 @@ exports[`<Image /> should render multiple fluid image variants 1`] = `
232243
/>
233244
<img
234245
alt=""
246+
aria-hidden="true"
235247
class="placeholder"
236248
itemprop="item-prop-for-the-image"
237249
src="string_of_base64"

packages/gatsby-image/src/__tests__/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,29 @@ describe(`<Image />`, () => {
292292
expect(onLoadMock).toHaveBeenCalledTimes(1)
293293
expect(onErrorMock).toHaveBeenCalledTimes(1)
294294
})
295+
296+
it(`should have an "aria-hidden" attribute on the fluid element`, () => {
297+
const divTag = setup(true).querySelector(`.gatsby-image-wrapper > div`)
298+
expect(divTag.getAttribute(`aria-hidden`)).toBe(`true`)
299+
})
300+
301+
it(`should have an "aria-hidden" attribute on the background element`, () => {
302+
const BgTag = setup(true).querySelector(`.gatsby-image-wrapper > div + div`)
303+
expect(BgTag.getAttribute(`aria-hidden`)).toBe(`true`)
304+
})
305+
306+
it(`should have an "aria-hidden" attribute on the Placeholder component when fluid`, () => {
307+
const placeholderImageTag = setup(true).querySelector(`img`)
308+
expect(placeholderImageTag.getAttribute(`aria-hidden`)).toBe(`true`)
309+
})
310+
311+
it(`should have an "aria-hidden" attribute on the Placeholder component when fixed`, () => {
312+
const placeholderImageTag = setup().querySelector(`img`)
313+
expect(placeholderImageTag.getAttribute(`aria-hidden`)).toBe(`true`)
314+
})
315+
316+
it(`should not have an "aria-hidden" attribute on the Image`, () => {
317+
const placeholderImageTag = setup().querySelector(`picture img`)
318+
expect(placeholderImageTag.getAttribute(`aria-hidden`)).toBe(null)
319+
})
295320
})

packages/gatsby-image/src/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,14 @@ const noscriptImg = props => {
254254
// Earlier versions of gatsby-image during the 2.x cycle did not wrap
255255
// the `Img` component in a `picture` element. This maintains compatibility
256256
// until a breaking change can be introduced in the next major release
257-
const Placeholder = ({ src, imageVariants, generateSources, spreadProps }) => {
258-
const baseImage = <Img src={src} {...spreadProps} />
257+
const Placeholder = ({
258+
src,
259+
imageVariants,
260+
generateSources,
261+
spreadProps,
262+
ariaHidden,
263+
}) => {
264+
const baseImage = <Img src={src} {...spreadProps} ariaHidden={ariaHidden} />
259265

260266
return imageVariants.length > 1 ? (
261267
<picture>
@@ -277,11 +283,14 @@ const Img = React.forwardRef((props, ref) => {
277283
onError,
278284
loading,
279285
draggable,
286+
// `ariaHidden`props is used to exclude placeholders from the accessibility tree.
287+
ariaHidden,
280288
...otherProps
281289
} = props
282290

283291
return (
284292
<img
293+
aria-hidden={ariaHidden}
285294
sizes={sizes}
286295
srcSet={srcSet}
287296
src={src}
@@ -467,6 +476,7 @@ class Image extends React.Component {
467476
>
468477
{/* Preserve the aspect ratio. */}
469478
<Tag
479+
aria-hidden
470480
style={{
471481
width: `100%`,
472482
paddingBottom: `${100 / image.aspectRatio}%`,
@@ -476,6 +486,7 @@ class Image extends React.Component {
476486
{/* Show a solid background color. */}
477487
{bgColor && (
478488
<Tag
489+
aria-hidden
479490
title={title}
480491
style={{
481492
backgroundColor: bgColor,
@@ -493,6 +504,7 @@ class Image extends React.Component {
493504
{/* Show the blurry base64 image. */}
494505
{image.base64 && (
495506
<Placeholder
507+
ariaHidden
496508
src={image.base64}
497509
spreadProps={placeholderImageProps}
498510
imageVariants={imageVariants}
@@ -503,6 +515,7 @@ class Image extends React.Component {
503515
{/* Show the traced SVG image. */}
504516
{image.tracedSVG && (
505517
<Placeholder
518+
ariaHidden
506519
src={image.tracedSVG}
507520
spreadProps={placeholderImageProps}
508521
imageVariants={imageVariants}
@@ -577,6 +590,7 @@ class Image extends React.Component {
577590
{/* Show a solid background color. */}
578591
{bgColor && (
579592
<Tag
593+
aria-hidden
580594
title={title}
581595
style={{
582596
backgroundColor: bgColor,
@@ -591,6 +605,7 @@ class Image extends React.Component {
591605
{/* Show the blurry base64 image. */}
592606
{image.base64 && (
593607
<Placeholder
608+
ariaHidden
594609
src={image.base64}
595610
spreadProps={placeholderImageProps}
596611
imageVariants={imageVariants}
@@ -601,6 +616,7 @@ class Image extends React.Component {
601616
{/* Show the traced SVG image. */}
602617
{image.tracedSVG && (
603618
<Placeholder
619+
ariaHidden
604620
src={image.tracedSVG}
605621
spreadProps={placeholderImageProps}
606622
imageVariants={imageVariants}

0 commit comments

Comments
 (0)