Node.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibHTML/CSS/StyleResolver.h>
  28. #include <LibHTML/DOM/Element.h>
  29. #include <LibHTML/DOM/HTMLAnchorElement.h>
  30. #include <LibHTML/DOM/Node.h>
  31. #include <LibHTML/Layout/LayoutBlock.h>
  32. #include <LibHTML/Layout/LayoutDocument.h>
  33. #include <LibHTML/Layout/LayoutInline.h>
  34. #include <LibHTML/Layout/LayoutNode.h>
  35. #include <LibHTML/Layout/LayoutText.h>
  36. Node::Node(Document& document, NodeType type)
  37. : m_document(document)
  38. , m_type(type)
  39. {
  40. }
  41. Node::~Node()
  42. {
  43. }
  44. const HTMLAnchorElement* Node::enclosing_link_element() const
  45. {
  46. for (auto* node = this; node; node = node->parent()) {
  47. if (is<HTMLAnchorElement>(*node) && to<HTMLAnchorElement>(*node).has_attribute("href"))
  48. return to<HTMLAnchorElement>(node);
  49. }
  50. return nullptr;
  51. }
  52. const HTMLElement* Node::enclosing_html_element() const
  53. {
  54. return first_ancestor_of_type<HTMLElement>();
  55. }
  56. String Node::text_content() const
  57. {
  58. Vector<String> strings;
  59. StringBuilder builder;
  60. for (auto* child = first_child(); child; child = child->next_sibling()) {
  61. auto text = child->text_content();
  62. if (!text.is_empty()) {
  63. builder.append(child->text_content());
  64. builder.append(' ');
  65. }
  66. }
  67. if (builder.length() > 1)
  68. builder.trim(1);
  69. return builder.to_string();
  70. }
  71. const Element* Node::next_element_sibling() const
  72. {
  73. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  74. if (node->is_element())
  75. return static_cast<const Element*>(node);
  76. }
  77. return nullptr;
  78. }
  79. const Element* Node::previous_element_sibling() const
  80. {
  81. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  82. if (node->is_element())
  83. return static_cast<const Element*>(node);
  84. }
  85. return nullptr;
  86. }
  87. RefPtr<LayoutNode> Node::create_layout_node(const StyleProperties*) const
  88. {
  89. return nullptr;
  90. }
  91. void Node::invalidate_style()
  92. {
  93. for_each_in_subtree_of_type<Element>([&](auto& element) {
  94. element.set_needs_style_update(true);
  95. return IterationDecision::Continue;
  96. });
  97. document().schedule_style_update();
  98. }
  99. bool Node::is_link() const
  100. {
  101. auto* enclosing_link = enclosing_link_element();
  102. if (!enclosing_link)
  103. return false;
  104. return enclosing_link->has_attribute("href");
  105. }