LibWeb: Include element attributes in innerHTML getter

This commit is contained in:
Linus Groh 2020-12-17 13:37:40 +00:00 committed by Andreas Kling
parent f36feb42bd
commit 4833f0066e
Notes: sideshowbarker 2024-07-19 00:46:45 +09:00

View file

@ -271,19 +271,48 @@ void Element::set_inner_html(StringView markup)
String Element::inner_html() const
{
auto escape_string = [](const StringView& string, bool attribute_mode) -> String {
// https://html.spec.whatwg.org/multipage/parsing.html#escapingString
StringBuilder builder;
for (auto& ch : string) {
if (ch == '&')
builder.append("&");
// FIXME: also replace U+00A0 NO-BREAK SPACE with  
else if (ch == '"' && attribute_mode)
builder.append(""");
else if (ch == '<' && !attribute_mode)
builder.append("&lt;");
else if (ch == '>' && !attribute_mode)
builder.append("&gt;");
else
builder.append(ch);
}
return builder.to_string();
};
StringBuilder builder;
Function<void(const Node&)> recurse = [&](auto& node) {
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
if (child->is_element()) {
auto& element = downcast<Element>(*child);
builder.append('<');
builder.append(downcast<Element>(*child).local_name());
builder.append(element.local_name());
element.for_each_attribute([&](auto& name, auto& value) {
builder.append(' ');
builder.append(name);
builder.append('=');
builder.append('"');
builder.append(escape_string(value, true));
builder.append('"');
});
builder.append('>');
recurse(*child);
// FIXME: This should be skipped for void elements
builder.append("</");
builder.append(downcast<Element>(*child).local_name());
builder.append(element.local_name());
builder.append('>');
}
if (child->is_text()) {