Unverified Commit 861bc0d7 authored by lijianan's avatar lijianan Committed by GitHub

fix: error when image `width={Infinity}` (#48855)

This handles the case when `Infinity` is passed to the Image `width` prop
parent f2b920b2
...@@ -254,9 +254,12 @@ function generateImgAttrs({ ...@@ -254,9 +254,12 @@ function generateImgAttrs({
} }
function getInt(x: unknown): number | undefined { function getInt(x: unknown): number | undefined {
if (typeof x === 'number' || typeof x === 'undefined') { if (typeof x === 'undefined') {
return x return x
} }
if (typeof x === 'number') {
return Number.isFinite(x) ? x : NaN
}
if (typeof x === 'string' && /^[0-9]+$/.test(x)) { if (typeof x === 'string' && /^[0-9]+$/.test(x)) {
return parseInt(x, 10) return parseInt(x, 10)
} }
......
import React from 'react'
import Image from 'next/image'
export default function Page() {
return (
<div>
<p>Invalid Infinity width</p>
<Image src="/test.jpg" width={Infinity} height={300} />
</div>
)
}
...@@ -824,6 +824,15 @@ function runTests(mode) { ...@@ -824,6 +824,15 @@ function runTests(mode) {
) )
}) })
it('should show error when invalid Infinity width prop', async () => {
const browser = await webdriver(appPort, '/invalid-Infinity-width')
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" property. Expected a numeric value in pixels but received "Infinity".`
)
})
it('should show error when invalid height prop', async () => { it('should show error when invalid height prop', async () => {
const browser = await webdriver(appPort, '/invalid-height') const browser = await webdriver(appPort, '/invalid-height')
......
import React from 'react'
import Image from 'next/image'
export default function Page() {
return (
<div>
<p>Invalid Infinity width</p>
<Image src="/test.jpg" width={Infinity} height={300} />
</div>
)
}
...@@ -824,6 +824,15 @@ function runTests(mode) { ...@@ -824,6 +824,15 @@ function runTests(mode) {
) )
}) })
it('should show error when invalid Infinity width prop', async () => {
const browser = await webdriver(appPort, '/invalid-Infinity-width')
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" property. Expected a numeric value in pixels but received "Infinity".`
)
})
it('should show error when invalid height prop', async () => { it('should show error when invalid height prop', async () => {
const browser = await webdriver(appPort, '/invalid-height') const browser = await webdriver(appPort, '/invalid-height')
......
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