Skip to content

Commit 7684b4f

Browse files
wconnorwalshwardpeet
authored andcommitted
feat(gatsby-image): Whitelist crossorigin prop for pass through to img tag (#9758)
* Whitelisted `crossorigin` prop for pass through to `img` tag * Updated typings.
1 parent 9298fa3 commit 7684b4f

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

packages/gatsby-image/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ You will need to add it in your graphql query as is shown in the following snipp
315315
| `fadeIn` | `bool` | Defaults to fading in the image on load |
316316
| `title` | `string` | Passed to the `img` element |
317317
| `alt` | `string` | Passed to the `img` element |
318+
| `crossOrigin` | `string` | Passed to the `img` element |
318319
| `className` | `string` / `object` | Passed to the wrapper element. Object is needed to support Glamor's css prop |
319320
| `style` | `object` | Spread into the default styles of the wrapper element |
320321
| `imgStyle` | `object` | Spread into the default styles of the actual `img` element |

packages/gatsby-image/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface GatsbyImageProps {
3232
alt?: string
3333
className?: string | object
3434
critical?: boolean
35+
crossOrigin?: string | boolean;
3536
style?: object
3637
imgStyle?: object
3738
placeholderStyle?: object

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports[`<Img /> should render fixed size images 1`] = `
2424
/>
2525
<img
2626
alt="Alt text for the image"
27+
crossorigin="anonymous"
2728
height="100"
2829
itemprop="item-prop-for-the-image"
2930
src="test_image.jpg"
@@ -68,6 +69,7 @@ exports[`<Img /> should render fluid images 1`] = `
6869
/>
6970
<img
7071
alt="Alt text for the image"
72+
crossorigin="anonymous"
7173
itemprop="item-prop-for-the-image"
7274
sizes="(max-width: 600px) 100vw, 600px"
7375
src="test_image.jpg"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const setup = (fluid = false, onLoad = () => {}, onError = () => {}) => {
3131
style={{ display: `inline` }}
3232
title={`Title for the image`}
3333
alt={`Alt text for the image`}
34+
crossOrigin={`anonymous`}
3435
{...fluid && { fluid: fluidShapeMock }}
3536
{...!fluid && { fixed: fixedShapeMock }}
3637
onLoad={onLoad}
@@ -55,12 +56,13 @@ describe(`<Img />`, () => {
5556
expect(component).toMatchSnapshot()
5657
})
5758

58-
it(`should have correct src, title and alt attributes`, () => {
59+
it(`should have correct src, title, alt, and crossOrigin attributes`, () => {
5960
const imageTag = setup().querySelector(`picture img`)
6061
expect(imageTag.getAttribute(`src`)).toEqual(`test_image.jpg`)
6162
expect(imageTag.getAttribute(`srcSet`)).toEqual(`some srcSet`)
6263
expect(imageTag.getAttribute(`title`)).toEqual(`Title for the image`)
6364
expect(imageTag.getAttribute(`alt`)).toEqual(`Alt text for the image`)
65+
expect(imageTag.getAttribute(`crossOrigin`)).toEqual(`anonymous`)
6466
})
6567

6668
it(`should have correct placeholder src, title, style and class attributes`, () => {

packages/gatsby-image/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ const noscriptImg = props => {
9898
const height = props.height ? `height="${props.height}" ` : ``
9999
const opacity = props.opacity ? props.opacity : `1`
100100
const transitionDelay = props.transitionDelay ? props.transitionDelay : `0.5s`
101-
return `<picture>${srcSetWebp}<img ${width}${height}${sizes}${srcSet}${src}${alt}${title}style="position:absolute;top:0;left:0;transition:opacity 0.5s;transition-delay:${transitionDelay};opacity:${opacity};width:100%;height:100%;object-fit:cover;object-position:center"/></picture>`
101+
const crossOrigin = props.crossOrigin
102+
? `crossorigin="${props.crossOrigin}" `
103+
: ``
104+
return `<picture>${srcSetWebp}<img ${width}${height}${sizes}${srcSet}${src}${alt}${title}${crossOrigin}style="position:absolute;top:0;left:0;transition:opacity 0.5s;transition-delay:${transitionDelay};opacity:${opacity};width:100%;height:100%;object-fit:cover;object-position:center"/></picture>`
102105
}
103106

104107
const Img = React.forwardRef((props, ref) => {
@@ -337,6 +340,7 @@ class Image extends React.Component {
337340
title={title}
338341
sizes={image.sizes}
339342
src={image.src}
343+
crossOrigin={this.props.crossOrigin}
340344
srcSet={image.srcSet}
341345
style={imageStyle}
342346
ref={this.imageRef}
@@ -423,6 +427,7 @@ class Image extends React.Component {
423427
height={image.height}
424428
sizes={image.sizes}
425429
src={image.src}
430+
crossOrigin={this.props.crossOrigin}
426431
srcSet={image.srcSet}
427432
style={imageStyle}
428433
ref={this.imageRef}
@@ -494,6 +499,7 @@ Image.propTypes = {
494499
alt: PropTypes.string,
495500
className: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), // Support Glamor's css prop.
496501
critical: PropTypes.bool,
502+
crossOrigin: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
497503
style: PropTypes.object,
498504
imgStyle: PropTypes.object,
499505
placeholderStyle: PropTypes.object,

0 commit comments

Comments
 (0)