Document.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/DOMImplementation.h>
  41. #include <LibWeb/DOM/NonElementParentNode.h>
  42. #include <LibWeb/DOM/ParentNode.h>
  43. namespace Web::DOM {
  44. enum class QuirksMode {
  45. No,
  46. Limited,
  47. Yes
  48. };
  49. class Document
  50. : public ParentNode
  51. , public NonElementParentNode<Document>
  52. , public Bindings::ScriptExecutionContext {
  53. public:
  54. using WrapperType = Bindings::DocumentWrapper;
  55. static NonnullRefPtr<Document> create(const URL& url = "about:blank") { return adopt(*new Document(url)); }
  56. virtual ~Document() override;
  57. void set_url(const URL& url) { m_url = url; }
  58. URL url() const { return m_url; }
  59. Origin origin() const;
  60. void set_origin(const Origin& origin);
  61. bool is_scripting_enabled() const { return true; }
  62. URL complete_url(const String&) const;
  63. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  64. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  65. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  66. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  67. virtual FlyString node_name() const override { return "#document"; }
  68. void set_hovered_node(Node*);
  69. Node* hovered_node() { return m_hovered_node; }
  70. const Node* hovered_node() const { return m_hovered_node; }
  71. void set_inspected_node(Node*);
  72. Node* inspected_node() { return m_inspected_node; }
  73. const Node* inspected_node() const { return m_inspected_node; }
  74. const Element* document_element() const;
  75. const HTML::HTMLHtmlElement* html_element() const;
  76. const HTML::HTMLHeadElement* head() const;
  77. const HTML::HTMLElement* body() const;
  78. void set_body(HTML::HTMLElement& new_body);
  79. String title() const;
  80. void set_title(const String&);
  81. void attach_to_frame(Badge<Frame>, Frame&);
  82. void detach_from_frame(Badge<Frame>, Frame&);
  83. Frame* frame() { return m_frame.ptr(); }
  84. const Frame* frame() const { return m_frame.ptr(); }
  85. Page* page();
  86. const Page* page() const;
  87. Color background_color(const Gfx::Palette&) const;
  88. RefPtr<Gfx::Bitmap> background_image() const;
  89. Color link_color() const;
  90. void set_link_color(Color);
  91. Color active_link_color() const;
  92. void set_active_link_color(Color);
  93. Color visited_link_color() const;
  94. void set_visited_link_color(Color);
  95. void layout();
  96. void force_layout();
  97. void invalidate_layout();
  98. void update_style();
  99. void update_layout();
  100. virtual bool is_child_allowed(const Node&) const override;
  101. const Layout::InitialContainingBlockBox* layout_node() const;
  102. Layout::InitialContainingBlockBox* layout_node();
  103. void schedule_style_update();
  104. NonnullRefPtrVector<Element> get_elements_by_name(const String&) const;
  105. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  106. NonnullRefPtrVector<Element> get_elements_by_class_name(const FlyString&) const;
  107. const String& source() const { return m_source; }
  108. void set_source(const String& source) { m_source = source; }
  109. virtual JS::Interpreter& interpreter() override;
  110. JS::Value run_javascript(const StringView&);
  111. NonnullRefPtr<Element> create_element(const String& tag_name);
  112. NonnullRefPtr<DocumentFragment> create_document_fragment();
  113. NonnullRefPtr<Text> create_text_node(const String& data);
  114. NonnullRefPtr<Comment> create_comment(const String& data);
  115. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  116. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  117. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  118. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  119. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  120. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  121. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  122. QuirksMode mode() const { return m_quirks_mode; }
  123. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  124. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  125. void adopt_node(Node&);
  126. const DocumentType* doctype() const;
  127. const String& compat_mode() const;
  128. void set_editable(bool editable) { m_editable = editable; }
  129. virtual bool is_editable() const final;
  130. Element* focused_element() { return m_focused_element; }
  131. const Element* focused_element() const { return m_focused_element; }
  132. void set_focused_element(Element*);
  133. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  134. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  135. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  136. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  137. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  138. const String& ready_state() const { return m_ready_state; }
  139. void set_ready_state(const String&);
  140. void ref_from_node(Badge<Node>)
  141. {
  142. increment_referencing_node_count();
  143. }
  144. void unref_from_node(Badge<Node>)
  145. {
  146. decrement_referencing_node_count();
  147. }
  148. void removed_last_ref();
  149. Window& window() { return *m_window; }
  150. const String& content_type() const { return m_content_type; }
  151. void set_content_type(const String& content_type) { m_content_type = content_type; }
  152. const String& encoding() const { return m_encoding; }
  153. void set_encoding(const String& encoding) { m_encoding = encoding; }
  154. // NOTE: These are intended for the JS bindings
  155. const String& character_set() const { return encoding(); }
  156. const String& charset() const { return encoding(); }
  157. const String& input_encoding() const { return encoding(); }
  158. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  159. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  160. void completely_finish_loading();
  161. const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
  162. virtual EventTarget* get_parent(const Event&) override;
  163. private:
  164. explicit Document(const URL&);
  165. virtual RefPtr<Layout::Node> create_layout_node(const CSS::StyleProperties* parent_style) override;
  166. void tear_down_layout_tree();
  167. void increment_referencing_node_count()
  168. {
  169. ASSERT(!m_deletion_has_begun);
  170. ++m_referencing_node_count;
  171. }
  172. void decrement_referencing_node_count()
  173. {
  174. ASSERT(!m_deletion_has_begun);
  175. ASSERT(m_referencing_node_count);
  176. --m_referencing_node_count;
  177. if (!m_referencing_node_count && !ref_count()) {
  178. m_deletion_has_begun = true;
  179. delete this;
  180. }
  181. }
  182. unsigned m_referencing_node_count { 0 };
  183. OwnPtr<CSS::StyleResolver> m_style_resolver;
  184. RefPtr<CSS::StyleSheetList> m_style_sheets;
  185. RefPtr<Node> m_hovered_node;
  186. RefPtr<Node> m_inspected_node;
  187. WeakPtr<Frame> m_frame;
  188. URL m_url;
  189. RefPtr<Window> m_window;
  190. RefPtr<Layout::InitialContainingBlockBox> m_layout_root;
  191. Optional<Color> m_link_color;
  192. Optional<Color> m_active_link_color;
  193. Optional<Color> m_visited_link_color;
  194. RefPtr<Core::Timer> m_style_update_timer;
  195. String m_source;
  196. OwnPtr<JS::Interpreter> m_interpreter;
  197. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  198. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  199. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  200. QuirksMode m_quirks_mode { QuirksMode::No };
  201. bool m_editable { false };
  202. WeakPtr<Element> m_focused_element;
  203. bool m_created_for_appropriate_template_contents { false };
  204. RefPtr<Document> m_associated_inert_template_document;
  205. String m_ready_state { "loading" };
  206. String m_content_type { "application/xml" };
  207. String m_encoding { "UTF-8" };
  208. bool m_ready_for_post_load_tasks { false };
  209. NonnullRefPtr<DOMImplementation> m_implementation;
  210. };
  211. }
  212. AK_BEGIN_TYPE_TRAITS(Web::DOM::Document)
  213. static bool is_type(const Web::DOM::Node& node) { return node.is_document(); }
  214. AK_END_TYPE_TRAITS()