Document.h 12 KB

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