Skip to content

Commit e0095fa

Browse files
authored
fix(next/image): render valid html according to W3C (#33825)
## Bug - [x] Fixes #33809 - [x] Related to #33218 - [x] Integration tests added: Usage of [html-validator](https://www.npmjs.com/package/html-validator) to validate the HTML. - [x] Errors have helpful link attached, see `contributing.md` (N/A)
1 parent b00270b commit e0095fa

File tree

6 files changed

+250
-8
lines changed

6 files changed

+250
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@testing-library/react": "11.2.5",
5757
"@types/cheerio": "0.22.16",
5858
"@types/fs-extra": "8.1.0",
59+
"@types/html-validator": "5.0.2",
5960
"@types/http-proxy": "1.17.3",
6061
"@types/jest": "24.0.13",
6162
"@types/node": "13.11.0",
@@ -112,6 +113,7 @@
112113
"get-port": "5.1.1",
113114
"glob": "7.1.6",
114115
"gzip-size": "5.1.1",
116+
"html-validator": "5.1.18",
115117
"image-size": "0.9.3",
116118
"is-animated": "2.0.0",
117119
"isomorphic-unfetch": "3.0.0",

packages/next/client/image.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,7 @@ export default function Image({
605605
wrapperStyle.maxWidth = '100%'
606606
hasSizer = true
607607
sizerStyle.maxWidth = '100%'
608-
// url encoded svg is a little bit shorten than base64 encoding
609-
sizerSvgUrl = `data:image/svg+xml,%3csvg xmlns=%27http://www.w3.org/2000/svg%27 version=%271.1%27 width=%27${widthInt}%27 height=%27${heightInt}%27/%3e`
608+
sizerSvgUrl = `data:image/svg+xml,%3csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20version=%271.1%27%20width=%27${widthInt}%27%20height=%27${heightInt}%27/%3e`
610609
} else if (layout === 'fixed') {
611610
// <Image src="i.png" width="100" height="100" layout="fixed" />
612611
wrapperStyle.display = 'inline-block'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Html, Head, Main, NextScript } from 'next/document'
2+
3+
export default function Document() {
4+
return (
5+
<Html lang="en">
6+
<Head />
7+
<body>
8+
<Main />
9+
<NextScript />
10+
</body>
11+
</Html>
12+
)
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Head from 'next/head'
2+
import Image from 'next/image'
3+
4+
const Page = () => {
5+
return (
6+
<div>
7+
<Head>
8+
<title>Title</title>
9+
<meta name="description" content="Description..." />
10+
<link rel="icon" type="image/jpeg" href="/test.jpg" />
11+
</Head>
12+
13+
<main>
14+
<Image src="/test.jpg" width="400" height="400" alt="basic image" />
15+
</main>
16+
</div>
17+
)
18+
}
19+
20+
export default Page

test/integration/image-component/default/test/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env jest */
22

33
import cheerio from 'cheerio'
4+
import validateHTML from 'html-validator'
45
import {
56
check,
67
findPort,
@@ -1039,6 +1040,26 @@ function runTests(mode) {
10391040
}
10401041
}
10411042
})
1043+
1044+
it('should be valid W3C HTML', async () => {
1045+
let browser
1046+
try {
1047+
browser = await webdriver(appPort, '/valid-html-w3c')
1048+
await waitFor(1000)
1049+
expect(await browser.hasElementByCssSelector('img')).toBeTruthy()
1050+
const url = await browser.url()
1051+
const result = await validateHTML({
1052+
url,
1053+
format: 'json',
1054+
isLocal: true,
1055+
})
1056+
expect(result.messages).toEqual([])
1057+
} finally {
1058+
if (browser) {
1059+
await browser.close()
1060+
}
1061+
}
1062+
})
10421063
}
10431064

10441065
describe('Image Component Tests', () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy