mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 01:29:17 +00:00
LibWeb: Implement Text.wholeText
This getter returns the concatenation of the data of the contiguous Text nodes of `this` (being this plus its siblings) in tree order.
This commit is contained in:
parent
7dacd6be89
commit
69da6a0ce4
Notes:
github-actions[bot]
2024-07-20 17:03:45 +00:00
Author: https://github.com/aescarias 🔰
Commit: 69da6a0ce4
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/725
Reviewed-by: https://github.com/tcl3 ✅
5 changed files with 76 additions and 1 deletions
|
@ -117,4 +117,36 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Text>> Text::split_text(size_t offset)
|
|||
return new_node;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-text-wholetext
|
||||
String Text::whole_text()
|
||||
{
|
||||
// https://dom.spec.whatwg.org/#contiguous-text-nodes
|
||||
// The contiguous Text nodes of a node node are node, node’s previous sibling Text node, if any, and its contiguous
|
||||
// Text nodes, and node’s next sibling Text node, if any, and its contiguous Text nodes, avoiding any duplicates.
|
||||
Vector<Text*> nodes;
|
||||
|
||||
nodes.append(this);
|
||||
|
||||
auto* current_node = previous_sibling();
|
||||
while (current_node && (current_node->is_text() || current_node->is_cdata_section())) {
|
||||
nodes.append(static_cast<Text*>(current_node));
|
||||
current_node = current_node->previous_sibling();
|
||||
}
|
||||
|
||||
// Reverse nodes so they are in tree order
|
||||
nodes.reverse();
|
||||
|
||||
current_node = next_sibling();
|
||||
while (current_node && (current_node->is_text() || current_node->is_cdata_section())) {
|
||||
nodes.append(static_cast<Text*>(current_node));
|
||||
current_node = current_node->next_sibling();
|
||||
}
|
||||
|
||||
StringBuilder builder;
|
||||
for (auto const& text_node : nodes)
|
||||
builder.append(text_node->data());
|
||||
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue