Serenity Operating System
at hosted 159 lines 5.0 kB view raw
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/FlyString.h> 30#include <AK/Function.h> 31#include <AK/NonnullRefPtrVector.h> 32#include <AK/OwnPtr.h> 33#include <AK/String.h> 34#include <AK/URL.h> 35#include <AK/WeakPtr.h> 36#include <LibCore/Forward.h> 37#include <LibJS/Forward.h> 38#include <LibWeb/CSS/StyleResolver.h> 39#include <LibWeb/CSS/StyleSheet.h> 40#include <LibWeb/DOM/NonElementParentNode.h> 41#include <LibWeb/DOM/ParentNode.h> 42 43namespace Web { 44 45class Document 46 : public ParentNode 47 , public NonElementParentNode<Document> { 48public: 49 using WrapperType = Bindings::DocumentWrapper; 50 51 explicit Document(const URL& = {}); 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 Origin origin() const; 58 59 URL complete_url(const String&) const; 60 61 void fixup(); 62 63 StyleResolver& style_resolver() { return *m_style_resolver; } 64 const StyleResolver& style_resolver() const { return *m_style_resolver; } 65 66 void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); } 67 const NonnullRefPtrVector<StyleSheet>& stylesheets() const { return m_sheets; } 68 69 virtual FlyString tag_name() const override { return "#document"; } 70 71 void set_hovered_node(Node*); 72 Node* hovered_node() { return m_hovered_node; } 73 const Node* hovered_node() const { return m_hovered_node; } 74 75 void set_inspected_node(Node*); 76 Node* inspected_node() { return m_inspected_node; } 77 const Node* inspected_node() const { return m_inspected_node; } 78 79 const HTMLHtmlElement* document_element() const; 80 const HTMLHeadElement* head() const; 81 const HTMLBodyElement* body() const; 82 83 String title() const; 84 85 void attach_to_frame(Badge<Frame>, Frame&); 86 void detach_from_frame(Badge<Frame>, Frame&); 87 88 Frame* frame() { return m_frame.ptr(); } 89 const Frame* frame() const { return m_frame.ptr(); } 90 91 Color background_color(const Gfx::Palette&) const; 92 RefPtr<Gfx::Bitmap> background_image() const; 93 94 Color link_color() const; 95 void set_link_color(Color); 96 97 Color active_link_color() const; 98 void set_active_link_color(Color); 99 100 Color visited_link_color() const; 101 void set_visited_link_color(Color); 102 103 void layout(); 104 void force_layout(); 105 void invalidate_layout(); 106 107 void update_style(); 108 void update_layout(); 109 Function<void()> on_layout_updated; 110 111 virtual bool is_child_allowed(const Node&) const override; 112 113 const LayoutDocument* layout_node() const; 114 LayoutDocument* layout_node(); 115 116 void schedule_style_update(); 117 118 Vector<const Element*> get_elements_by_name(const String&) const; 119 NonnullRefPtrVector<Element> query_selector_all(const StringView&); 120 121 const String& source() const { return m_source; } 122 void set_source(const String& source) { m_source = source; } 123 124 JS::Interpreter& interpreter(); 125 126 JS::Value run_javascript(const StringView&); 127 128private: 129 virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; 130 131 OwnPtr<StyleResolver> m_style_resolver; 132 NonnullRefPtrVector<StyleSheet> m_sheets; 133 RefPtr<Node> m_hovered_node; 134 RefPtr<Node> m_inspected_node; 135 WeakPtr<Frame> m_frame; 136 URL m_url; 137 138 RefPtr<Window> m_window; 139 140 RefPtr<LayoutDocument> m_layout_root; 141 142 Optional<Color> m_link_color; 143 Optional<Color> m_active_link_color; 144 Optional<Color> m_visited_link_color; 145 146 RefPtr<Core::Timer> m_style_update_timer; 147 148 String m_source; 149 150 OwnPtr<JS::Interpreter> m_interpreter; 151}; 152 153template<> 154inline bool is<Document>(const Node& node) 155{ 156 return node.is_document(); 157} 158 159}