mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:27:43 +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:
parent
93230386f7
commit
0c6af2d5b4
3 changed files with 24 additions and 4 deletions
|
@ -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/Element.h>
|
||||||
#include <LibHTML/DOM/HTMLAnchorElement.h>
|
#include <LibHTML/DOM/HTMLAnchorElement.h>
|
||||||
#include <LibHTML/CSS/StyleResolver.h>
|
#include <LibHTML/DOM/Node.h>
|
||||||
#include <LibHTML/Layout/LayoutNode.h>
|
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
#include <LibHTML/Layout/LayoutDocument.h>
|
#include <LibHTML/Layout/LayoutDocument.h>
|
||||||
#include <LibHTML/Layout/LayoutInline.h>
|
#include <LibHTML/Layout/LayoutInline.h>
|
||||||
|
#include <LibHTML/Layout/LayoutNode.h>
|
||||||
#include <LibHTML/Layout/LayoutText.h>
|
#include <LibHTML/Layout/LayoutText.h>
|
||||||
|
|
||||||
Node::Node(Document& document, NodeType type)
|
Node::Node(Document& document, NodeType type)
|
||||||
|
@ -39,7 +40,6 @@ RefPtr<LayoutNode> Node::create_layout_node(const StyleResolver& resolver, const
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
|
RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
|
||||||
{
|
{
|
||||||
auto layout_node = create_layout_node(resolver, parent_properties);
|
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 static_cast<const HTMLElement*>(this);
|
||||||
return parent() ? parent()->enclosing_html_element() : nullptr;
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -36,6 +36,8 @@ public:
|
||||||
|
|
||||||
virtual String tag_name() const = 0;
|
virtual String tag_name() const = 0;
|
||||||
|
|
||||||
|
virtual String text_content() const;
|
||||||
|
|
||||||
Document& document() { return m_document; }
|
Document& document() { return m_document; }
|
||||||
const Document& document() const { return m_document; }
|
const Document& document() const { return m_document; }
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ public:
|
||||||
|
|
||||||
virtual String tag_name() const override { return "#text"; }
|
virtual String tag_name() const override { return "#text"; }
|
||||||
|
|
||||||
|
virtual String text_content() const override { return m_data; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_data;
|
String m_data;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue