2019-09-29 16:22:15 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibHTML/CSS/StyleResolver.h>
|
2019-09-25 12:17:29 +03:00
|
|
|
#include <LibHTML/DOM/Element.h>
|
2019-09-29 11:59:38 +02:00
|
|
|
#include <LibHTML/DOM/HTMLAnchorElement.h>
|
2019-09-29 16:22:15 +02:00
|
|
|
#include <LibHTML/DOM/Node.h>
|
2019-09-25 12:17:29 +03:00
|
|
|
#include <LibHTML/Layout/LayoutBlock.h>
|
|
|
|
#include <LibHTML/Layout/LayoutDocument.h>
|
|
|
|
#include <LibHTML/Layout/LayoutInline.h>
|
2019-09-29 16:22:15 +02:00
|
|
|
#include <LibHTML/Layout/LayoutNode.h>
|
2019-09-25 12:17:29 +03:00
|
|
|
#include <LibHTML/Layout/LayoutText.h>
|
2019-06-15 18:55:47 +02:00
|
|
|
|
2019-09-29 11:43:07 +02:00
|
|
|
Node::Node(Document& document, NodeType type)
|
|
|
|
: m_document(document)
|
|
|
|
, m_type(type)
|
2019-06-15 18:55:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::~Node()
|
|
|
|
{
|
|
|
|
}
|
2019-09-25 12:17:29 +03:00
|
|
|
|
2019-09-29 11:59:38 +02:00
|
|
|
const HTMLAnchorElement* Node::enclosing_link_element() const
|
|
|
|
{
|
2019-10-19 21:21:29 +02:00
|
|
|
for (auto* node = this; node; node = node->parent()) {
|
|
|
|
if (is<HTMLAnchorElement>(*node) && to<HTMLAnchorElement>(*node).has_attribute("href"))
|
|
|
|
return to<HTMLAnchorElement>(node);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2019-09-29 11:59:38 +02:00
|
|
|
}
|
2019-09-29 12:24:36 +02:00
|
|
|
|
|
|
|
const HTMLElement* Node::enclosing_html_element() const
|
|
|
|
{
|
2019-10-06 21:07:36 +02:00
|
|
|
return first_ancestor_of_type<HTMLElement>();
|
2019-09-29 12:24:36 +02:00
|
|
|
}
|
2019-09-29 16:22:15 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2019-10-06 19:54:50 +02:00
|
|
|
|
|
|
|
const Element* Node::next_element_sibling() const
|
|
|
|
{
|
|
|
|
for (auto* node = next_sibling(); node; node = node->next_sibling()) {
|
|
|
|
if (node->is_element())
|
|
|
|
return static_cast<const Element*>(node);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Element* Node::previous_element_sibling() const
|
|
|
|
{
|
|
|
|
for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
|
|
|
|
if (node->is_element())
|
|
|
|
return static_cast<const Element*>(node);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-10-09 20:17:01 +02:00
|
|
|
|
2019-10-15 15:06:16 +02:00
|
|
|
RefPtr<LayoutNode> Node::create_layout_node(const StyleProperties*) const
|
2019-10-09 20:17:01 +02:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-10-14 18:32:02 +02:00
|
|
|
|
|
|
|
void Node::invalidate_style()
|
|
|
|
{
|
2019-10-19 18:57:02 +02:00
|
|
|
for_each_in_subtree([&](auto& node) {
|
|
|
|
if (is<Element>(node))
|
|
|
|
node.set_needs_style_update(true);
|
2019-10-21 12:01:30 +02:00
|
|
|
return IterationDecision::Continue;
|
2019-10-19 18:57:02 +02:00
|
|
|
});
|
|
|
|
document().schedule_style_update();
|
2019-10-14 18:32:02 +02:00
|
|
|
}
|
2019-10-19 21:21:29 +02:00
|
|
|
|
|
|
|
bool Node::is_link() const
|
|
|
|
{
|
|
|
|
auto* enclosing_link = enclosing_link_element();
|
|
|
|
if (!enclosing_link)
|
|
|
|
return false;
|
|
|
|
return enclosing_link->has_attribute("href");
|
|
|
|
}
|