diff --git a/Tests/LibWeb/Text/expected/css/import-rule-shift-jis.txt b/Tests/LibWeb/Text/expected/css/import-rule-shift-jis.txt new file mode 100644 index 00000000000..aaecaf93c4a --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/import-rule-shift-jis.txt @@ -0,0 +1 @@ +PASS (didn't crash) diff --git a/Tests/LibWeb/Text/input/css/import-rule-shift-jis.html b/Tests/LibWeb/Text/input/css/import-rule-shift-jis.html new file mode 100644 index 00000000000..6a30019f081 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/import-rule-shift-jis.html @@ -0,0 +1,9 @@ + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp index 18622a4bc23..c8f1b1b5635 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp @@ -1,12 +1,13 @@ /* * Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, Sam Atkins - * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2022-2024, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include +#include #include #include #include @@ -97,7 +98,23 @@ void CSSImportRule::resource_did_load() dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url()); } - auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data(), resource()->url()); + // FIXME: The fallback here should be the `environment encoding` of the importing style sheet. + auto encoding = resource()->encoding().value_or("utf-8"); + auto maybe_decoder = TextCodec::decoder_for(encoding); + if (!maybe_decoder.has_value()) { + dbgln("CSSImportRule: Failed to decode CSS file: {} Unsupported encoding: {}", resource()->url(), encoding); + return; + } + auto& decoder = maybe_decoder.release_value(); + + auto decoded_or_error = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(decoder, resource()->encoded_data()); + if (decoded_or_error.is_error()) { + dbgln("CSSImportRule: Failed to decode CSS file: {} Encoding was: {}", resource()->url(), encoding); + return; + } + auto decoded = decoded_or_error.release_value(); + + auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), decoded, resource()->url()); if (!sheet) { dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url()); return;