diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index a3143c746ff..0aa11a26ff4 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -4244,6 +4244,36 @@ JS::NonnullGCPtr HTMLParser::create(DOM::Document& document, StringV return document.heap().allocate_without_realm(document, input, encoding); } +enum class AttributeMode { + No, + Yes, +}; + +static String escape_string(StringView string, AttributeMode attribute_mode) +{ + // https://html.spec.whatwg.org/multipage/parsing.html#escapingString + StringBuilder builder; + for (auto code_point : Utf8View { string }) { + // 1. Replace any occurrence of the "&" character by the string "&". + if (code_point == '&') + builder.append("&"sv); + // 2. Replace any occurrences of the U+00A0 NO-BREAK SPACE character by the string " ". + else if (code_point == 0xA0) + builder.append(" "sv); + // 3. If the algorithm was invoked in the attribute mode, replace any occurrences of the """ character by the string """. + else if (code_point == '"' && attribute_mode == AttributeMode::Yes) + builder.append("""sv); + // 4. If the algorithm was not invoked in the attribute mode, replace any occurrences of the "<" character by the string "<", and any occurrences of the ">" character by the string ">". + else if (code_point == '<' && attribute_mode == AttributeMode::No) + builder.append("<"sv); + else if (code_point == '>' && attribute_mode == AttributeMode::No) + builder.append(">"sv); + else + builder.append_code_point(code_point); + } + return builder.to_string_without_validation(); +} + // https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-serialisation-algorithm String HTMLParser::serialize_html_fragment(DOM::Node const& node) { @@ -4265,35 +4295,6 @@ String HTMLParser::serialize_html_fragment(DOM::Node const& node) actual_node = verify_cast(element).content(); } - enum class AttributeMode { - No, - Yes, - }; - - auto escape_string = [](StringView string, AttributeMode attribute_mode) -> String { - // https://html.spec.whatwg.org/multipage/parsing.html#escapingString - StringBuilder builder; - for (auto code_point : Utf8View { string }) { - // 1. Replace any occurrence of the "&" character by the string "&". - if (code_point == '&') - builder.append("&"sv); - // 2. Replace any occurrences of the U+00A0 NO-BREAK SPACE character by the string " ". - else if (code_point == 0xA0) - builder.append(" "sv); - // 3. If the algorithm was invoked in the attribute mode, replace any occurrences of the """ character by the string """. - else if (code_point == '"' && attribute_mode == AttributeMode::Yes) - builder.append("""sv); - // 4. If the algorithm was not invoked in the attribute mode, replace any occurrences of the "<" character by the string "<", and any occurrences of the ">" character by the string ">". - else if (code_point == '<' && attribute_mode == AttributeMode::No) - builder.append("<"sv); - else if (code_point == '>' && attribute_mode == AttributeMode::No) - builder.append(">"sv); - else - builder.append_code_point(code_point); - } - return MUST(builder.to_string()); - }; - // 2. Let s be a string, and initialize it to the empty string. StringBuilder builder;