Skip to content

Improve WebResponse charset handling - #748

Merged
rbri merged 12 commits into
HtmlUnit:masterfrom
duonglaiquang:duong_encoding
Mar 24, 2024
Merged

rbri merged 12 commits into
HtmlUnit:masterfrom
duonglaiquang:duong_encoding

Conversation

@duonglaiquang

Copy link
Copy Markdown
Contributor

This PR does the following

  • Do label conversions inside EncodingSniffer.toCharset().
  • Fixes the priority of BOM by moving it to the highest priority charset to be in line with html/xml/js/css etc. specifications.
  • Add WebRequest.defaultResponseContentCharset_ to be used to set the charset for the WebResponse when no charset is found. Also deprecates WebResponse.defaultUtf8().
  • Update the default charset of css/js to UTF-8.
  • Fix <iframe> so that the parent charset is used when no charset could otherwise be found.
  • Change the fallback charset of WebResponse from ISO-8859-1 to UTF-8 to be in line with the latest standards.
  • Change the number of prescan bytes of an HTML document from 4096 to 1024 bytes to be in line with specs.
  • Treat x-user-defined charset declaration in meta tag as windows-1252 as documented in specs.
  • Add support for CSS @charset declaration.
  • Change WebResponse.getContentCharset() so that it "just works" for most cases.
    • Move the BOM and content-type header charset reading code from EncodingSniffer to WebResponse so that WebResponse has first-hand knowledge of these concepts.
    • Deprecate the hard to understand getContentCharsetOrNull() and instead add wasContentCharsetTentative() used to check if the charset returned by getContentCharset() was "tenatative".
    • Add method WebResponse.getHeaderContentCharset() to cover the limited use-case that getContentCharsetOrNull() sort of handled.

This commit fixes an issue where WebResponse.getCharset() fails to
correctly sniff certain charsets in content-type meta tag due to
missing support for charset aliases in EncodingSniffer.toCharset().
This commit fixes the priority of the BOM charset and moves it to the
highest level to be in line with html/js/css etc. specs.
This commit deprecates WebResponse.defaultCharsetUtf8() and adds a
more flexible WebRequest.setDefaultResponseContentCharset() that will
be used in the proceeding refactorization commits.
This commit changes the default charset of JavaScript to utf-8 to be in line
with official specs.
This commit changes the default charset of CSS to utf-8 to be in line
with official specs.
This commit changes the prescan length of HTML from 4096 bytes to 1024
to behave similarly to modern browsers.
This commit changes iframes to be more in line with specs by changing
it to use the container document's charset as the fallback charset
when a charset is not specified by the iframe document.
This commit moves the BOM and content-type header charset reading code
to inside WebResponse.getContentCharsetOrNull() rather than
EncodingSniffer since it is common code, and for better flexibility in
the proceeding feature commits.
This commit remove the hard to understand getContentCharsetOrNull() and
instead adds wasContentCharsetTentative() used to check if the charset
returned by getContentCharset() was tenatative.

This commit also adds getHeaderContentCharset() as a utility method.
@duonglaiquang

Copy link
Copy Markdown
Contributor Author

@rbri I created the PR again

@rbri

rbri commented Mar 16, 2024

Copy link
Copy Markdown
Member

Thanks...

@rbri
rbri merged commit 591e833 into HtmlUnit:master Mar 24, 2024
@rbri

rbri commented Mar 24, 2024 •

Copy link
Copy Markdown
Member

Finally many thanks for that.
I did the merge in several steps to no break ans tests (at least no tests backed by real browser). Therefore the changes are not 100% compatible with your ones.

@duonglaiquang please have a look at the latest code, maybe there are still some problems for you that we have to fix.
There is at least one failing test case (org.htmlunit.WebClient3Test.encodingCharsetGB2312GBKChar()) - maybe you can also have a look...

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.

2 participants