Document.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_ref(*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. HTML::HTMLHtmlElement* html_element();
  74. HTML::HTMLHeadElement* head();
  75. HTML::HTMLElement* body();
  76. const HTML::HTMLHtmlElement* html_element() const
  77. {
  78. return const_cast<Document*>(this)->html_element();
  79. }
  80. const HTML::HTMLHeadElement* head() const
  81. {
  82. return const_cast<Document*>(this)->head();
  83. }
  84. const HTML::HTMLElement* body() const
  85. {
  86. return const_cast<Document*>(this)->body();
  87. }
  88. ExceptionOr<void> set_body(HTML::HTMLElement* new_body);
  89. String title() const;
  90. void set_title(const String&);
  91. void attach_to_browsing_context(Badge<BrowsingContext>, BrowsingContext&);
  92. void detach_from_browsing_context(Badge<BrowsingContext>, BrowsingContext&);
  93. BrowsingContext* browsing_context() { return m_browsing_context.ptr(); }
  94. const BrowsingContext* browsing_context() const { return m_browsing_context.ptr(); }
  95. Page* page();
  96. const Page* page() const;
  97. Color background_color(const Gfx::Palette&) const;
  98. RefPtr<Gfx::Bitmap> background_image() const;
  99. CSS::Repeat background_repeat_x() const;
  100. CSS::Repeat background_repeat_y() const;
  101. Color link_color() const;
  102. void set_link_color(Color);
  103. Color active_link_color() const;
  104. void set_active_link_color(Color);
  105. Color visited_link_color() const;
  106. void set_visited_link_color(Color);
  107. void force_layout();
  108. void invalidate_layout();
  109. void update_style();
  110. void update_layout();
  111. virtual bool is_child_allowed(const Node&) const override;
  112. const Layout::InitialContainingBlockBox* layout_node() const;
  113. Layout::InitialContainingBlockBox* layout_node();
  114. void schedule_style_update();
  115. void schedule_forced_layout();
  116. NonnullRefPtr<HTMLCollection> get_elements_by_name(String const&);
  117. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  118. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  119. NonnullRefPtr<HTMLCollection> applets();
  120. NonnullRefPtr<HTMLCollection> anchors();
  121. NonnullRefPtr<HTMLCollection> images();
  122. NonnullRefPtr<HTMLCollection> embeds();
  123. NonnullRefPtr<HTMLCollection> plugins();
  124. NonnullRefPtr<HTMLCollection> links();
  125. NonnullRefPtr<HTMLCollection> forms();
  126. NonnullRefPtr<HTMLCollection> scripts();
  127. const String& source() const { return m_source; }
  128. void set_source(const String& source) { m_source = source; }
  129. virtual JS::Interpreter& interpreter() override;
  130. JS::Value run_javascript(const StringView& source, const StringView& filename = "(unknown)");
  131. NonnullRefPtr<Element> create_element(const String& tag_name);
  132. NonnullRefPtr<Element> create_element_ns(const String& namespace_, const String& qualifed_name);
  133. NonnullRefPtr<DocumentFragment> create_document_fragment();
  134. NonnullRefPtr<Text> create_text_node(const String& data);
  135. NonnullRefPtr<Comment> create_comment(const String& data);
  136. NonnullRefPtr<Range> create_range();
  137. NonnullRefPtr<Event> create_event(const String& interface);
  138. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  139. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  140. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  141. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  142. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  143. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  144. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  145. QuirksMode mode() const { return m_quirks_mode; }
  146. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  147. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  148. void adopt_node(Node&);
  149. ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
  150. const DocumentType* doctype() const;
  151. const String& compat_mode() const;
  152. void set_editable(bool editable) { m_editable = editable; }
  153. virtual bool is_editable() const final;
  154. Element* focused_element() { return m_focused_element; }
  155. const Element* focused_element() const { return m_focused_element; }
  156. void set_focused_element(Element*);
  157. const Element* active_element() const { return m_active_element; }
  158. void set_active_element(Element*);
  159. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  160. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  161. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  162. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  163. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  164. const String& ready_state() const { return m_ready_state; }
  165. void set_ready_state(const String&);
  166. void ref_from_node(Badge<Node>)
  167. {
  168. increment_referencing_node_count();
  169. }
  170. void unref_from_node(Badge<Node>)
  171. {
  172. decrement_referencing_node_count();
  173. }
  174. void removed_last_ref();
  175. Window& window() { return *m_window; }
  176. Window* default_view() { return m_window; }
  177. const String& content_type() const { return m_content_type; }
  178. void set_content_type(const String& content_type) { m_content_type = content_type; }
  179. bool has_encoding() const { return m_encoding.has_value(); }
  180. const Optional<String>& encoding() const { return m_encoding; }
  181. String encoding_or_default() const { return m_encoding.value_or("UTF-8"); }
  182. void set_encoding(const Optional<String>& encoding) { m_encoding = encoding; }
  183. // NOTE: These are intended for the JS bindings
  184. String character_set() const { return encoding_or_default(); }
  185. String charset() const { return encoding_or_default(); }
  186. String input_encoding() const { return encoding_or_default(); }
  187. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  188. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  189. void completely_finish_loading();
  190. const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
  191. RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
  192. void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = script; }
  193. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  194. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  195. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  196. virtual EventTarget* get_parent(const Event&) override;
  197. String dump_dom_tree_as_json() const;
  198. private:
  199. explicit Document(const URL&);
  200. // ^DOM::Node
  201. virtual RefPtr<Layout::Node> create_layout_node() override;
  202. // ^HTML::GlobalEventHandlers
  203. virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }
  204. void tear_down_layout_tree();
  205. void increment_referencing_node_count()
  206. {
  207. VERIFY(!m_deletion_has_begun);
  208. ++m_referencing_node_count;
  209. }
  210. void decrement_referencing_node_count()
  211. {
  212. VERIFY(!m_deletion_has_begun);
  213. VERIFY(m_referencing_node_count);
  214. --m_referencing_node_count;
  215. if (!m_referencing_node_count && !ref_count()) {
  216. m_deletion_has_begun = true;
  217. delete this;
  218. }
  219. }
  220. unsigned m_referencing_node_count { 0 };
  221. OwnPtr<CSS::StyleResolver> m_style_resolver;
  222. RefPtr<CSS::StyleSheetList> m_style_sheets;
  223. RefPtr<Node> m_hovered_node;
  224. RefPtr<Node> m_inspected_node;
  225. WeakPtr<BrowsingContext> m_browsing_context;
  226. URL m_url;
  227. RefPtr<Window> m_window;
  228. RefPtr<Layout::InitialContainingBlockBox> m_layout_root;
  229. Optional<Color> m_link_color;
  230. Optional<Color> m_active_link_color;
  231. Optional<Color> m_visited_link_color;
  232. RefPtr<Core::Timer> m_style_update_timer;
  233. RefPtr<Core::Timer> m_forced_layout_timer;
  234. String m_source;
  235. OwnPtr<JS::Interpreter> m_interpreter;
  236. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  237. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  238. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  239. QuirksMode m_quirks_mode { QuirksMode::No };
  240. bool m_editable { false };
  241. WeakPtr<Element> m_focused_element;
  242. WeakPtr<Element> m_active_element;
  243. bool m_created_for_appropriate_template_contents { false };
  244. RefPtr<Document> m_associated_inert_template_document;
  245. String m_ready_state { "loading" };
  246. String m_content_type { "application/xml" };
  247. Optional<String> m_encoding;
  248. bool m_ready_for_post_load_tasks { false };
  249. NonnullRefPtr<DOMImplementation> m_implementation;
  250. RefPtr<HTML::HTMLScriptElement> m_current_script;
  251. bool m_should_invalidate_styles_on_attribute_changes { true };
  252. u32 m_ignore_destructive_writes_counter { 0 };
  253. };
  254. }