Decode responses with the declared charset to fix unicode titles (#2) - #8
Open
kochj23 wants to merge 1 commit into
Open
Decode responses with the declared charset to fix unicode titles (#2)#8kochj23 wants to merge 1 commit into
kochj23 wants to merge 1 commit into
Conversation
The switch to lxml regressed non-ascii page titles: raw response bytes were handed straight to lxml.html.fromstring(), which assumes latin-1 when no encoding is known. A utf-8 page like GitHub then rendered as "GitHub · Social Coding" instead of "GitHub · Social Coding". Decode the response body using the charset declared in the Content-Type header before parsing, via a new charset_from_contenttype() helper. Unknown/garbage charsets fall through untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Closes #2
Problem
The switch to lxml regressed non-ascii page titles. Raw response bytes were handed straight to
lxml.html.fromstring(), which assumes latin-1 when it isn't told the encoding. A utf-8 page such as GitHub then came back asTitle: [GitHub · Social Coding]instead ofGitHub · Social Coding(the·, U+00B7, is0xC2 0xB7in utf-8, which decodes to·under latin-1).Fix
In
plugin.py, decode the response body using the charset declared in theContent-Typeheader before it reaches the HTML parsers. A newcharset_from_contenttype()helper extracts the charset (text/html; charset=utf-8->utf-8,text/html->None), and unknown/garbage charsets fall through untouched so nothing regresses.Testing — please read
I could not execute the test suite locally. This is a Python 2 + Supybot plugin, and this environment has only Python 3 (no
python2) and no Supybot runtime, sosupybot.test/PluginTestCasecannot be imported or run here. I did not run the suite and am not claiming a passing run.What I did instead:
fetch_url()flow and the mojibake mechanics above.DetrollTestCasestub with a realtestCharsetFromContentTypemethod that asserts the charset extraction (including quoted and parameterised forms) and the utf-8 decode round-trip that produces the correct·. Its logic is verified by inspection; it should run under a real Py2/Supybot setup.🤖 Generated with Claude Code