LibWeb: Factor out attribute serialization into a separate function

This commit is contained in:
Andreas Kling 2024-04-09 14:46:21 +02:00 committed by Tim Flynn
parent eefd5edc84
commit 0412e17bac
Notes: sideshowbarker 2024-07-17 04:34:25 +09:00

View file

@ -4244,33 +4244,13 @@ JS::NonnullGCPtr<HTMLParser> HTMLParser::create(DOM::Document& document, StringV
return document.heap().allocate_without_realm<HTMLParser>(document, input, encoding); return document.heap().allocate_without_realm<HTMLParser>(document, input, encoding);
} }
// https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-serialisation-algorithm
String HTMLParser::serialize_html_fragment(DOM::Node const& node)
{
// The algorithm takes as input a DOM Element, Document, or DocumentFragment referred to as the node.
VERIFY(node.is_element() || node.is_document() || node.is_document_fragment());
JS::NonnullGCPtr<DOM::Node const> actual_node = node;
if (is<DOM::Element>(node)) {
auto const& element = verify_cast<DOM::Element>(node);
// 1. If the node serializes as void, then return the empty string.
// (NOTE: serializes as void is defined only on elements in the spec)
if (element.serializes_as_void())
return String {};
// 3. If the node is a template element, then let the node instead be the template element's template contents (a DocumentFragment node).
// (NOTE: This is out of order of the spec to avoid another dynamic cast. The second step just creates a string builder, so it shouldn't matter)
if (is<HTML::HTMLTemplateElement>(element))
actual_node = verify_cast<HTML::HTMLTemplateElement>(element).content();
}
enum class AttributeMode { enum class AttributeMode {
No, No,
Yes, Yes,
}; };
auto escape_string = [](StringView string, AttributeMode attribute_mode) -> String { static String escape_string(StringView string, AttributeMode attribute_mode)
{
// https://html.spec.whatwg.org/multipage/parsing.html#escapingString // https://html.spec.whatwg.org/multipage/parsing.html#escapingString
StringBuilder builder; StringBuilder builder;
for (auto code_point : Utf8View { string }) { for (auto code_point : Utf8View { string }) {
@ -4291,8 +4271,29 @@ String HTMLParser::serialize_html_fragment(DOM::Node const& node)
else else
builder.append_code_point(code_point); builder.append_code_point(code_point);
} }
return MUST(builder.to_string()); 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)
{
// The algorithm takes as input a DOM Element, Document, or DocumentFragment referred to as the node.
VERIFY(node.is_element() || node.is_document() || node.is_document_fragment());
JS::NonnullGCPtr<DOM::Node const> actual_node = node;
if (is<DOM::Element>(node)) {
auto const& element = verify_cast<DOM::Element>(node);
// 1. If the node serializes as void, then return the empty string.
// (NOTE: serializes as void is defined only on elements in the spec)
if (element.serializes_as_void())
return String {};
// 3. If the node is a template element, then let the node instead be the template element's template contents (a DocumentFragment node).
// (NOTE: This is out of order of the spec to avoid another dynamic cast. The second step just creates a string builder, so it shouldn't matter)
if (is<HTML::HTMLTemplateElement>(element))
actual_node = verify_cast<HTML::HTMLTemplateElement>(element).content();
}
// 2. Let s be a string, and initialize it to the empty string. // 2. Let s be a string, and initialize it to the empty string.
StringBuilder builder; StringBuilder builder;