-
-
Notifications
You must be signed in to change notification settings - Fork 4
fix: inject charset meta tag at start of head #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chenjiahan
wants to merge
4
commits into
main
Choose a base branch
from
chenjiahan/fix-charset-meta-position
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−20
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63e14a1
fix: inject charset meta tag at start of head
chenjiahan 9d8b107
Merge remote-tracking branch 'origin/main' into chenjiahan/fix-charse…
chenjiahan 8d33b38
fix: simplify charset tag injection
chenjiahan 74a6c13
refactor: simplify head tag lookup
chenjiahan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Buffer } from 'node:buffer'; | ||
| import { describe, expect, it } from '@rstest/core'; | ||
| import { HtmlRspackPlugin } from './helpers/compile.js'; | ||
|
|
||
| const compiler = { | ||
| options: { | ||
| mode: 'development', | ||
| }, | ||
| }; | ||
|
|
||
| function createTag(tagName, attributes, innerHTML) { | ||
| return { | ||
| tagName, | ||
| voidTag: tagName.toLowerCase() === 'meta', | ||
| attributes: attributes || {}, | ||
| innerHTML: innerHTML || '', | ||
| meta: {}, | ||
| }; | ||
| } | ||
|
|
||
| function postProcessHtml(html, headTags, options) { | ||
| const plugin = new HtmlRspackPlugin({ inject: true, ...options }); | ||
|
|
||
| return plugin.postProcessHtml( | ||
| compiler, | ||
| html, | ||
| { publicPath: '', js: [], css: [] }, | ||
| { headTags, bodyTags: [] }, | ||
| ); | ||
| } | ||
|
|
||
| describe('HtmlRspackPlugin HTML post processing', () => { | ||
| it('injects a generated charset at the start of head and within the first 1024 bytes', async () => { | ||
| const longHeadContent = '你好'.repeat(300); | ||
| const html = `<!doctype html><html><head>${longHeadContent}</head><body></body></html>`; | ||
| const output = await postProcessHtml(html, [ | ||
| createTag('meta', { charset: 'UTF-8' }), | ||
| createTag('script', { src: 'main.js' }), | ||
| ]); | ||
| const charsetMatch = /<meta charset="UTF-8">/.exec(output); | ||
|
|
||
| expect(charsetMatch).not.toBeNull(); | ||
| expect(output).toContain('<head><meta charset="UTF-8">'); | ||
| expect(output).toContain('<script src="main.js"></script></head>'); | ||
| expect( | ||
| Buffer.byteLength( | ||
| output.slice(0, charsetMatch.index + charsetMatch[0].length), | ||
| ), | ||
| ).toBeLessThanOrEqual(1024); | ||
| }); | ||
|
|
||
| it('handles greater-than signs inside head attributes', async () => { | ||
| const html = '<html><head data-value=">"></head><body></body></html>'; | ||
| const output = await postProcessHtml(html, [ | ||
| createTag('meta', { charset: 'UTF-8' }), | ||
| ]); | ||
|
|
||
| expect(output).toBe( | ||
| '<html><head data-value=">"><meta charset="UTF-8"></head><body></body></html>', | ||
| ); | ||
| }); | ||
|
|
||
| it('creates a head before injecting a generated charset', async () => { | ||
| const output = await postProcessHtml( | ||
| '<!doctype html><html><body></body></html>', | ||
| [ | ||
| createTag('meta', { charset: 'UTF-8' }), | ||
| createTag('script', { src: 'main.js' }), | ||
| ], | ||
| ); | ||
|
|
||
| expect(output).toBe( | ||
| '<!doctype html><html><head><meta charset="UTF-8"><script src="main.js"></script></head><body></body></html>', | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the template already contains an active charset declaration and
metagenerates another with a different value, this loop unconditionally places the generated declaration before the authored one. Previously the generated tag was appended near</head>, so the template's earlier declaration retained precedence; the new order changes the browser's effective encoding and leaves conflicting declarations. Detect an active template charset and avoid prepending another one.Useful? React with 👍 / 👎.