LibWeb: Support for Content-Language HTTP header

This commit is contained in:
Piotr 2025-01-14 20:31:44 +01:00 committed by Tim Ledbetter
parent dda1573746
commit c9edb6ffc4
Notes: github-actions[bot] 2025-02-19 10:54:32 +00:00
5 changed files with 51 additions and 0 deletions

View file

@ -369,6 +369,12 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type type
if (auto maybe_last_modified = navigation_params.response->header_list()->get("Last-Modified"sv.bytes()); maybe_last_modified.has_value())
document->m_last_modified = Core::DateTime::parse("%a, %d %b %Y %H:%M:%S %Z"sv, maybe_last_modified.value());
// NOTE: Non-standard: Pull out the Content-Language header to determine the document's language.
if (auto maybe_http_content_language = navigation_params.response->header_list()->get("Content-Language"sv.bytes()); maybe_http_content_language.has_value()) {
if (auto maybe_content_language = String::from_utf8(maybe_http_content_language.value()); !maybe_content_language.is_error())
document->m_http_content_language = maybe_content_language.release_value();
}
// 10. Set window's associated Document to document.
window->set_associated_document(*document);

View file

@ -397,6 +397,7 @@ public:
Optional<String> const& pragma_set_default_language() const { return m_pragma_set_default_language; }
void set_pragma_set_default_language(String language) { m_pragma_set_default_language = move(language); }
Optional<String> const& http_content_language() const { return m_http_content_language; }
bool has_encoding() const { return m_encoding.has_value(); }
Optional<String> const& encoding() const { return m_encoding; }
@ -923,6 +924,7 @@ private:
HTML::DocumentReadyState m_readiness { HTML::DocumentReadyState::Complete };
String m_content_type { "application/xml"_string };
Optional<String> m_pragma_set_default_language;
Optional<String> m_http_content_language;
Optional<String> m_encoding;
bool m_ready_for_post_load_tasks { false };

View file

@ -3670,6 +3670,10 @@ Optional<String> Element::lang() const
}
// - If there is no pragma-set default language set, then language information from a higher-level protocol (such as HTTP),
if (document().http_content_language().has_value()) {
return document().http_content_language();
}
// if any, must be used as the final fallback language instead.
// - In the absence of any such language information, and in cases where the higher-level protocol reports multiple languages,
// the language of the node is unknown, and the corresponding language tag is the empty string.

View file

@ -0,0 +1 @@
OK

View file

@ -0,0 +1,38 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
asyncTest(async (done) => {
const httpServer = httpTestServer();
const url = await httpServer.createEcho("GET", "/http-content-language-test", {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Content-Language",
"Content-Language": "ko",
},
body: `
<style>
div { width: 50px; }
#box:lang(ko) { width: 100px; }
</style>
<body><div id="box">TEST</div></body>
<script>
if (document.getElementById("box").offsetWidth == 100) {
parent.postMessage("OK", "*")
} else {
parent.postMessage("FAIL", "*")
}
<\/script>`,
});
const frame = document.createElement('iframe');
frame.src = url;
addEventListener("message", (event) => {
println(event.data);
done();
}, false);
document.body.appendChild(frame);
});
</script>