Answer the charset declaration question from <head> - #87
Merged
Conversation
fixCharsetMetas set declared from a <meta charset> anywhere in the tree, so a page whose only charset meta sits in <body>, which is the malformed markup case the walk exists to handle, came out of sanitize with no declaration in <head> at all. Readers pre-scan the first 1024 bytes for the encoding, so the declaration was never found and every multibyte character mojibaked, which is the failure issue #16 reports. Rewriting stale values stays whole-document. Only the declared question moves back to <head>, so a stray body meta is rewritten to utf-8 and <head> still gets its own declaration first. rewriteContentTypeCharset also starts at the first field rather than the second, so content="charset=iso-8859-1" with no media type is rewritten instead of being left to contradict the injected declaration. A real media type has no equals sign, so the existing Cut rejects it. Follow-up to #81.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #81.
fixCharsetMetassetsdeclaredfrom a<meta charset>anywhere in the tree, andensureCharsetthen returns early. So a page whose only charset meta sits in<body>, which is exactly the malformed-markup case the tree-wide walk was added to handle, comes out of sanitize with no declaration in<head>at all.Same input against both trees, body padded past the prescan window:
Browsers and ZIM readers determine the encoding by pre-scanning the first 1024 bytes. A declaration at byte 3247 is not found, the reader falls back to its locale encoding, and every multibyte character mojibakes. That is the failure #16 reports and the one
ensureCharsetexists to prevent.The fix keeps the two questions apart. Rewriting stale values stays whole-document, since a declaration in
<body>still contradicts the bytes on disk. Only "does the document declare an encoding" moves back to<head>, viaheadDeclaresCharset, which also skips inert<template>content. A stray body meta is now rewritten toutf-8and<head>gets its own declaration first: two occurrences, no contradiction, correct prescan.Also folded in the one-character bound from the same review:
rewriteContentTypeCharsetstarted atparts[1], socontent="charset=iso-8859-1"with no media type was never rewritten and survived alongside the injected UTF-8 declaration. A real media type has no=, so the existingCutrejects it without the offset.TestCharsetRewritesNonUTF8case 3 previously asserted the buggy shape (CharsetAdded == false, one charset). It now covers the no-media-type Content-Type instead, and the body case moves toTestCharsetInBodyStillDeclaresInHead, which asserts both metas are present, neither is stale, and the first one lands inside the 1024-byte window.