mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibWeb: Include element attributes in innerHTML getter
This commit is contained in:
parent
f36feb42bd
commit
4833f0066e
Notes:
sideshowbarker
2024-07-19 00:46:45 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/4833f0066e3 Pull-request: https://github.com/SerenityOS/serenity/pull/4442 Reviewed-by: https://github.com/awesomekling
1 changed files with 31 additions and 2 deletions
|
@ -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("<");
|
||||
else if (ch == '>' && !attribute_mode)
|
||||
builder.append(">");
|
||||
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()) {
|
||||
|
|
Loading…
Add table
Reference in a new issue