mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:57:35 +00:00
LibWeb: Include element attributes in innerHTML getter
This commit is contained in:
parent
f36feb42bd
commit
4833f0066e
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
|
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;
|
StringBuilder builder;
|
||||||
|
|
||||||
Function<void(const Node&)> recurse = [&](auto& node) {
|
Function<void(const Node&)> recurse = [&](auto& node) {
|
||||||
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
|
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
|
||||||
if (child->is_element()) {
|
if (child->is_element()) {
|
||||||
|
auto& element = downcast<Element>(*child);
|
||||||
builder.append('<');
|
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('>');
|
builder.append('>');
|
||||||
|
|
||||||
recurse(*child);
|
recurse(*child);
|
||||||
|
|
||||||
|
// FIXME: This should be skipped for void elements
|
||||||
builder.append("</");
|
builder.append("</");
|
||||||
builder.append(downcast<Element>(*child).local_name());
|
builder.append(element.local_name());
|
||||||
builder.append('>');
|
builder.append('>');
|
||||||
}
|
}
|
||||||
if (child->is_text()) {
|
if (child->is_text()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue