1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 22:02:12 +00:00

LibHTML: Add Node::text_content()

This returns a String built from all of a Node's text descendants,
including itself.
This commit is contained in:
Andreas Kling 2019-09-29 16:22:15 +02:00
parent 93230386f7
commit 0c6af2d5b4
3 changed files with 24 additions and 4 deletions

View file

@ -1,11 +1,12 @@
#include <LibHTML/DOM/Node.h>
#include <AK/StringBuilder.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/HTMLAnchorElement.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/DOM/Node.h>
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutDocument.h>
#include <LibHTML/Layout/LayoutInline.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/Layout/LayoutText.h>
Node::Node(Document& document, NodeType type)
@ -39,7 +40,6 @@ RefPtr<LayoutNode> Node::create_layout_node(const StyleResolver& resolver, const
ASSERT_NOT_REACHED();
}
RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
{
auto layout_node = create_layout_node(resolver, parent_properties);
@ -88,3 +88,19 @@ const HTMLElement* Node::enclosing_html_element() const
return static_cast<const HTMLElement*>(this);
return parent() ? parent()->enclosing_html_element() : nullptr;
}
String Node::text_content() const
{
Vector<String> strings;
StringBuilder builder;
for (auto* child = first_child(); child; child = child->next_sibling()) {
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();
}