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#pragma once
28
29#include <AK/Function.h>
30#include <AK/NonnullRefPtrVector.h>
31#include <AK/OwnPtr.h>
32#include <AK/String.h>
33#include <AK/URL.h>
34#include <AK/WeakPtr.h>
35#include <LibCore/Forward.h>
36#include <LibHTML/CSS/StyleResolver.h>
37#include <LibHTML/CSS/StyleSheet.h>
38#include <LibHTML/DOM/ParentNode.h>
39
40class Frame;
41class HTMLBodyElement;
42class HTMLHtmlElement;
43class HTMLHeadElement;
44class LayoutDocument;
45class LayoutNode;
46class StyleResolver;
47class StyleSheet;
48
49class Document : public ParentNode {
50public:
51 Document();
52 virtual ~Document() override;
53
54 void set_url(const URL& url) { m_url = url; }
55 const URL& url() const { return m_url; }
56
57 URL complete_url(const String&) const;
58
59 void fixup();
60
61 StyleResolver& style_resolver() { return *m_style_resolver; }
62 const StyleResolver& style_resolver() const { return *m_style_resolver; }
63
64 void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); }
65 const NonnullRefPtrVector<StyleSheet>& stylesheets() const { return m_sheets; }
66
67 virtual String tag_name() const override { return "#document"; }
68
69 void set_hovered_node(Node*);
70 Node* hovered_node() { return m_hovered_node; }
71 const Node* hovered_node() const { return m_hovered_node; }
72
73 void set_inspected_node(Node*);
74 Node* inspected_node() { return m_inspected_node; }
75 const Node* inspected_node() const { return m_inspected_node; }
76
77 const HTMLHtmlElement* document_element() const;
78 const HTMLHeadElement* head() const;
79 const HTMLBodyElement* body() const;
80
81 String title() const;
82
83 void attach_to_frame(Badge<Frame>, Frame&);
84 void detach_from_frame(Badge<Frame>, Frame&);
85
86 Frame* frame() { return m_frame.ptr(); }
87 const Frame* frame() const { return m_frame.ptr(); }
88
89 Color background_color(const Gfx::Palette&) const;
90 RefPtr<Gfx::Bitmap> background_image() const;
91
92 Color link_color() const;
93 void set_link_color(Color);
94
95 Color active_link_color() const;
96 void set_active_link_color(Color);
97
98 Color visited_link_color() const;
99 void set_visited_link_color(Color);
100
101 void layout();
102 void force_layout();
103
104 void update_style();
105 void update_layout();
106 Function<void()> on_layout_updated;
107
108 virtual bool is_child_allowed(const Node&) const override;
109
110 const LayoutDocument* layout_node() const;
111 LayoutDocument* layout_node();
112
113 void schedule_style_update();
114
115 const Element* get_element_by_id(const String&) const;
116 Vector<const Element*> get_elements_by_name(const String&) const;
117
118 const String& source() const { return m_source; }
119 void set_source(const String& source) { m_source = source; }
120
121private:
122 virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
123
124 OwnPtr<StyleResolver> m_style_resolver;
125 NonnullRefPtrVector<StyleSheet> m_sheets;
126 RefPtr<Node> m_hovered_node;
127 RefPtr<Node> m_inspected_node;
128 WeakPtr<Frame> m_frame;
129 URL m_url;
130
131 RefPtr<LayoutDocument> m_layout_root;
132
133 Optional<Color> m_link_color;
134 Optional<Color> m_active_link_color;
135 Optional<Color> m_visited_link_color;
136
137 RefPtr<Core::Timer> m_style_update_timer;
138
139 String m_source;
140};
141
142template<>
143inline bool is<Document>(const Node& node)
144{
145 return node.is_document();
146}