Document.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 attach_to_frame(Badge<Frame>, Frame&);
  81. void detach_from_frame(Badge<Frame>, Frame&);
  82. Frame* frame() { return m_frame.ptr(); }
  83. const Frame* frame() const { return m_frame.ptr(); }
  84. Page* page();
  85. const Page* page() const;
  86. Color background_color(const Gfx::Palette&) const;
  87. RefPtr<Gfx::Bitmap> background_image() const;
  88. Color link_color() const;
  89. void set_link_color(Color);
  90. Color active_link_color() const;
  91. void set_active_link_color(Color);
  92. Color visited_link_color() const;
  93. void set_visited_link_color(Color);
  94. void layout();
  95. void force_layout();
  96. void invalidate_layout();
  97. void update_style();
  98. void update_layout();
  99. virtual bool is_child_allowed(const Node&) const override;
  100. const LayoutDocument* layout_node() const;
  101. LayoutDocument* layout_node();
  102. void schedule_style_update();
  103. NonnullRefPtrVector<Element> get_elements_by_name(const String&) const;
  104. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  105. const String& source() const { return m_source; }
  106. void set_source(const String& source) { m_source = source; }
  107. virtual JS::Interpreter& interpreter() override;
  108. JS::Value run_javascript(const StringView&);
  109. NonnullRefPtr<Element> create_element(const String& tag_name);
  110. NonnullRefPtr<DocumentFragment> create_document_fragment();
  111. NonnullRefPtr<Text> create_text_node(const String& data);
  112. NonnullRefPtr<Comment> create_comment(const String& data);
  113. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  114. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  115. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  116. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  117. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  118. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  119. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  120. QuirksMode mode() const { return m_quirks_mode; }
  121. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  122. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  123. void adopt_node(Node&);
  124. const DocumentType* doctype() const;
  125. const String& compat_mode() const;
  126. void set_editable(bool editable) { m_editable = editable; }
  127. virtual bool is_editable() const final;
  128. Element* focused_element() { return m_focused_element; }
  129. const Element* focused_element() const { return m_focused_element; }
  130. void set_focused_element(Element*);
  131. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  132. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  133. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  134. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  135. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  136. const String& ready_state() const { return m_ready_state; }
  137. void set_ready_state(const String&);
  138. void ref_from_node(Badge<Node>)
  139. {
  140. increment_referencing_node_count();
  141. }
  142. void unref_from_node(Badge<Node>)
  143. {
  144. decrement_referencing_node_count();
  145. }
  146. void removed_last_ref();
  147. Window& window() { return *m_window; }
  148. const String& content_type() const { return m_content_type; }
  149. void set_content_type(const String& content_type) { m_content_type = content_type; }
  150. const String& encoding() const { return m_encoding; }
  151. void set_encoding(const String& encoding) { m_encoding = encoding; }
  152. // NOTE: These are intended for the JS bindings
  153. const String& character_set() const { return encoding(); }
  154. const String& charset() const { return encoding(); }
  155. const String& input_encoding() const { return encoding(); }
  156. const NonnullRefPtr<DOMImplementation> implementation() { return m_implementation; }
  157. private:
  158. explicit Document(const URL&);
  159. virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override;
  160. void tear_down_layout_tree();
  161. void increment_referencing_node_count()
  162. {
  163. ASSERT(!m_deletion_has_begun);
  164. ++m_referencing_node_count;
  165. }
  166. void decrement_referencing_node_count()
  167. {
  168. ASSERT(!m_deletion_has_begun);
  169. ASSERT(m_referencing_node_count);
  170. --m_referencing_node_count;
  171. if (!m_referencing_node_count && !ref_count()) {
  172. m_deletion_has_begun = true;
  173. delete this;
  174. }
  175. }
  176. unsigned m_referencing_node_count { 0 };
  177. OwnPtr<CSS::StyleResolver> m_style_resolver;
  178. RefPtr<CSS::StyleSheetList> m_style_sheets;
  179. RefPtr<Node> m_hovered_node;
  180. RefPtr<Node> m_inspected_node;
  181. WeakPtr<Frame> m_frame;
  182. URL m_url;
  183. RefPtr<Window> m_window;
  184. RefPtr<LayoutDocument> m_layout_root;
  185. Optional<Color> m_link_color;
  186. Optional<Color> m_active_link_color;
  187. Optional<Color> m_visited_link_color;
  188. RefPtr<Core::Timer> m_style_update_timer;
  189. String m_source;
  190. OwnPtr<JS::Interpreter> m_interpreter;
  191. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  192. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  193. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  194. QuirksMode m_quirks_mode { QuirksMode::No };
  195. bool m_editable { false };
  196. WeakPtr<Element> m_focused_element;
  197. bool m_created_for_appropriate_template_contents { false };
  198. RefPtr<Document> m_associated_inert_template_document;
  199. String m_ready_state { "loading" };
  200. String m_content_type { "application/xml" };
  201. String m_encoding { "UTF-8" };
  202. NonnullRefPtr<DOMImplementation> m_implementation;
  203. };
  204. }
  205. AK_BEGIN_TYPE_TRAITS(Web::DOM::Document)
  206. static bool is_type(const Web::DOM::Node& node) { return node.is_document(); }
  207. AK_END_TYPE_TRAITS()