mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibWeb: Factor out attribute serialization into a separate function
This commit is contained in:
parent
eefd5edc84
commit
0412e17bac
Notes:
sideshowbarker
2024-07-17 04:34:25 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0412e17bac Pull-request: https://github.com/SerenityOS/serenity/pull/23905 Reviewed-by: https://github.com/trflynn89
1 changed files with 30 additions and 29 deletions
|
@ -4244,6 +4244,36 @@ JS::NonnullGCPtr<HTMLParser> HTMLParser::create(DOM::Document& document, StringV
|
|||
return document.heap().allocate_without_realm<HTMLParser>(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<HTML::HTMLTemplateElement>(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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue