Document.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  62. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  63. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  64. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  65. virtual FlyString node_name() const override { return "#document"; }
  66. void set_hovered_node(Node*);
  67. Node* hovered_node() { return m_hovered_node; }
  68. const Node* hovered_node() const { return m_hovered_node; }
  69. void set_inspected_node(Node*);
  70. Node* inspected_node() { return m_inspected_node; }
  71. const Node* inspected_node() const { return m_inspected_node; }
  72. const Element* document_element() const;
  73. const HTML::HTMLHtmlElement* html_element() const;
  74. const HTML::HTMLHeadElement* head() const;
  75. const HTML::HTMLElement* body() const;
  76. void set_body(HTML::HTMLElement& new_body);
  77. String title() const;
  78. void attach_to_frame(Badge<Frame>, Frame&);
  79. void detach_from_frame(Badge<Frame>, Frame&);
  80. Frame* frame() { return m_frame.ptr(); }
  81. const Frame* frame() const { return m_frame.ptr(); }
  82. Color background_color(const Gfx::Palette&) const;
  83. RefPtr<Gfx::Bitmap> background_image() const;
  84. Color link_color() const;
  85. void set_link_color(Color);
  86. Color active_link_color() const;
  87. void set_active_link_color(Color);
  88. Color visited_link_color() const;
  89. void set_visited_link_color(Color);
  90. void layout();
  91. void force_layout();
  92. void invalidate_layout();
  93. void update_style();
  94. void update_layout();
  95. virtual bool is_child_allowed(const Node&) const override;
  96. const LayoutDocument* layout_node() const;
  97. LayoutDocument* layout_node();
  98. void schedule_style_update();
  99. NonnullRefPtrVector<Element> get_elements_by_name(const String&) const;
  100. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  101. const String& source() const { return m_source; }
  102. void set_source(const String& source) { m_source = source; }
  103. virtual JS::Interpreter& interpreter() override;
  104. JS::Value run_javascript(const StringView&);
  105. NonnullRefPtr<Element> create_element(const String& tag_name);
  106. NonnullRefPtr<DocumentFragment> create_document_fragment();
  107. NonnullRefPtr<Text> create_text_node(const String& data);
  108. NonnullRefPtr<Comment> create_comment(const String& data);
  109. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  110. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  111. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  112. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  113. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  114. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  115. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  116. QuirksMode mode() const { return m_quirks_mode; }
  117. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  118. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  119. void adopt_node(Node&);
  120. const DocumentType* doctype() const;
  121. const String& compat_mode() const;
  122. void set_editable(bool editable) { m_editable = editable; }
  123. virtual bool is_editable() const final;
  124. Element* focused_element() { return m_focused_element; }
  125. const Element* focused_element() const { return m_focused_element; }
  126. void set_focused_element(Element*);
  127. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  128. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  129. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  130. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  131. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  132. const String& ready_state() const { return m_ready_state; }
  133. void set_ready_state(const String&);
  134. void ref_from_node(Badge<Node>)
  135. {
  136. ++m_referencing_node_count;
  137. }
  138. void unref_from_node(Badge<Node>)
  139. {
  140. ASSERT(m_referencing_node_count);
  141. --m_referencing_node_count;
  142. if (!m_referencing_node_count && !ref_count()) {
  143. removed_last_ref();
  144. }
  145. }
  146. void removed_last_ref();
  147. private:
  148. virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override;
  149. unsigned m_referencing_node_count { 0 };
  150. OwnPtr<CSS::StyleResolver> m_style_resolver;
  151. RefPtr<CSS::StyleSheetList> m_style_sheets;
  152. RefPtr<Node> m_hovered_node;
  153. RefPtr<Node> m_inspected_node;
  154. WeakPtr<Frame> m_frame;
  155. URL m_url;
  156. RefPtr<Window> m_window;
  157. RefPtr<LayoutDocument> m_layout_root;
  158. Optional<Color> m_link_color;
  159. Optional<Color> m_active_link_color;
  160. Optional<Color> m_visited_link_color;
  161. RefPtr<Core::Timer> m_style_update_timer;
  162. String m_source;
  163. OwnPtr<JS::Interpreter> m_interpreter;
  164. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  165. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  166. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  167. QuirksMode m_quirks_mode { QuirksMode::No };
  168. bool m_editable { false };
  169. WeakPtr<Element> m_focused_element;
  170. bool m_created_for_appropriate_template_contents { false };
  171. RefPtr<Document> m_associated_inert_template_document;
  172. String m_ready_state { "loading" };
  173. };
  174. }
  175. AK_BEGIN_TYPE_TRAITS(Web::DOM::Document)
  176. static bool is_type(const Web::DOM::Node& node) { return node.is_document(); }
  177. AK_END_TYPE_TRAITS()