Skip to content

fix: preserve stylesheet text when rewriting encodings - #520

Open
maximilliangrand wants to merge 2 commits into
Y2Z:masterfrom
maximilliangrand:fix/stylesheet-encoding
Open

maximilliangrand wants to merge 2 commits into
Y2Z:masterfrom
maximilliangrand:fix/stylesheet-encoding

Conversation

@maximilliangrand

Copy link
Copy Markdown
Contributor

Related to #351.

A linked stylesheet served as Windows-1252 is decoded before rewriting, but its UTF-8 output is embedded with the original Windows-1252 charset. For example, content: 'café' becomes mojibake when the saved page is opened. CSS @import has the complementary problem: its bytes are decoded as UTF-8 even when another charset is declared.

Reuse the linked-stylesheet decoding path for imports, then label all rewritten stylesheet data URLs as UTF-8. Keep the UTF-8 fallback when charset metadata is absent. No new dependency or public API is needed.

Regression coverage exercises linked stylesheets and all three import forms (quoted string, quoted URL, unquoted URL), using Windows-1252, explicit UTF-8, and no charset. Existing CLI expectations only gain the UTF-8 charset parameter; their stylesheet payloads are unchanged.

Validation:

  • Both new regression tests fail against unchanged production code and pass with the fix.
  • cargo test --all --locked: 300 passed.
  • cargo fmt --all -- --check and git diff --check: pass.
  • Saved a synthetic Windows-1252 stylesheet through the CLI and opened the result in Chrome 154 on macOS. The computed ::before content is "café".

This handles charset metadata already returned by asset retrieval; it does not add CSS @charset sniffing or change document/frame encoding.

AI assistance: implemented and validated with OpenAI Codex.

@snshn

snshn commented Oct 7, 2026

Copy link
Copy Markdown
Member

Thank you for the PR.

Now, this is a pretty big problem I've been seeing when it comes to both stylesheets and HTML. I believe this commit improves a lot of things, I need to review and test it more, will report here briefly.

@snshn
snshn requested a balanced review from Copilot October 7, 2026 21:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The decoding paths and output metadata are consistent, with focused regression coverage for the affected cases.

0 open findings

What changed in this PR

Preserves stylesheet text by decoding source bytes correctly and marking rewritten CSS data URLs as UTF-8.

Changes:

  • Centralizes stylesheet decoding for linked and imported CSS.
  • Emits rewritten stylesheet data URLs with UTF-8 metadata.
  • Adds regression coverage for multiple encodings and import syntaxes.
File Description
src/​css.rs Adds shared decoding and UTF-8 rewriting.
src/​html.rs Uses shared decoding for linked stylesheets.
tests/​css/​embed_css.rs Tests encoded CSS imports.
tests/​html/​walk.rs Tests encoded linked stylesheets.
tests/​cli/​basic.rs Updates UTF-8 data URL expectations.
tests/​cli/​local_files.rs Updates local stylesheet expectations.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@snshn

snshn commented Oct 7, 2026

Copy link
Copy Markdown
Member

@maximilliangrand could you please rebase it when you get a minute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants