Node.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <AK/StringBuilder.h>
  2. #include <LibHTML/CSS/StyleResolver.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/HTMLAnchorElement.h>
  5. #include <LibHTML/DOM/Node.h>
  6. #include <LibHTML/Layout/LayoutBlock.h>
  7. #include <LibHTML/Layout/LayoutDocument.h>
  8. #include <LibHTML/Layout/LayoutInline.h>
  9. #include <LibHTML/Layout/LayoutNode.h>
  10. #include <LibHTML/Layout/LayoutText.h>
  11. Node::Node(Document& document, NodeType type)
  12. : m_document(document)
  13. , m_type(type)
  14. {
  15. }
  16. Node::~Node()
  17. {
  18. }
  19. const HTMLAnchorElement* Node::enclosing_link_element() const
  20. {
  21. for (auto* node = this; node; node = node->parent()) {
  22. if (is<HTMLAnchorElement>(*node) && to<HTMLAnchorElement>(*node).has_attribute("href"))
  23. return to<HTMLAnchorElement>(node);
  24. }
  25. return nullptr;
  26. }
  27. const HTMLElement* Node::enclosing_html_element() const
  28. {
  29. return first_ancestor_of_type<HTMLElement>();
  30. }
  31. String Node::text_content() const
  32. {
  33. Vector<String> strings;
  34. StringBuilder builder;
  35. for (auto* child = first_child(); child; child = child->next_sibling()) {
  36. auto text = child->text_content();
  37. if (!text.is_empty()) {
  38. builder.append(child->text_content());
  39. builder.append(' ');
  40. }
  41. }
  42. if (builder.length() > 1)
  43. builder.trim(1);
  44. return builder.to_string();
  45. }
  46. const Element* Node::next_element_sibling() const
  47. {
  48. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  49. if (node->is_element())
  50. return static_cast<const Element*>(node);
  51. }
  52. return nullptr;
  53. }
  54. const Element* Node::previous_element_sibling() const
  55. {
  56. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  57. if (node->is_element())
  58. return static_cast<const Element*>(node);
  59. }
  60. return nullptr;
  61. }
  62. RefPtr<LayoutNode> Node::create_layout_node(const StyleProperties*) const
  63. {
  64. return nullptr;
  65. }
  66. void Node::invalidate_style()
  67. {
  68. for_each_in_subtree([&](auto& node) {
  69. if (is<Element>(node))
  70. node.set_needs_style_update(true);
  71. });
  72. document().schedule_style_update();
  73. }
  74. bool Node::is_link() const
  75. {
  76. auto* enclosing_link = enclosing_link_element();
  77. if (!enclosing_link)
  78. return false;
  79. return enclosing_link->has_attribute("href");
  80. }