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