Document.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/Function.h>
  29. #include <AK/NonnullRefPtrVector.h>
  30. #include <AK/OwnPtr.h>
  31. #include <AK/String.h>
  32. #include <AK/URL.h>
  33. #include <AK/WeakPtr.h>
  34. #include <LibCore/Forward.h>
  35. #include <LibJS/Forward.h>
  36. #include <LibWeb/Bindings/ScriptExecutionContext.h>
  37. #include <LibWeb/CSS/StyleResolver.h>
  38. #include <LibWeb/CSS/StyleSheet.h>
  39. #include <LibWeb/CSS/StyleSheetList.h>
  40. #include <LibWeb/DOM/NonElementParentNode.h>
  41. #include <LibWeb/DOM/ParentNode.h>
  42. namespace Web::DOM {
  43. enum class QuirksMode {
  44. No,
  45. Limited,
  46. Yes
  47. };
  48. class Document
  49. : public ParentNode
  50. , public NonElementParentNode<Document>
  51. , public Bindings::ScriptExecutionContext {
  52. public:
  53. using WrapperType = Bindings::DocumentWrapper;
  54. explicit Document(const URL& = {});
  55. virtual ~Document() override;
  56. void set_url(const URL& url) { m_url = url; }
  57. URL url() const { return m_url; }
  58. Origin origin() const;
  59. bool is_scripting_enabled() const { return true; }
  60. URL complete_url(const String&) const;
  61. void fixup();
  62. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  63. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  64. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  65. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  66. virtual FlyString node_name() const override { return "#document"; }
  67. void set_hovered_node(Node*);
  68. Node* hovered_node() { return m_hovered_node; }
  69. const Node* hovered_node() const { return m_hovered_node; }
  70. void set_inspected_node(Node*);
  71. Node* inspected_node() { return m_inspected_node; }
  72. const Node* inspected_node() const { return m_inspected_node; }
  73. const Element* document_element() const;
  74. const HTML::HTMLHtmlElement* html_element() const;
  75. const HTML::HTMLHeadElement* head() const;
  76. const HTML::HTMLElement* body() const;
  77. void set_body(HTML::HTMLElement& new_body);
  78. String title() const;
  79. void attach_to_frame(Badge<Frame>, Frame&);
  80. void detach_from_frame(Badge<Frame>, Frame&);
  81. Frame* frame() { return m_frame.ptr(); }
  82. const Frame* frame() const { return m_frame.ptr(); }
  83. Color background_color(const Gfx::Palette&) const;
  84. RefPtr<Gfx::Bitmap> background_image() const;
  85. Color link_color() const;
  86. void set_link_color(Color);
  87. Color active_link_color() const;
  88. void set_active_link_color(Color);
  89. Color visited_link_color() const;
  90. void set_visited_link_color(Color);
  91. void layout();
  92. void force_layout();
  93. void invalidate_layout();
  94. void update_style();
  95. void update_layout();
  96. virtual bool is_child_allowed(const Node&) const override;
  97. const LayoutDocument* layout_node() const;
  98. LayoutDocument* layout_node();
  99. void schedule_style_update();
  100. NonnullRefPtrVector<Element> get_elements_by_name(const String&) const;
  101. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  102. const String& source() const { return m_source; }
  103. void set_source(const String& source) { m_source = source; }
  104. virtual JS::Interpreter& interpreter() override;
  105. JS::Value run_javascript(const StringView&);
  106. NonnullRefPtr<Element> create_element(const String& tag_name);
  107. NonnullRefPtr<DocumentFragment> create_document_fragment();
  108. NonnullRefPtr<Text> create_text_node(const String& data);
  109. NonnullRefPtr<Comment> create_comment(const String& data);
  110. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  111. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  112. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  113. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  114. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  115. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  116. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  117. QuirksMode mode() const { return m_quirks_mode; }
  118. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  119. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  120. void adopt_node(Node&);
  121. const DocumentType* doctype() const;
  122. const String& compat_mode() const;
  123. void set_editable(bool editable) { m_editable = editable; }
  124. virtual bool is_editable() const final;
  125. Element* focused_element() { return m_focused_element; }
  126. const Element* focused_element() const { return m_focused_element; }
  127. void set_focused_element(Element*);
  128. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  129. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  130. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  131. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  132. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  133. const String& ready_state() const { return m_ready_state; }
  134. void set_ready_state(const String&);
  135. private:
  136. virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override;
  137. OwnPtr<CSS::StyleResolver> m_style_resolver;
  138. RefPtr<CSS::StyleSheetList> m_style_sheets;
  139. RefPtr<Node> m_hovered_node;
  140. RefPtr<Node> m_inspected_node;
  141. WeakPtr<Frame> m_frame;
  142. URL m_url;
  143. RefPtr<Window> m_window;
  144. RefPtr<LayoutDocument> m_layout_root;
  145. Optional<Color> m_link_color;
  146. Optional<Color> m_active_link_color;
  147. Optional<Color> m_visited_link_color;
  148. RefPtr<Core::Timer> m_style_update_timer;
  149. String m_source;
  150. OwnPtr<JS::Interpreter> m_interpreter;
  151. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  152. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  153. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  154. QuirksMode m_quirks_mode { QuirksMode::No };
  155. bool m_editable { false };
  156. WeakPtr<Element> m_focused_element;
  157. bool m_created_for_appropriate_template_contents { false };
  158. RefPtr<Document> m_associated_inert_template_document;
  159. String m_ready_state { "loading" };
  160. };
  161. }
  162. AK_BEGIN_TYPE_TRAITS(Web::DOM::Document)
  163. static bool is_type(const Web::DOM::Node& node) { return node.is_document(); }
  164. AK_END_TYPE_TRAITS()