mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:47:36 +00:00
LibWeb: Add some helpers to the DOM Node class
This patch adds the following things needed by the HTML spec: - Node::child_text_content() - Node::is_connected() - Node::root()
This commit is contained in:
parent
6c409310a8
commit
128eaf9295
2 changed files with 31 additions and 0 deletions
|
@ -155,4 +155,30 @@ void Node::dispatch_event(NonnullRefPtr<Event> event)
|
||||||
parent()->dispatch_event(move(event));
|
parent()->dispatch_event(move(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String Node::child_text_content() const
|
||||||
|
{
|
||||||
|
if (!is<ParentNode>(*this))
|
||||||
|
return String::empty();
|
||||||
|
|
||||||
|
StringBuilder builder;
|
||||||
|
to<ParentNode>(*this).for_each_child([&](auto& child) {
|
||||||
|
if (is<Text>(child))
|
||||||
|
builder.append(to<Text>(child).text_content());
|
||||||
|
});
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node* Node::root() const
|
||||||
|
{
|
||||||
|
const Node* root = this;
|
||||||
|
while (root->parent())
|
||||||
|
root = root->parent();
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Node::is_connected() const
|
||||||
|
{
|
||||||
|
return root() && root()->is_document();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,8 +94,13 @@ public:
|
||||||
const HTMLAnchorElement* enclosing_link_element() const;
|
const HTMLAnchorElement* enclosing_link_element() const;
|
||||||
const HTMLElement* enclosing_html_element() const;
|
const HTMLElement* enclosing_html_element() const;
|
||||||
|
|
||||||
|
String child_text_content() const;
|
||||||
|
|
||||||
virtual bool is_html_element() const { return false; }
|
virtual bool is_html_element() const { return false; }
|
||||||
|
|
||||||
|
const Node* root() const;
|
||||||
|
bool is_connected() const;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
const T* first_child_of_type() const;
|
const T* first_child_of_type() const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue