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:
Ángel Carias 2024-07-19 13:57:24 -06:00 committed by Tim Ledbetter
commit 69da6a0ce4
Notes: github-actions[bot] 2024-07-20 17:03:45 +00:00
5 changed files with 76 additions and 1 deletions

View file

@ -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, nodes previous sibling Text node, if any, and its contiguous
// Text nodes, and nodes 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());
}
}