-
Notifications
You must be signed in to change notification settings - Fork 25k
feat: added crossOrigin, referrerPolicy, srcSet, width, height and src props to the Image Component.
#34481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
dhruvtailor7
wants to merge
15
commits into
facebook:main
from
dhruvtailor7:feat/image-map-src-to-source
Closed
feat: added crossOrigin, referrerPolicy, srcSet, width, height and src props to the Image Component.
#34481
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
70ddbf1
feat: map `src` prop to `source.uri` in Image Component
920362f
flow error resolved
e4e2c98
flow error resolved
db25a3b
Merge branch 'facebook:main' into feat/image-map-src-to-source
dhruvtailor7 d0d801f
adding `crossOrigin`, `referrerPolicy` and `srcSet` props to Image co…
525ddfd
Merge branch 'facebook:main' into feat/image-map-src-to-source
dhruvtailor7 110ff34
separated out the common logic for picking image source and added tes…
7a2ed13
Merge branch 'main' into feat/image-map-src-to-source
783ba84
updated headers logic and added a test case
839c6e2
resolve flow test cases
acbe420
Merge branch 'facebook:main' into feat/image-map-src-to-source
dhruvtailor7 19380c0
updated width and height type.
a7e5cf8
Merge branch 'facebook:main' into feat/image-map-src-to-source
dhruvtailor7 8c8f539
review changes
c4d59c2
use ternary operator instead of if.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import type {ResolvedAssetSource} from './AssetSourceResolver'; | ||
| import type {ImageProps} from './ImageProps'; | ||
|
|
||
| import resolveAssetSource from './resolveAssetSource'; | ||
|
|
||
| /** | ||
| * A function which returns the appropriate value for image source | ||
| * by resolving the `source`, `src` and `srcSet` props. | ||
| */ | ||
| export function getImageSourcesFromImageProps( | ||
| imageProps: ImageProps, | ||
| ): ?ResolvedAssetSource | $ReadOnlyArray<{uri: string, ...}> { | ||
| let source = resolveAssetSource(imageProps.source); | ||
|
|
||
| let sources; | ||
|
|
||
| const {crossOrigin, referrerPolicy, src, srcSet} = imageProps; | ||
dhruvtailor7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const getHeaders = () => { | ||
dhruvtailor7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const headers: {[string]: string} = {}; | ||
| if (crossOrigin === 'use-credentials') { | ||
| headers['Access-Control-Allow-Credentials'] = 'true'; | ||
| } | ||
| if (referrerPolicy != null) { | ||
| headers['Referrer-Policy'] = referrerPolicy; | ||
| } | ||
| return headers; | ||
| }; | ||
|
|
||
| if (srcSet != null) { | ||
| const sourceList = []; | ||
| const srcSetList = srcSet.split(', '); | ||
| // `src` prop should be used with default scale if `srcSet` does not have 1x scale. | ||
| let shouldUseSrcForDefaultScale = true; | ||
| const {width, height} = imageProps; | ||
dhruvtailor7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| srcSetList.forEach(imageSrc => { | ||
| const [uri, xScale = '1x'] = imageSrc.split(' '); | ||
| const scale = parseInt(xScale.split('x')[0], 10); | ||
dhruvtailor7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (scale) { | ||
| if (scale === 1) { | ||
dhruvtailor7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 1x scale is provided in `srcSet` prop so ignore the `src` prop if provided. | ||
| shouldUseSrcForDefaultScale = false; | ||
| } | ||
| sourceList.push({headers: getHeaders(), scale, uri, width, height}); | ||
| } | ||
| }); | ||
|
|
||
| if (shouldUseSrcForDefaultScale && src != null) { | ||
| sourceList.push({ | ||
| headers: getHeaders(), | ||
| scale: 1, | ||
| uri: src, | ||
| width, | ||
| height, | ||
| }); | ||
| } | ||
| if (sourceList.length === 0) { | ||
| console.warn('The provided value for srcSet is not valid.'); | ||
| } | ||
|
|
||
| sources = sourceList; | ||
| } else if (src != null) { | ||
| const {width, height} = imageProps; | ||
| sources = [{uri: src, headers: getHeaders(), width, height}]; | ||
| } else { | ||
| sources = source; | ||
| } | ||
| return sources; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @format | ||
| * @emails oncall+react_native | ||
| */ | ||
|
|
||
| const {getImageSourcesFromImageProps} = require('../ImageSourceUtils'); | ||
|
|
||
| describe('ImageSourceUtils', () => { | ||
| it('source prop provided', () => { | ||
| const imageProps = {source: require('./img/img1.png')}; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should ignore source when src is provided', () => { | ||
| let uri = 'imageURI'; | ||
| const imageProps = {source: require('./img/img1.png'), src: uri}; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(1); | ||
| expect(sources[0].uri).toBe(uri); | ||
| }); | ||
|
|
||
| it('should ignore source and src when srcSet is provided', () => { | ||
| let uri = 'imageURI'; | ||
|
|
||
| let uri1 = 'uri1'; | ||
| let scale1 = '1x'; | ||
|
|
||
| let uri2 = 'uri2'; | ||
| let scale2 = '2x'; | ||
|
|
||
| const imageProps = { | ||
| source: require('./img/img1.png'), | ||
| src: uri, | ||
| srcSet: `${uri1} ${scale1}, ${uri2} ${scale2}`, | ||
| }; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(2); | ||
| expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 1})); | ||
| expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 2})); | ||
| }); | ||
|
|
||
| it('should use src as default when 1x scale is not provided in srcSet', () => { | ||
| let uri = 'imageURI'; | ||
|
|
||
| let uri1 = 'uri1'; | ||
| let scale1 = '3x'; | ||
|
|
||
| let uri2 = 'uri2'; | ||
| let scale2 = '2x'; | ||
|
|
||
| const imageProps = { | ||
| src: uri, | ||
| srcSet: `${uri1} ${scale1}, ${uri2} ${scale2}`, | ||
| }; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(3); | ||
| expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 3})); | ||
| expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 2})); | ||
| expect(sources[2]).toEqual(expect.objectContaining({uri: uri, scale: 1})); | ||
| }); | ||
|
|
||
| it('should use 1x as default scale if only url is provided in srcSet', () => { | ||
dhruvtailor7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let uri1 = 'uri1'; | ||
| let scale1 = '2x'; | ||
|
|
||
| let uri2 = 'uri2'; | ||
|
|
||
| const imageProps = { | ||
| srcSet: `${uri1} ${scale1}, ${uri2}`, | ||
| }; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(2); | ||
| expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 2})); | ||
| expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 1})); | ||
| }); | ||
|
|
||
| it('should contain crossorigin headers when provided with src', () => { | ||
| let uri = 'imageURI'; | ||
|
|
||
| const imageProps = { | ||
| src: uri, | ||
| crossOrigin: 'use-credentials', | ||
| }; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(1); | ||
| expect(sources[0]).toHaveProperty('headers', { | ||
| ['Access-Control-Allow-Credentials']: 'true', | ||
| }); | ||
| }); | ||
|
|
||
| it('should contain referrerPolicy headers when provided with src', () => { | ||
| let uri = 'imageURI'; | ||
|
|
||
| let referrerPolicy = 'origin-when-cross-origin'; | ||
| const imageProps = { | ||
| src: uri, | ||
| referrerPolicy: referrerPolicy, | ||
| }; | ||
| const sources = getImageSourcesFromImageProps(imageProps); | ||
|
|
||
| expect(sources).toBeDefined(); | ||
| expect(sources).toHaveLength(1); | ||
| expect(sources[0]).toHaveProperty('headers', { | ||
| ['Referrer-Policy']: referrerPolicy, | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.