Document.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/Function.h>
  9. #include <AK/NonnullRefPtrVector.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/String.h>
  12. #include <AK/URL.h>
  13. #include <AK/WeakPtr.h>
  14. #include <LibCore/Forward.h>
  15. #include <LibJS/Forward.h>
  16. #include <LibWeb/Bindings/ScriptExecutionContext.h>
  17. #include <LibWeb/Bindings/WindowObject.h>
  18. #include <LibWeb/CSS/CSSStyleSheet.h>
  19. #include <LibWeb/CSS/StyleResolver.h>
  20. #include <LibWeb/CSS/StyleSheetList.h>
  21. #include <LibWeb/Cookie/Cookie.h>
  22. #include <LibWeb/DOM/DOMImplementation.h>
  23. #include <LibWeb/DOM/ExceptionOr.h>
  24. #include <LibWeb/DOM/NonElementParentNode.h>
  25. #include <LibWeb/DOM/ParentNode.h>
  26. #include <LibWeb/HTML/HTMLScriptElement.h>
  27. namespace Web::DOM {
  28. enum class QuirksMode {
  29. No,
  30. Limited,
  31. Yes
  32. };
  33. class Document
  34. : public ParentNode
  35. , public NonElementParentNode<Document>
  36. , public HTML::GlobalEventHandlers
  37. , public Bindings::ScriptExecutionContext {
  38. public:
  39. using WrapperType = Bindings::DocumentWrapper;
  40. static NonnullRefPtr<Document> create(const URL& url = "about:blank")
  41. {
  42. return adopt(*new Document(url));
  43. }
  44. static NonnullRefPtr<Document> create_with_global_object(Bindings::WindowObject&)
  45. {
  46. return Document::create();
  47. }
  48. virtual ~Document() override;
  49. String cookie(Cookie::Source = Cookie::Source::NonHttp);
  50. void set_cookie(String, Cookie::Source = Cookie::Source::NonHttp);
  51. bool should_invalidate_styles_on_attribute_changes() const { return m_should_invalidate_styles_on_attribute_changes; }
  52. void set_should_invalidate_styles_on_attribute_changes(bool b) { m_should_invalidate_styles_on_attribute_changes = b; }
  53. void set_url(const URL& url) { m_url = url; }
  54. URL url() const { return m_url; }
  55. Origin origin() const;
  56. void set_origin(const Origin& origin);
  57. bool is_scripting_enabled() const { return true; }
  58. URL complete_url(const String&) const;
  59. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  60. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  61. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  62. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  63. NonnullRefPtr<CSS::StyleSheetList> style_sheets_for_bindings() { return *m_style_sheets; }
  64. virtual FlyString node_name() const override { return "#document"; }
  65. void set_hovered_node(Node*);
  66. Node* hovered_node() { return m_hovered_node; }
  67. const Node* hovered_node() const { return m_hovered_node; }
  68. void set_inspected_node(Node*);
  69. Node* inspected_node() { return m_inspected_node; }
  70. const Node* inspected_node() const { return m_inspected_node; }
  71. Element* document_element();
  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. ExceptionOr<void> set_body(HTML::HTMLElement& new_body);
  77. String title() const;
  78. void set_title(const String&);
  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. Page* page();
  84. const Page* page() const;
  85. Color background_color(const Gfx::Palette&) const;
  86. RefPtr<Gfx::Bitmap> background_image() const;
  87. CSS::Repeat background_repeat_x() const;
  88. CSS::Repeat background_repeat_y() 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 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 Layout::InitialContainingBlockBox* layout_node() const;
  101. Layout::InitialContainingBlockBox* layout_node();
  102. void schedule_style_update();
  103. void schedule_forced_layout();
  104. NonnullRefPtr<HTMLCollection> get_elements_by_name(String const&);
  105. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  106. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  107. NonnullRefPtr<HTMLCollection> applets();
  108. const String& source() const { return m_source; }
  109. void set_source(const String& source) { m_source = source; }
  110. virtual JS::Interpreter& interpreter() override;
  111. JS::Value run_javascript(const StringView& source, const StringView& filename = "(unknown)");
  112. NonnullRefPtr<Element> create_element(const String& tag_name);
  113. NonnullRefPtr<Element> create_element_ns(const String& namespace_, const String& qualifed_name);
  114. NonnullRefPtr<DocumentFragment> create_document_fragment();
  115. NonnullRefPtr<Text> create_text_node(const String& data);
  116. NonnullRefPtr<Comment> create_comment(const String& data);
  117. NonnullRefPtr<Range> create_range();
  118. NonnullRefPtr<Event> create_event(const String& interface);
  119. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  120. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  121. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  122. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  123. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  124. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  125. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  126. QuirksMode mode() const { return m_quirks_mode; }
  127. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  128. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  129. void adopt_node(Node&);
  130. ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
  131. const DocumentType* doctype() const;
  132. const String& compat_mode() const;
  133. void set_editable(bool editable) { m_editable = editable; }
  134. virtual bool is_editable() const final;
  135. Element* focused_element() { return m_focused_element; }
  136. const Element* focused_element() const { return m_focused_element; }
  137. void set_focused_element(Element*);
  138. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  139. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  140. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  141. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  142. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  143. const String& ready_state() const { return m_ready_state; }
  144. void set_ready_state(const String&);
  145. void ref_from_node(Badge<Node>)
  146. {
  147. increment_referencing_node_count();
  148. }
  149. void unref_from_node(Badge<Node>)
  150. {
  151. decrement_referencing_node_count();
  152. }
  153. void removed_last_ref();
  154. Window& window() { return *m_window; }
  155. Window* default_view() { return m_window; }
  156. const String& content_type() const { return m_content_type; }
  157. void set_content_type(const String& content_type) { m_content_type = content_type; }
  158. const String& encoding() const { return m_encoding; }
  159. void set_encoding(const String& encoding) { m_encoding = encoding; }
  160. // NOTE: These are intended for the JS bindings
  161. const String& character_set() const { return encoding(); }
  162. const String& charset() const { return encoding(); }
  163. const String& input_encoding() const { return encoding(); }
  164. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  165. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  166. void completely_finish_loading();
  167. const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
  168. RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
  169. void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = script; }
  170. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  171. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  172. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  173. virtual EventTarget* get_parent(const Event&) override;
  174. private:
  175. explicit Document(const URL&);
  176. // ^DOM::Node
  177. virtual RefPtr<Layout::Node> create_layout_node() override;
  178. // ^HTML::GlobalEventHandlers
  179. virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }
  180. void tear_down_layout_tree();
  181. void increment_referencing_node_count()
  182. {
  183. VERIFY(!m_deletion_has_begun);
  184. ++m_referencing_node_count;
  185. }
  186. void decrement_referencing_node_count()
  187. {
  188. VERIFY(!m_deletion_has_begun);
  189. VERIFY(m_referencing_node_count);
  190. --m_referencing_node_count;
  191. if (!m_referencing_node_count && !ref_count()) {
  192. m_deletion_has_begun = true;
  193. delete this;
  194. }
  195. }
  196. unsigned m_referencing_node_count { 0 };
  197. OwnPtr<CSS::StyleResolver> m_style_resolver;
  198. RefPtr<CSS::StyleSheetList> m_style_sheets;
  199. RefPtr<Node> m_hovered_node;
  200. RefPtr<Node> m_inspected_node;
  201. WeakPtr<Frame> m_frame;
  202. URL m_url;
  203. RefPtr<Window> m_window;
  204. RefPtr<Layout::InitialContainingBlockBox> m_layout_root;
  205. Optional<Color> m_link_color;
  206. Optional<Color> m_active_link_color;
  207. Optional<Color> m_visited_link_color;
  208. RefPtr<Core::Timer> m_style_update_timer;
  209. RefPtr<Core::Timer> m_forced_layout_timer;
  210. String m_source;
  211. OwnPtr<JS::Interpreter> m_interpreter;
  212. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  213. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  214. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  215. QuirksMode m_quirks_mode { QuirksMode::No };
  216. bool m_editable { false };
  217. WeakPtr<Element> m_focused_element;
  218. bool m_created_for_appropriate_template_contents { false };
  219. RefPtr<Document> m_associated_inert_template_document;
  220. String m_ready_state { "loading" };
  221. String m_content_type { "application/xml" };
  222. String m_encoding { "UTF-8" };
  223. bool m_ready_for_post_load_tasks { false };
  224. NonnullRefPtr<DOMImplementation> m_implementation;
  225. RefPtr<HTML::HTMLScriptElement> m_current_script;
  226. bool m_should_invalidate_styles_on_attribute_changes { true };
  227. u32 m_ignore_destructive_writes_counter { 0 };
  228. };
  229. }