Unverified Commit c4e26ab2 authored by Steven's avatar Steven Committed by GitHub

Prevent inherited styles on `next/image` wrapper or sizer (#30064)

As a follow up to #30041 which changed `<div>` to `<span>`, this PR makes sure that unexpected styles are not applied to the image wrapper or sizer spans.

For example: `.content span {}` would apply to all spans and incorrectly style the image wrapper.

This PR adds [`all: initial`](https://developer.mozilla.org/en-US/docs/Web/CSS/all) to effectively reset the span styles.
parent f308b8a3
...@@ -458,10 +458,19 @@ export default function Image({ ...@@ -458,10 +458,19 @@ export default function Image({
}) })
const isVisible = !isLazy || isIntersected const isVisible = !isLazy || isIntersected
let wrapperStyle: JSX.IntrinsicElements['span']['style'] | undefined const wrapperStyle: JSX.IntrinsicElements['span']['style'] = {
let sizerStyle: JSX.IntrinsicElements['span']['style'] | undefined all: 'initial',
boxSizing: 'border-box',
overflow: 'hidden',
}
const sizerStyle: JSX.IntrinsicElements['span']['style'] = {
all: 'initial',
boxSizing: 'border-box',
display: 'block',
}
let hasSizer = false
let sizerSvg: string | undefined let sizerSvg: string | undefined
let imgStyle: ImgElementStyle | undefined = { const imgStyle: ImgElementStyle = {
position: 'absolute', position: 'absolute',
top: 0, top: 0,
left: 0, left: 0,
...@@ -495,19 +504,12 @@ export default function Image({ ...@@ -495,19 +504,12 @@ export default function Image({
: {} : {}
if (layout === 'fill') { if (layout === 'fill') {
// <Image src="i.png" layout="fill" /> // <Image src="i.png" layout="fill" />
wrapperStyle = { wrapperStyle.display = 'block'
display: 'block', wrapperStyle.position = 'absolute'
overflow: 'hidden', wrapperStyle.top = 0
wrapperStyle.left = 0
position: 'absolute', wrapperStyle.bottom = 0
top: 0, wrapperStyle.right = 0
left: 0,
bottom: 0,
right: 0,
boxSizing: 'border-box',
margin: 0,
}
} else if ( } else if (
typeof widthInt !== 'undefined' && typeof widthInt !== 'undefined' &&
typeof heightInt !== 'undefined' typeof heightInt !== 'undefined'
...@@ -517,41 +519,24 @@ export default function Image({ ...@@ -517,41 +519,24 @@ export default function Image({
const paddingTop = isNaN(quotient) ? '100%' : `${quotient * 100}%` const paddingTop = isNaN(quotient) ? '100%' : `${quotient * 100}%`
if (layout === 'responsive') { if (layout === 'responsive') {
// <Image src="i.png" width="100" height="100" layout="responsive" /> // <Image src="i.png" width="100" height="100" layout="responsive" />
wrapperStyle = { wrapperStyle.display = 'block'
display: 'block', wrapperStyle.position = 'relative'
overflow: 'hidden', hasSizer = true
position: 'relative', sizerStyle.paddingTop = paddingTop
boxSizing: 'border-box',
margin: 0,
}
sizerStyle = { display: 'block', boxSizing: 'border-box', paddingTop }
} else if (layout === 'intrinsic') { } else if (layout === 'intrinsic') {
// <Image src="i.png" width="100" height="100" layout="intrinsic" /> // <Image src="i.png" width="100" height="100" layout="intrinsic" />
wrapperStyle = { wrapperStyle.display = 'inline-block'
display: 'inline-block', wrapperStyle.position = 'relative'
maxWidth: '100%', wrapperStyle.maxWidth = '100%'
overflow: 'hidden', hasSizer = true
position: 'relative', sizerStyle.maxWidth = '100%'
boxSizing: 'border-box',
margin: 0,
}
sizerStyle = {
display: 'block',
boxSizing: 'border-box',
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'
display: 'inline-block', wrapperStyle.position = 'relative'
overflow: 'hidden', wrapperStyle.width = widthInt
boxSizing: 'border-box', wrapperStyle.height = heightInt
position: 'relative',
width: widthInt,
height: heightInt,
}
} }
} else { } else {
// <Image src="i.png" /> // <Image src="i.png" />
...@@ -584,11 +569,12 @@ export default function Image({ ...@@ -584,11 +569,12 @@ export default function Image({
return ( return (
<span style={wrapperStyle}> <span style={wrapperStyle}>
{sizerStyle ? ( {hasSizer ? (
<span style={sizerStyle}> <span style={sizerStyle}>
{sizerSvg ? ( {sizerSvg ? (
<img <img
style={{ style={{
all: 'initial',
maxWidth: '100%', maxWidth: '100%',
display: 'block', display: 'block',
margin: 0, margin: 0,
......
import React from 'react'
import Image from 'next/image'
import style from '../style.module.css'
const Page = () => {
return (
<div id="main-container" className={style.mainContainer}>
<h1>Image Style Inheritance</h1>
<Image
id="img-fixed"
layout="fixed"
src="/test.jpg"
width="400"
height="400"
/>
<Image
id="img-intrinsic"
layout="intrinsic"
src="/test.jpg"
width="400"
height="400"
/>
<div style={{ position: 'relative', width: '200px', height: '200px' }}>
<Image id="img-fill" layout="fill" src="/test.jpg" objectFit="cover" />
</div>
<Image
id="img-responsive"
layout="responsive"
src="/test.jpg"
width="400"
height="400"
/>
<footer>Footer</footer>
</div>
)
}
export default Page
.displayFlex { .displayFlex {
display: flex; display: flex;
} }
.mainContainer {
border-radius: 75px;
}
.mainContainer span {
border-radius: 50px;
}
.mainContainer img {
border-radius: 100px;
}
...@@ -699,6 +699,46 @@ function runTests(mode) { ...@@ -699,6 +699,46 @@ function runTests(mode) {
} }
}) })
it('should apply style inheritance for img elements but not wrapper elements', async () => {
let browser
try {
browser = await webdriver(appPort, '/style-inheritance')
await browser.eval(
`document.querySelector("footer").scrollIntoView({behavior: "smooth"})`
)
const allImgs = await browser.eval(`
function foo() {
const imgs = document.querySelectorAll("img[id]");
for (let img of imgs) {
const br = window.getComputedStyle(img).getPropertyValue("border-radius");
if (!br) return 'no-border-radius';
if (br !== '100px') return br;
}
return true;
}()
`)
expect(allImgs).toBe(true)
const allSpans = await browser.eval(`
function foo() {
const spans = document.querySelectorAll("span");
for (let span of spans) {
const br = window.getComputedStyle(span).getPropertyValue("border-radius");
if (br && br !== '0px') return br;
}
return false;
}()
`)
expect(allSpans).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
// Tests that use the `unsized` attribute: // Tests that use the `unsized` attribute:
if (mode !== 'dev') { if (mode !== 'dev') {
it('should correctly rotate image', async () => { it('should correctly rotate image', 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