Unverified Commit 1a0c1e8a authored by Steven's avatar Steven Committed by GitHub

Fix `next/image` when parent is `<p>` (#30041)

- Fixes #29717 
- Fixes #21433 
- Closes #21475 
parent 0daaae32
...@@ -16,7 +16,7 @@ description: Enable Image Optimization with the built-in Image component. ...@@ -16,7 +16,7 @@ description: Enable Image Optimization with the built-in Image component.
| Version | Changes | | Version | Changes |
| --------- | ------------------------------------------------------------------------------------------------- | | --------- | ------------------------------------------------------------------------------------------------- |
| `v12.0.0` | `formats` configuration added as well as AVIF support. | | `v12.0.0` | `formats` configuration added.<br/>AVIF format added.<br/>Wrapper `<div>` changed to `<span>`. |
| `v11.1.0` | `onLoadingComplete` and `lazyBoundary` props added. | | `v11.1.0` | `onLoadingComplete` and `lazyBoundary` props added. |
| `v11.0.0` | `src` prop support for static import.<br/>`placeholder` prop added.<br/>`blurDataURL` prop added. | | `v11.0.0` | `src` prop support for static import.<br/>`placeholder` prop added.<br/>`blurDataURL` prop added. |
| `v10.0.5` | `loader` prop added. | | `v10.0.5` | `loader` prop added. |
......
...@@ -151,7 +151,7 @@ The image component has several different [layout modes](/docs/api-reference/nex ...@@ -151,7 +151,7 @@ The image component has several different [layout modes](/docs/api-reference/nex
**Target the image with className, not based on DOM structure** **Target the image with className, not based on DOM structure**
Regardless of the layout mode used, the Image component will have a consistent DOM structure of one `<img>` tag wrapped by exactly one `<div>`. For some modes, it may also have a sibling `<div>` for spacing. These additional `<div>` elements are critical to allow the component to prevent layout shifts. Regardless of the layout mode used, the Image component will have a consistent DOM structure of one `<img>` tag wrapped by exactly one `<span>`. For some modes, it may also have a sibling `<span>` for spacing. These additional `<span>` elements are critical to allow the component to prevent layout shifts.
The recommended way to style the inner `<img>` is to set the `className` prop on the Image component to the value of an imported [CSS Module](/docs/basic-features/built-in-css-support.md#adding-component-level-css). The value of `className` will be automatically applied to the underlying `<img>` element. The recommended way to style the inner `<img>` is to set the `className` prop on the Image component to the value of an imported [CSS Module](/docs/basic-features/built-in-css-support.md#adding-component-level-css). The value of `className` will be automatically applied to the underlying `<img>` element.
......
...@@ -458,8 +458,8 @@ export default function Image({ ...@@ -458,8 +458,8 @@ export default function Image({
}) })
const isVisible = !isLazy || isIntersected const isVisible = !isLazy || isIntersected
let wrapperStyle: JSX.IntrinsicElements['div']['style'] | undefined let wrapperStyle: JSX.IntrinsicElements['span']['style'] | undefined
let sizerStyle: JSX.IntrinsicElements['div']['style'] | undefined let sizerStyle: JSX.IntrinsicElements['span']['style'] | undefined
let sizerSvg: string | undefined let sizerSvg: string | undefined
let imgStyle: ImgElementStyle | undefined = { let imgStyle: ImgElementStyle | undefined = {
position: 'absolute', position: 'absolute',
...@@ -537,17 +537,17 @@ export default function Image({ ...@@ -537,17 +537,17 @@ export default function Image({
margin: 0, margin: 0,
} }
sizerStyle = { sizerStyle = {
boxSizing: 'border-box',
display: 'block', display: 'block',
boxSizing: 'border-box',
maxWidth: '100%', maxWidth: '100%',
} }
sizerSvg = `<svg width="${widthInt}" height="${heightInt}" xmlns="http://www.w3.org/2000/svg" version="1.1"/>` sizerSvg = `<svg width="${widthInt}" height="${heightInt}" xmlns="http://www.w3.org/2000/svg" version="1.1"/>`
} else if (layout === 'fixed') { } else if (layout === 'fixed') {
// <Image src="i.png" width="100" height="100" layout="fixed" /> // <Image src="i.png" width="100" height="100" layout="fixed" />
wrapperStyle = { wrapperStyle = {
display: 'inline-block',
overflow: 'hidden', overflow: 'hidden',
boxSizing: 'border-box', boxSizing: 'border-box',
display: 'inline-block',
position: 'relative', position: 'relative',
width: widthInt, width: widthInt,
height: heightInt, height: heightInt,
...@@ -583,9 +583,9 @@ export default function Image({ ...@@ -583,9 +583,9 @@ export default function Image({
let srcString: string = src let srcString: string = src
return ( return (
<div style={wrapperStyle}> <span style={wrapperStyle}>
{sizerStyle ? ( {sizerStyle ? (
<div style={sizerStyle}> <span style={sizerStyle}>
{sizerSvg ? ( {sizerSvg ? (
<img <img
style={{ style={{
...@@ -600,7 +600,7 @@ export default function Image({ ...@@ -600,7 +600,7 @@ export default function Image({
src={`data:image/svg+xml;base64,${toBase64(sizerSvg)}`} src={`data:image/svg+xml;base64,${toBase64(sizerSvg)}`}
/> />
) : null} ) : null}
</div> </span>
) : null} ) : null}
<img <img
{...rest} {...rest}
...@@ -659,7 +659,7 @@ export default function Image({ ...@@ -659,7 +659,7 @@ export default function Image({
></link> ></link>
</Head> </Head>
) : null} ) : null}
</div> </span>
) )
} }
......
import React from 'react'
import Image from 'next/image'
import img from '../public/test.jpg'
const Page = () => {
return (
<p>
<Image id="inside-paragraph" src={img} />
</p>
)
}
export default Page
...@@ -645,6 +645,19 @@ function runTests(mode) { ...@@ -645,6 +645,19 @@ function runTests(mode) {
expect(await hasRedbox(browser)).toBe(false) expect(await hasRedbox(browser)).toBe(false)
}) })
it('should not warn when Image is child of p', async () => {
const browser = await webdriver(appPort, '/inside-paragraph')
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
expect(await hasRedbox(browser)).toBe(false)
expect(warnings).not.toMatch(
/Expected server HTML to contain a matching/gm
)
expect(warnings).not.toMatch(/cannot appear as a descendant/gm)
})
} else { } else {
//server-only tests //server-only tests
it('should not create an image folder in server/chunks', async () => { it('should not create an image folder in server/chunks', async () => {
......
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