Node.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Function.h>
  30. #include <LibJS/Runtime/ScriptFunction.h>
  31. #include <LibWeb/Bindings/EventWrapper.h>
  32. #include <LibWeb/Bindings/NodeWrapper.h>
  33. #include <LibWeb/CSS/StyleResolver.h>
  34. #include <LibWeb/DOM/Element.h>
  35. #include <LibWeb/DOM/Event.h>
  36. #include <LibWeb/DOM/EventListener.h>
  37. #include <LibWeb/DOM/HTMLAnchorElement.h>
  38. #include <LibWeb/DOM/Node.h>
  39. #include <LibWeb/Layout/LayoutBlock.h>
  40. #include <LibWeb/Layout/LayoutDocument.h>
  41. #include <LibWeb/Layout/LayoutInline.h>
  42. #include <LibWeb/Layout/LayoutNode.h>
  43. #include <LibWeb/Layout/LayoutText.h>
  44. //#define EVENT_DEBUG
  45. namespace Web {
  46. Node::Node(Document& document, NodeType type)
  47. : m_document(document)
  48. , m_type(type)
  49. {
  50. }
  51. Node::~Node()
  52. {
  53. if (layout_node() && layout_node()->parent())
  54. layout_node()->parent()->remove_child(*layout_node());
  55. }
  56. const HTMLAnchorElement* Node::enclosing_link_element() const
  57. {
  58. for (auto* node = this; node; node = node->parent()) {
  59. if (is<HTMLAnchorElement>(*node) && to<HTMLAnchorElement>(*node).has_attribute("href"))
  60. return to<HTMLAnchorElement>(node);
  61. }
  62. return nullptr;
  63. }
  64. const HTMLElement* Node::enclosing_html_element() const
  65. {
  66. return first_ancestor_of_type<HTMLElement>();
  67. }
  68. String Node::text_content() const
  69. {
  70. Vector<String> strings;
  71. StringBuilder builder;
  72. for (auto* child = first_child(); child; child = child->next_sibling()) {
  73. auto text = child->text_content();
  74. if (!text.is_empty()) {
  75. builder.append(child->text_content());
  76. builder.append(' ');
  77. }
  78. }
  79. if (builder.length() > 1)
  80. builder.trim(1);
  81. return builder.to_string();
  82. }
  83. const Element* Node::next_element_sibling() const
  84. {
  85. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  86. if (node->is_element())
  87. return static_cast<const Element*>(node);
  88. }
  89. return nullptr;
  90. }
  91. const Element* Node::previous_element_sibling() const
  92. {
  93. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  94. if (node->is_element())
  95. return static_cast<const Element*>(node);
  96. }
  97. return nullptr;
  98. }
  99. RefPtr<LayoutNode> Node::create_layout_node(const StyleProperties*) const
  100. {
  101. return nullptr;
  102. }
  103. void Node::invalidate_style()
  104. {
  105. for_each_in_subtree_of_type<Element>([&](auto& element) {
  106. element.set_needs_style_update(true);
  107. return IterationDecision::Continue;
  108. });
  109. document().schedule_style_update();
  110. }
  111. bool Node::is_link() const
  112. {
  113. auto* enclosing_link = enclosing_link_element();
  114. if (!enclosing_link)
  115. return false;
  116. return enclosing_link->has_attribute("href");
  117. }
  118. void Node::dispatch_event(NonnullRefPtr<Event> event)
  119. {
  120. for (auto& listener : listeners()) {
  121. if (listener.event_name == event->name()) {
  122. auto* function = const_cast<EventListener&>(*listener.listener).function();
  123. #ifdef EVENT_DEBUG
  124. static_cast<const JS::ScriptFunction*>(function)->body().dump(0);
  125. #endif
  126. auto* this_value = wrap(function->heap(), *this);
  127. #ifdef EVENT_DEBUG
  128. dbg() << "calling event listener with this=" << this_value;
  129. #endif
  130. auto* event_wrapper = wrap(function->heap(), *event);
  131. document().interpreter().call(function, this_value, { event_wrapper });
  132. }
  133. }
  134. // FIXME: This is a hack. We should follow the real rules of event bubbling.
  135. if (parent())
  136. parent()->dispatch_event(move(event));
  137. }
  138. }