Skip to content

Truly independent web browser

LibTextCodec: Remove only the sniffed BOM when decoding

Shannon Boothd8d081698266
diff --git a/Libraries/LibTextCodec/Decoder.cpp b/Libraries/LibTextCodec/Decoder.cpp
index 72ae91ed651..82885e40566 100644
--- a/Libraries/LibTextCodec/Decoder.cpp
+++ b/Libraries/LibTextCodec/Decoder.cpp
@@ -400,7 +400,7 @@ ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte
// 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
// FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
- auto output = TRY(actual_decoder->to_utf8(input, IgnoreBOM::No, ErrorMode::Replacement));
+ auto output = TRY(actual_decoder->to_utf8(input, IgnoreBOM::Yes, ErrorMode::Replacement));
// 4. Return output.
return output;
@@ -557,12 +557,12 @@ ErrorOr<Utf16String> UTF8Decoder::to_utf16(StringView input)
ErrorOr<size_t> UTF8Decoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-8"sv, input, IgnoreBOM::No);
+ return rust_length_in_utf16_code_units("UTF-8"sv, input, IgnoreBOM::Yes);
}
ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process("UTF-16BE"sv, input, IgnoreBOM::No, move(on_code_point));
+ return rust_process("UTF-16BE"sv, input, IgnoreBOM::Yes, move(on_code_point));
}
ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
@@ -572,12 +572,12 @@ ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom,
ErrorOr<size_t> UTF16BEDecoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-16BE"sv, input, IgnoreBOM::No);
+ return rust_length_in_utf16_code_units("UTF-16BE"sv, input, IgnoreBOM::Yes);
}
ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
{
- return rust_process("UTF-16LE"sv, input, IgnoreBOM::No, move(on_code_point));
+ return rust_process("UTF-16LE"sv, input, IgnoreBOM::Yes, move(on_code_point));
}
ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom, ErrorMode error_mode)
@@ -587,7 +587,7 @@ ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input, IgnoreBOM ignore_bom,
ErrorOr<size_t> UTF16LEDecoder::length_in_utf16_code_units(StringView input)
{
- return rust_length_in_utf16_code_units("UTF-16LE"sv, input, IgnoreBOM::No);
+ return rust_length_in_utf16_code_units("UTF-16LE"sv, input, IgnoreBOM::Yes);
}
// https://infra.spec.whatwg.org/#isomorphic-decode
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index f3655e1dffb..3564dd0830c 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -1160,7 +1160,7 @@ void fetch_single_module_script(JS::Realm& realm,
if (bytecode.has_value()) {
auto decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
- auto source_length = decoder->length_in_utf16_code_units(StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
+ auto source_length = TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
prepare_bytecode_cache_off_thread(settings_object, *bytecode, JS::ProgramType::Module, source_length, *source_hash,
[url = move(url), url_string = move(url_string), response_url = move(response_url),
module_type_string = move(module_type_string),
diff --git a/Tests/LibTextCodec/TestTextDecoders.cpp b/Tests/LibTextCodec/TestTextDecoders.cpp
index a991926a958..1cbcf85baf0 100644
--- a/Tests/LibTextCodec/TestTextDecoders.cpp
+++ b/Tests/LibTextCodec/TestTextDecoders.cpp
@@ -118,7 +118,7 @@ TEST_CASE(test_utf16be_process_code_points)
{
auto& decoder = decoder_for("UTF-16BE"sv);
- EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xfe, 0xff, 0x00, 'A', 0xd8, 0x3d, 0xde, 0x00 }))), (Vector<u32> { 0x41, 0x1F600 }));
+ EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xfe, 0xff, 0x00, 'A', 0xd8, 0x3d, 0xde, 0x00 }))), (Vector<u32> { 0xfeff, 0x41, 0x1F600 }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xd8, 0x3d, 0x00, 'A', 0xde, 0x00 }))), (Vector<u32> { 0xfffd, 0x41, 0xfffd }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0x00, 'A', 0xff }))), (Vector<u32> { 0x41, 0xfffd }));
EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 0x00, 'A', 0xff })), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "A\xef\xbf\xbd"sv);
@@ -251,8 +251,26 @@ TEST_CASE(test_utf16le_process_code_points)
{
auto& decoder = decoder_for("UTF-16LE"sv);
- EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xff, 0xfe, 'A', 0x00, 0x3d, 0xd8, 0x00, 0xde }))), (Vector<u32> { 0x41, 0x1F600 }));
+ EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0xff, 0xfe, 'A', 0x00, 0x3d, 0xd8, 0x00, 0xde }))), (Vector<u32> { 0xfeff, 0x41, 0x1F600 }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 0x3d, 0xd8, 'A', 0x00, 0x00, 0xde }))), (Vector<u32> { 0xfffd, 0x41, 0xfffd }));
EXPECT_EQ(process_code_points(decoder, StringView(bytes({ 'A', 0x00, 0xff }))), (Vector<u32> { 0x41, 0xfffd }));
EXPECT_EQ(MUST(decoder.to_utf8(StringView(bytes({ 'A', 0x00, 0xff })), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement)), "A\xef\xbf\xbd"sv);
}
+
+TEST_CASE(test_decode_removes_only_the_sniffed_byte_order_mark)
+{
+ auto& fallback_decoder = decoder_for("windows-1252"sv);
+
+ auto inputs = Array {
+ Vector<u8> { 0xef, 0xbb, 0xbf, 0xef, 0xbb, 0xbf, 'A' },
+ Vector<u8> { 0xfe, 0xff, 0xfe, 0xff, 0x00, 'A' },
+ Vector<u8> { 0xff, 0xfe, 0xff, 0xfe, 'A', 0x00 },
+ };
+
+ for (auto const& input_bytes : inputs) {
+ auto input = StringView(bytes(input_bytes));
+ EXPECT_EQ(MUST(TextCodec::convert_input_to_utf16_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), u"\uFEFFA"sv);
+ EXPECT_EQ(MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), "\uFEFFA"sv);
+ EXPECT_EQ(MUST(TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(fallback_decoder, input)), 2u);
+ }
+}