Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/Parser/Helpers.cpp (C++)

Code before:
}

RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingParams const& context, StringView string)
{
    if (string.is_empty())
        return {};
    return CSS::Parser::Parser::create(context, string).parse_as_supports();
}

Vector<CSS::Parser::ComponentValue> parse_component_values_list(CSS::Parser::ParsingParams const& parsing_params, StringView string)
{
    return CSS::Parser::Parser::create(parsing_params, string).parse_as_list_of_component_values();
}

// https://drafts.csswg.org/css-syntax/#css-decode-bytes
ErrorOr<String> css_decode_bytes(Optional<StringView> const& environment_encoding, Optional<String> mime_type_charset, ByteBuffer const& encoded_string)
{
    // https://drafts.csswg.org/css-syntax/#determine-the-fallback-encoding
    auto determine_the_fallback_encoding = [&mime_type_charset, &environment_encoding, &encoded_string]() -> StringView {
        // 1. If HTTP or equivalent protocol provides an encoding label (e.g. via the charset parameter of the Content-Type header) for the stylesheet,
        //    get an encoding from encoding label. If that does not return failure, return it.
        if (mime_type_charset.has_value()) {
            if (auto encoding = TextCodec::get_standardized_encoding(mime_type_charset.value()); encoding.has_value())
                return encoding.value();
        }
        // 2. Otherwise, check stylesheet’s byte stream. If the first 1024 bytes of the stream begin with the hex sequence
        // 40 63 68 61 72 73 65 74 20 22 XX* 22 3B
        // where each XX byte is a value between 0x16 and 0x21 inclusive or a value between 0x23 and 0x7F inclusive, then get an encoding from a string formed out of the sequence of XX bytes, interpreted as ASCII.
        auto check_stylesheets_byte_stream = [&encoded_string]() -> Optional<StringView> {
            size_t scan_length = min(encoded_string.size(), 1024);
            auto pattern_start = "@charset \""sv;
            auto pattern_end = "\";"sv;

            if (scan_length < pattern_start.length())
                return {};

            StringView start_view(encoded_string.data(), pattern_start.length());
            if (start_view != pattern_start)
                return {};

            auto encoding_start = pattern_start.length();
            StringView buffer_view(encoded_string.data(), scan_length);
            auto end_index = buffer_view.find(pattern_end, encoding_start);
            if (!end_index.has_value())
                return {};

            size_t encoding_length = end_index.value() - encoding_start;
            auto encoding_view = buffer_view.substring_view(encoding_start, encoding_length);

            for (char c : encoding_view) {
                u8 byte = static_cast<u8>(c);

Review comment (bug):
This apostrophe has ended up encoded wrong (which is ironic considering this is an encoding algorithm!). Please correct that, and also wrap the spec comments so no lines are longer than 120 columns. ( https://github.com/LadybirdBrowser/ladybird/blob/master/Documentation/CodingStyle.md#comments )

Code after:
}

RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingParams const& context, StringView string)
{
    if (string.is_empty())
        return {};
    return CSS::Parser::Parser::create(context, string).parse_as_supports();
}

Vector<CSS::Parser::ComponentValue> parse_component_values_list(CSS::Parser::ParsingParams const& parsing_params, StringView string)
{
    return CSS::Parser::Parser::create(parsing_params, string).parse_as_list_of_component_values();
}

// https://drafts.csswg.org/css-syntax/#css-decode-bytes
ErrorOr<String> css_decode_bytes(Optional<StringView> const& environment_encoding, Optional<String> mime_type_charset, ByteBuffer const& encoded_string)
{
    // https://drafts.csswg.org/css-syntax/#determine-the-fallback-encoding
    auto determine_the_fallback_encoding = [&mime_type_charset, &environment_encoding, &encoded_string]() -> StringView {
        // 1. If HTTP or equivalent protocol provides an encoding label (e.g. via the charset parameter of the Content-Type header) for the stylesheet,
        //    get an encoding from encoding label. If that does not return failure, return it.
        if (mime_type_charset.has_value()) {
            if (auto encoding = TextCodec::get_standardized_encoding(mime_type_charset.value()); encoding.has_value())
                return encoding.value();
        }
        // 2. Otherwise, check stylesheet’s byte stream. If the first 1024 bytes of the stream begin with the hex sequence
        // 40 63 68 61 72 73 65 74 20 22 XX* 22 3B
        // where each XX byte is a value between 0x16 and 0x21 inclusive or a value between 0x23 and 0x7F inclusive,
        // then get an encoding from a string formed out of the sequence of XX bytes, interpreted as ASCII.
        auto check_stylesheets_byte_stream = [&encoded_string]() -> Optional<StringView> {
            size_t scan_length = min(encoded_string.size(), 1024);
            auto pattern_start = "@charset \""sv;
            auto pattern_end = "\";"sv;

            if (scan_length < pattern_start.length())
                return {};

            StringView buffer_view = encoded_string.bytes().slice(0, scan_length);
            if (!buffer_view.starts_with(pattern_start))
                return {};

            auto encoding_start = pattern_start.length();
            auto end_index = buffer_view.find(pattern_end, encoding_start);
            if (!end_index.has_value())
                return {};

            size_t encoding_length = end_index.value() - encoding_start;
            auto encoding_view = buffer_view.substring_view(encoding_start, encoding_length);

            for (char c : encoding_view) {
                u8 byte = static_cast<u8>(c);