Document.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (c) 2018-2021, 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/HashMap.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/String.h>
  13. #include <AK/URL.h>
  14. #include <AK/WeakPtr.h>
  15. #include <LibCore/Forward.h>
  16. #include <LibJS/Forward.h>
  17. #include <LibWeb/Bindings/ScriptExecutionContext.h>
  18. #include <LibWeb/Bindings/WindowObject.h>
  19. #include <LibWeb/CSS/CSSStyleSheet.h>
  20. #include <LibWeb/CSS/StyleResolver.h>
  21. #include <LibWeb/CSS/StyleSheetList.h>
  22. #include <LibWeb/Cookie/Cookie.h>
  23. #include <LibWeb/DOM/DOMImplementation.h>
  24. #include <LibWeb/DOM/ExceptionOr.h>
  25. #include <LibWeb/DOM/NonElementParentNode.h>
  26. #include <LibWeb/DOM/ParentNode.h>
  27. #include <LibWeb/HTML/HTMLScriptElement.h>
  28. #include <LibWeb/HTML/History.h>
  29. namespace Web::DOM {
  30. enum class QuirksMode {
  31. No,
  32. Limited,
  33. Yes
  34. };
  35. class Document
  36. : public ParentNode
  37. , public NonElementParentNode<Document>
  38. , public HTML::GlobalEventHandlers
  39. , public Bindings::ScriptExecutionContext {
  40. public:
  41. using WrapperType = Bindings::DocumentWrapper;
  42. static NonnullRefPtr<Document> create(const URL& url = "about:blank")
  43. {
  44. return adopt_ref(*new Document(url));
  45. }
  46. static NonnullRefPtr<Document> create_with_global_object(Bindings::WindowObject&)
  47. {
  48. return Document::create();
  49. }
  50. virtual ~Document() override;
  51. String cookie(Cookie::Source = Cookie::Source::NonHttp);
  52. void set_cookie(String, Cookie::Source = Cookie::Source::NonHttp);
  53. String referrer() const;
  54. bool should_invalidate_styles_on_attribute_changes() const { return m_should_invalidate_styles_on_attribute_changes; }
  55. void set_should_invalidate_styles_on_attribute_changes(bool b) { m_should_invalidate_styles_on_attribute_changes = b; }
  56. void set_url(const URL& url) { m_url = url; }
  57. URL url() const { return m_url; }
  58. Origin origin() const;
  59. void set_origin(const Origin& origin);
  60. bool is_scripting_enabled() const { return true; }
  61. URL parse_url(String const&) const;
  62. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  63. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  64. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  65. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  66. NonnullRefPtr<CSS::StyleSheetList> style_sheets_for_bindings() { 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. Element* document_element();
  75. const Element* document_element() const;
  76. HTML::HTMLHtmlElement* html_element();
  77. HTML::HTMLHeadElement* head();
  78. HTML::HTMLElement* body();
  79. const HTML::HTMLHtmlElement* html_element() const
  80. {
  81. return const_cast<Document*>(this)->html_element();
  82. }
  83. const HTML::HTMLHeadElement* head() const
  84. {
  85. return const_cast<Document*>(this)->head();
  86. }
  87. const HTML::HTMLElement* body() const
  88. {
  89. return const_cast<Document*>(this)->body();
  90. }
  91. ExceptionOr<void> set_body(HTML::HTMLElement* new_body);
  92. String title() const;
  93. void set_title(const String&);
  94. void attach_to_browsing_context(Badge<BrowsingContext>, BrowsingContext&);
  95. void detach_from_browsing_context(Badge<BrowsingContext>, BrowsingContext&);
  96. BrowsingContext* browsing_context() { return m_browsing_context.ptr(); }
  97. const BrowsingContext* browsing_context() const { return m_browsing_context.ptr(); }
  98. Page* page();
  99. const Page* page() const;
  100. Color background_color(const Gfx::Palette&) const;
  101. RefPtr<Gfx::Bitmap> background_image() const;
  102. CSS::Repeat background_repeat_x() const;
  103. CSS::Repeat background_repeat_y() const;
  104. Color link_color() const;
  105. void set_link_color(Color);
  106. Color active_link_color() const;
  107. void set_active_link_color(Color);
  108. Color visited_link_color() const;
  109. void set_visited_link_color(Color);
  110. void force_layout();
  111. void invalidate_layout();
  112. void update_style();
  113. void update_layout();
  114. virtual bool is_child_allowed(const Node&) const override;
  115. const Layout::InitialContainingBlock* layout_node() const;
  116. Layout::InitialContainingBlock* layout_node();
  117. void schedule_style_update();
  118. void schedule_forced_layout();
  119. NonnullRefPtr<HTMLCollection> get_elements_by_name(String const&);
  120. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  121. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  122. NonnullRefPtr<HTMLCollection> applets();
  123. NonnullRefPtr<HTMLCollection> anchors();
  124. NonnullRefPtr<HTMLCollection> images();
  125. NonnullRefPtr<HTMLCollection> embeds();
  126. NonnullRefPtr<HTMLCollection> plugins();
  127. NonnullRefPtr<HTMLCollection> links();
  128. NonnullRefPtr<HTMLCollection> forms();
  129. NonnullRefPtr<HTMLCollection> scripts();
  130. const String& source() const { return m_source; }
  131. void set_source(const String& source) { m_source = source; }
  132. virtual JS::Interpreter& interpreter() override;
  133. JS::Value run_javascript(const StringView& source, const StringView& filename = "(unknown)");
  134. NonnullRefPtr<Element> create_element(const String& tag_name);
  135. NonnullRefPtr<Element> create_element_ns(const String& namespace_, const String& qualifed_name);
  136. NonnullRefPtr<DocumentFragment> create_document_fragment();
  137. NonnullRefPtr<Text> create_text_node(const String& data);
  138. NonnullRefPtr<Comment> create_comment(const String& data);
  139. NonnullRefPtr<Range> create_range();
  140. NonnullRefPtr<Event> create_event(const String& interface);
  141. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  142. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  143. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  144. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  145. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  146. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  147. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  148. QuirksMode mode() const { return m_quirks_mode; }
  149. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  150. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  151. void adopt_node(Node&);
  152. ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
  153. const DocumentType* doctype() const;
  154. const String& compat_mode() const;
  155. void set_editable(bool editable) { m_editable = editable; }
  156. virtual bool is_editable() const final;
  157. Element* focused_element() { return m_focused_element; }
  158. const Element* focused_element() const { return m_focused_element; }
  159. void set_focused_element(Element*);
  160. const Element* active_element() const { return m_active_element; }
  161. void set_active_element(Element*);
  162. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  163. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  164. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  165. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  166. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  167. const String& ready_state() const { return m_ready_state; }
  168. void set_ready_state(const String&);
  169. void ref_from_node(Badge<Node>)
  170. {
  171. increment_referencing_node_count();
  172. }
  173. void unref_from_node(Badge<Node>)
  174. {
  175. decrement_referencing_node_count();
  176. }
  177. void removed_last_ref();
  178. Window& window() { return *m_window; }
  179. Window* default_view() { return m_window; }
  180. const String& content_type() const { return m_content_type; }
  181. void set_content_type(const String& content_type) { m_content_type = content_type; }
  182. bool has_encoding() const { return m_encoding.has_value(); }
  183. const Optional<String>& encoding() const { return m_encoding; }
  184. String encoding_or_default() const { return m_encoding.value_or("UTF-8"); }
  185. void set_encoding(const Optional<String>& encoding) { m_encoding = encoding; }
  186. // NOTE: These are intended for the JS bindings
  187. String character_set() const { return encoding_or_default(); }
  188. String charset() const { return encoding_or_default(); }
  189. String input_encoding() const { return encoding_or_default(); }
  190. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  191. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  192. void completely_finish_loading();
  193. const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
  194. RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
  195. void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = script; }
  196. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  197. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  198. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  199. virtual EventTarget* get_parent(const Event&) override;
  200. String dump_dom_tree_as_json() const;
  201. bool has_a_style_sheet_that_is_blocking_scripts() const;
  202. bool is_fully_active() const;
  203. NonnullRefPtr<HTML::History> history() const { return m_history; }
  204. Bindings::LocationObject* location();
  205. private:
  206. explicit Document(const URL&);
  207. // ^DOM::Node
  208. virtual RefPtr<Layout::Node> create_layout_node() override;
  209. // ^HTML::GlobalEventHandlers
  210. virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }
  211. void tear_down_layout_tree();
  212. void increment_referencing_node_count()
  213. {
  214. VERIFY(!m_deletion_has_begun);
  215. ++m_referencing_node_count;
  216. }
  217. void decrement_referencing_node_count()
  218. {
  219. VERIFY(!m_deletion_has_begun);
  220. VERIFY(m_referencing_node_count);
  221. --m_referencing_node_count;
  222. if (!m_referencing_node_count && !ref_count()) {
  223. m_deletion_has_begun = true;
  224. delete this;
  225. }
  226. }
  227. unsigned m_referencing_node_count { 0 };
  228. OwnPtr<CSS::StyleResolver> m_style_resolver;
  229. RefPtr<CSS::StyleSheetList> m_style_sheets;
  230. RefPtr<Node> m_hovered_node;
  231. RefPtr<Node> m_inspected_node;
  232. WeakPtr<BrowsingContext> m_browsing_context;
  233. URL m_url;
  234. RefPtr<Window> m_window;
  235. RefPtr<Layout::InitialContainingBlock> m_layout_root;
  236. Optional<Color> m_link_color;
  237. Optional<Color> m_active_link_color;
  238. Optional<Color> m_visited_link_color;
  239. RefPtr<Core::Timer> m_style_update_timer;
  240. RefPtr<Core::Timer> m_forced_layout_timer;
  241. String m_source;
  242. OwnPtr<JS::Interpreter> m_interpreter;
  243. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  244. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  245. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  246. QuirksMode m_quirks_mode { QuirksMode::No };
  247. bool m_editable { false };
  248. WeakPtr<Element> m_focused_element;
  249. WeakPtr<Element> m_active_element;
  250. bool m_created_for_appropriate_template_contents { false };
  251. RefPtr<Document> m_associated_inert_template_document;
  252. String m_ready_state { "loading" };
  253. String m_content_type { "application/xml" };
  254. Optional<String> m_encoding;
  255. bool m_ready_for_post_load_tasks { false };
  256. NonnullRefPtr<DOMImplementation> m_implementation;
  257. RefPtr<HTML::HTMLScriptElement> m_current_script;
  258. bool m_should_invalidate_styles_on_attribute_changes { true };
  259. u32 m_ignore_destructive_writes_counter { 0 };
  260. // https://html.spec.whatwg.org/multipage/semantics.html#script-blocking-style-sheet-counter
  261. u32 m_script_blocking_style_sheet_counter { 0 };
  262. NonnullRefPtr<HTML::History> m_history;
  263. };
  264. }