Unverified Commit 4df0778c authored by Steven's avatar Steven Committed by GitHub

Fix error when `next/future/image` is missing width or height (#38132)

This PR fixes the case when `next/future/image` is missing the width or height props.

It also fixes a case when `100%` or `100vh` were incorrectly parsed as `100` pixels.
parent 876d4d12
...@@ -228,13 +228,13 @@ function generateImgAttrs({ ...@@ -228,13 +228,13 @@ function generateImgAttrs({
} }
function getInt(x: unknown): number | undefined { function getInt(x: unknown): number | undefined {
if (typeof x === 'number') { if (typeof x === 'number' || typeof x === 'undefined') {
return x return x
} }
if (typeof x === 'string') { if (typeof x === 'string' && /^[0-9]+$/.test(x)) {
return parseInt(x, 10) return parseInt(x, 10)
} }
return undefined return NaN
} }
// See https://stackoverflow.com/q/39777833/266535 for why we use this ref // See https://stackoverflow.com/q/39777833/266535 for why we use this ref
...@@ -382,12 +382,22 @@ export default function Image({ ...@@ -382,12 +382,22 @@ export default function Image({
)}` )}`
) )
} }
if ( if (typeof widthInt === 'undefined') {
(typeof widthInt !== 'undefined' && isNaN(widthInt)) || throw new Error(
(typeof heightInt !== 'undefined' && isNaN(heightInt)) `Image with src "${src}" is missing required "width" property.`
) { )
} else if (isNaN(widthInt)) {
throw new Error(
`Image with src "${src}" has invalid "width" property. Expected a numeric value in pixels but received "${width}".`
)
}
if (typeof heightInt === 'undefined') {
throw new Error(
`Image with src "${src}" is missing required "height" property.`
)
} else if (isNaN(heightInt)) {
throw new Error( throw new Error(
`Image with src "${src}" has invalid "width" or "height" property. These should be numeric values.` `Image with src "${src}" has invalid "height" property. Expected a numeric value in pixels but received "${height}".`
) )
} }
if (!VALID_LOADING_VALUES.includes(loading)) { if (!VALID_LOADING_VALUES.includes(loading)) {
......
...@@ -4,15 +4,9 @@ import Image from 'next/future/image' ...@@ -4,15 +4,9 @@ import Image from 'next/future/image'
export default function Page() { export default function Page() {
return ( return (
<div> <div>
<p>Invalid width or height</p> <p>Invalid height</p>
<Image <Image src="/test.jpg" width={400} height="50vh" />
src="/test.jpg"
width="fill"
height="fill"
placeholder="blur"
blurDataURL="/test.jpg"
/>
</div> </div>
) )
} }
import React from 'react'
import Image from 'next/future/image'
export default function Page() {
return (
<div>
<p>Invalid width</p>
<Image src="/test.jpg" width="100%" height={300} />
</div>
)
}
import React from 'react'
import Image from 'next/future/image'
export default function Page() {
return (
<div>
<h1>Missing height</h1>
<Image src="/test.jpg" width="100" />
</div>
)
}
import React from 'react'
import Image from 'next/future/image'
export default function Page() {
return (
<div>
<h1>Missing width or height</h1>
<Image src="/test.jpg" height={100} />
</div>
)
}
...@@ -29,6 +29,8 @@ const Page = () => { ...@@ -29,6 +29,8 @@ const Page = () => {
id="2" id="2"
src={clicked ? require('../public/test.png') : red} src={clicked ? require('../public/test.png') : red}
placeholder={clicked ? 'blur' : 'empty'} placeholder={clicked ? 'blur' : 'empty'}
width="256"
height="256"
/> />
<ImageWithMessage <ImageWithMessage
......
...@@ -57,6 +57,8 @@ const Page = () => { ...@@ -57,6 +57,8 @@ const Page = () => {
<ImageWithMessage <ImageWithMessage
id="6" id="6"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAFCAYAAACAcVaiAAAAE0lEQVR42mNk+P+/ngEKGMngAADDdwx3Uz/3AAAAAABJRU5ErkJggg==" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAFCAYAAACAcVaiAAAAE0lEQVR42mNk+P+/ngEKGMngAADDdwx3Uz/3AAAAAABJRU5ErkJggg=="
width={128}
height={128}
idToCount={idToCount} idToCount={idToCount}
setIdToCount={setIdToCount} setIdToCount={setIdToCount}
/> />
...@@ -108,9 +110,9 @@ function ImageWithMessage({ id, idToCount, setIdToCount, ...props }) { ...@@ -108,9 +110,9 @@ function ImageWithMessage({ id, idToCount, setIdToCount, ...props }) {
count++ count++
idToCount[id] = count idToCount[id] = count
setIdToCount(idToCount) setIdToCount(idToCount)
const msg = `loaded ${count} img${id} with dimensions ${naturalWidth}x${naturalHeight}` setMsg(
setMsg(msg) `loaded ${count} img${id} with dimensions ${naturalWidth}x${naturalHeight}`
console.log(msg) )
}} }}
{...props} {...props}
/> />
......
...@@ -206,9 +206,7 @@ function runTests(mode) { ...@@ -206,9 +206,7 @@ function runTests(mode) {
}) })
it('should callback onLoadingComplete when image is fully loaded', async () => { it('should callback onLoadingComplete when image is fully loaded', async () => {
let browser let browser = await webdriver(appPort, '/on-loading-complete')
try {
browser = await webdriver(appPort, '/on-loading-complete')
await browser.eval( await browser.eval(
`document.getElementById("footer").scrollIntoView({behavior: "smooth"})` `document.getElementById("footer").scrollIntoView({behavior: "smooth"})`
...@@ -293,11 +291,6 @@ function runTests(mode) { ...@@ -293,11 +291,6 @@ function runTests(mode) {
() => browser.eval(`document.getElementById("msg9").textContent`), () => browser.eval(`document.getElementById("msg9").textContent`),
'loaded 1 img9 with dimensions 400x400' 'loaded 1 img9 with dimensions 400x400'
) )
} finally {
if (browser) {
await browser.close()
}
}
}) })
it('should callback native onLoad in most cases', async () => { it('should callback native onLoad in most cases', async () => {
...@@ -645,12 +638,39 @@ function runTests(mode) { ...@@ -645,12 +638,39 @@ function runTests(mode) {
) )
}) })
it('should show error when not numeric string width or height', async () => { it('should show error when invalid width prop', async () => {
const browser = await webdriver(appPort, '/invalid-width-or-height') const browser = await webdriver(appPort, '/invalid-width')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" property. Expected a numeric value in pixels but received "100%".`
)
})
it('should show error when invalid height prop', async () => {
const browser = await webdriver(appPort, '/invalid-height')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "height" property. Expected a numeric value in pixels but received "50vh".`
)
})
it('should show error when missing width prop', async () => {
const browser = await webdriver(appPort, '/missing-width')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" is missing required "width" property.`
)
})
it('should show error when missing height prop', async () => {
const browser = await webdriver(appPort, '/missing-height')
expect(await hasRedbox(browser)).toBe(true) expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toContain( expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" or "height" property. These should be numeric values.` `Image with src "/test.jpg" is missing required "height" property.`
) )
}) })
......
...@@ -10,5 +10,5 @@ type ImageCardProps = ImageProps & { ...@@ -10,5 +10,5 @@ type ImageCardProps = ImageProps & {
* Example of using the `Image` component in a HOC. * Example of using the `Image` component in a HOC.
*/ */
export function ImageCard(props: ImageCardProps) { export function ImageCard(props: ImageCardProps) {
return <Image {...props} layout="fill" /> return <Image {...props} width="400" height="400" />
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment