1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

LibWeb: Add Node.textContent

This requires moving remove_all_children() from ParentNode to
Node, which makes ParentNode.cpp empty, so remove it.

It also co-opts the existing Node::text_content() method and
tweaks it slightly to fit the semantics of Node.textContent.
This commit is contained in:
Nico Weber 2020-08-17 12:36:00 -04:00 committed by Andreas Kling
parent c0c7b4a098
commit e9b56b5b9c
7 changed files with 44 additions and 44 deletions

View file

@ -84,14 +84,25 @@ String Node::text_content() const
auto text = child->text_content();
if (!text.is_empty()) {
builder.append(child->text_content());
builder.append(' ');
}
}
if (builder.length() > 1)
builder.trim(1);
return builder.to_string();
}
void Node::set_text_content(const String& content)
{
if (is_text()) {
downcast<Text>(this)->set_data(content);
} else {
remove_all_children();
append_child(document().create_text_node(content));
}
set_needs_style_update(true);
document().schedule_style_update();
document().invalidate_layout();
}
RefPtr<LayoutNode> Node::create_layout_node(const CSS::StyleProperties*)
{
return nullptr;
@ -197,6 +208,13 @@ RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, b
return node;
}
void Node::remove_all_children()
{
while (RefPtr<Node> child = first_child()) {
remove_child(*child);
}
}
void Node::set_document(Badge<Document>, Document& document)
{
m_document = &document;