Document.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2018-2022, 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/Vector.h>
  15. #include <AK/WeakPtr.h>
  16. #include <LibCore/Forward.h>
  17. #include <LibJS/Forward.h>
  18. #include <LibWeb/Bindings/WindowObject.h>
  19. #include <LibWeb/CSS/CSSStyleSheet.h>
  20. #include <LibWeb/CSS/StyleComputer.h>
  21. #include <LibWeb/CSS/StyleSheetList.h>
  22. #include <LibWeb/Cookie/Cookie.h>
  23. #include <LibWeb/DOM/ExceptionOr.h>
  24. #include <LibWeb/DOM/NonElementParentNode.h>
  25. #include <LibWeb/DOM/ParentNode.h>
  26. #include <LibWeb/HTML/DocumentReadyState.h>
  27. #include <LibWeb/HTML/HTMLScriptElement.h>
  28. #include <LibWeb/HTML/History.h>
  29. #include <LibWeb/HTML/Scripting/Environments.h>
  30. namespace Web::DOM {
  31. enum class QuirksMode {
  32. No,
  33. Limited,
  34. Yes
  35. };
  36. class Document
  37. : public ParentNode
  38. , public NonElementParentNode<Document>
  39. , public HTML::GlobalEventHandlers {
  40. public:
  41. using WrapperType = Bindings::DocumentWrapper;
  42. static NonnullRefPtr<Document> create(const AK::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 const&, 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 AK::URL& url) { m_url = url; }
  57. AK::URL url() const { return m_url; }
  58. String url_string() const { return m_url.to_string(); }
  59. String document_uri() const { return m_url.to_string(); }
  60. Origin origin() const;
  61. void set_origin(Origin const& origin);
  62. AK::URL parse_url(String const&) const;
  63. CSS::StyleComputer& style_computer() { return *m_style_computer; }
  64. const CSS::StyleComputer& style_computer() const { return *m_style_computer; }
  65. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  66. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  67. NonnullRefPtr<CSS::StyleSheetList> style_sheets_for_bindings() { return *m_style_sheets; }
  68. virtual FlyString node_name() const override { return "#document"; }
  69. void set_hovered_node(Node*);
  70. Node* hovered_node() { return m_hovered_node; }
  71. Node const* hovered_node() const { return m_hovered_node; }
  72. void set_inspected_node(Node*);
  73. Node* inspected_node() { return m_inspected_node; }
  74. Node const* inspected_node() const { return m_inspected_node; }
  75. Element* document_element();
  76. Element const* document_element() const;
  77. HTML::HTMLHtmlElement* html_element();
  78. HTML::HTMLHeadElement* head();
  79. HTML::HTMLElement* body();
  80. const HTML::HTMLHtmlElement* html_element() const
  81. {
  82. return const_cast<Document*>(this)->html_element();
  83. }
  84. const HTML::HTMLHeadElement* head() const
  85. {
  86. return const_cast<Document*>(this)->head();
  87. }
  88. const HTML::HTMLElement* body() const
  89. {
  90. return const_cast<Document*>(this)->body();
  91. }
  92. ExceptionOr<void> set_body(HTML::HTMLElement* new_body);
  93. String title() const;
  94. void set_title(String const&);
  95. void attach_to_browsing_context(Badge<HTML::BrowsingContext>, HTML::BrowsingContext&);
  96. void detach_from_browsing_context(Badge<HTML::BrowsingContext>, HTML::BrowsingContext&);
  97. HTML::BrowsingContext* browsing_context() { return m_browsing_context.ptr(); }
  98. HTML::BrowsingContext const* browsing_context() const { return m_browsing_context.ptr(); }
  99. Page* page();
  100. Page const* page() const;
  101. Color background_color(Gfx::Palette const&) const;
  102. Vector<CSS::BackgroundLayerData> const* background_layers() const;
  103. Color link_color() const;
  104. void set_link_color(Color);
  105. Color active_link_color() const;
  106. void set_active_link_color(Color);
  107. Color visited_link_color() const;
  108. void set_visited_link_color(Color);
  109. void force_layout();
  110. void update_style();
  111. void update_layout();
  112. void set_needs_layout();
  113. void invalidate_layout();
  114. void invalidate_stacking_context_tree();
  115. virtual bool is_child_allowed(Node const&) const override;
  116. Layout::InitialContainingBlock const* layout_node() const;
  117. Layout::InitialContainingBlock* layout_node();
  118. void schedule_style_update();
  119. void schedule_layout_update();
  120. NonnullRefPtr<HTMLCollection> get_elements_by_name(String 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. String const& source() const { return m_source; }
  131. void set_source(String const& source) { m_source = source; }
  132. HTML::EnvironmentSettingsObject& relevant_settings_object();
  133. JS::Realm& realm();
  134. JS::Interpreter& interpreter();
  135. JS::Value run_javascript(StringView source, StringView filename = "(unknown)");
  136. ExceptionOr<NonnullRefPtr<Element>> create_element(String const& tag_name);
  137. ExceptionOr<NonnullRefPtr<Element>> create_element_ns(String const& namespace_, String const& qualified_name);
  138. NonnullRefPtr<DocumentFragment> create_document_fragment();
  139. NonnullRefPtr<Text> create_text_node(String const& data);
  140. NonnullRefPtr<Comment> create_comment(String const& data);
  141. NonnullRefPtr<Range> create_range();
  142. NonnullRefPtr<Event> create_event(String const& interface);
  143. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  144. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  145. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>);
  146. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  147. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLParser>);
  148. NonnullRefPtrVector<HTML::HTMLScriptElement>& scripts_to_execute_when_parsing_has_finished() { return m_scripts_to_execute_when_parsing_has_finished; }
  149. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  150. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLParser>);
  151. NonnullRefPtrVector<HTML::HTMLScriptElement>& scripts_to_execute_as_soon_as_possible() { return m_scripts_to_execute_as_soon_as_possible; }
  152. QuirksMode mode() const { return m_quirks_mode; }
  153. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  154. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  155. ExceptionOr<NonnullRefPtr<Node>> import_node(NonnullRefPtr<Node> node, bool deep);
  156. void adopt_node(Node&);
  157. ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
  158. DocumentType const* doctype() const;
  159. String const& compat_mode() const;
  160. void set_editable(bool editable) { m_editable = editable; }
  161. virtual bool is_editable() const final;
  162. Element* focused_element() { return m_focused_element; }
  163. Element const* focused_element() const { return m_focused_element; }
  164. void set_focused_element(Element*);
  165. Element const* active_element() const { return m_active_element; }
  166. void set_active_element(Element*);
  167. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  168. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  169. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  170. Document const* associated_inert_template_document() const { return m_associated_inert_template_document; }
  171. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  172. String ready_state() const;
  173. void update_readiness(HTML::DocumentReadyState);
  174. void ref_from_node(Badge<Node>)
  175. {
  176. increment_referencing_node_count();
  177. }
  178. void unref_from_node(Badge<Node>)
  179. {
  180. decrement_referencing_node_count();
  181. }
  182. void removed_last_ref();
  183. HTML::Window& window() { return *m_window; }
  184. HTML::Window const& window() const { return *m_window; }
  185. ExceptionOr<void> write(Vector<String> const& strings);
  186. ExceptionOr<void> writeln(Vector<String> const& strings);
  187. ExceptionOr<Document*> open(String const& = "", String const& = "");
  188. ExceptionOr<void> close();
  189. HTML::Window* default_view() { return m_window; }
  190. String const& content_type() const { return m_content_type; }
  191. void set_content_type(String const& content_type) { m_content_type = content_type; }
  192. bool has_encoding() const { return m_encoding.has_value(); }
  193. Optional<String> const& encoding() const { return m_encoding; }
  194. String encoding_or_default() const { return m_encoding.value_or("UTF-8"); }
  195. void set_encoding(Optional<String> const& encoding) { m_encoding = encoding; }
  196. // NOTE: These are intended for the JS bindings
  197. String character_set() const { return encoding_or_default(); }
  198. String charset() const { return encoding_or_default(); }
  199. String input_encoding() const { return encoding_or_default(); }
  200. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  201. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  202. void completely_finish_loading();
  203. NonnullRefPtr<DOMImplementation> implementation() const;
  204. RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
  205. void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
  206. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  207. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  208. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  209. virtual EventTarget* get_parent(Event const&) override;
  210. String dump_dom_tree_as_json() const;
  211. bool has_a_style_sheet_that_is_blocking_scripts() const;
  212. bool is_fully_active() const;
  213. bool is_active() const;
  214. NonnullRefPtr<HTML::History> history() const { return m_history; }
  215. Bindings::LocationObject* location();
  216. size_t number_of_things_delaying_the_load_event() { return m_number_of_things_delaying_the_load_event; }
  217. void increment_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  218. void decrement_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  219. bool page_showing() const { return m_page_showing; }
  220. void set_page_showing(bool value) { m_page_showing = value; }
  221. bool hidden() const;
  222. String visibility_state() const;
  223. void run_the_resize_steps();
  224. void evaluate_media_queries_and_report_changes();
  225. void add_media_query_list(NonnullRefPtr<CSS::MediaQueryList>&);
  226. bool has_focus() const;
  227. void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
  228. void detach_parser(Badge<HTML::HTMLParser>);
  229. static bool is_valid_name(String const&);
  230. struct PrefixAndTagName {
  231. FlyString prefix;
  232. FlyString tag_name;
  233. };
  234. static ExceptionOr<PrefixAndTagName> validate_qualified_name(String const& qualified_name);
  235. NonnullRefPtr<NodeIterator> create_node_iterator(Node& root, unsigned what_to_show, RefPtr<NodeFilter>);
  236. NonnullRefPtr<TreeWalker> create_tree_walker(Node& root, unsigned what_to_show, RefPtr<NodeFilter>);
  237. void register_node_iterator(Badge<NodeIterator>, NodeIterator&);
  238. void unregister_node_iterator(Badge<NodeIterator>, NodeIterator&);
  239. template<typename Callback>
  240. void for_each_node_iterator(Callback callback)
  241. {
  242. for (auto* node_iterator : m_node_iterators)
  243. callback(*node_iterator);
  244. }
  245. bool needs_full_style_update() const { return m_needs_full_style_update; }
  246. void set_needs_full_style_update(bool b) { m_needs_full_style_update = b; }
  247. bool in_removed_last_ref() const { return m_in_removed_last_ref; }
  248. bool has_active_favicon() const { return m_active_favicon; }
  249. void check_favicon_after_loading_link_resource();
  250. private:
  251. explicit Document(const AK::URL&);
  252. // ^HTML::GlobalEventHandlers
  253. virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }
  254. void tear_down_layout_tree();
  255. void evaluate_media_rules();
  256. ExceptionOr<void> run_the_document_write_steps(String);
  257. void increment_referencing_node_count()
  258. {
  259. VERIFY(!m_deletion_has_begun);
  260. ++m_referencing_node_count;
  261. }
  262. void decrement_referencing_node_count()
  263. {
  264. VERIFY(!m_deletion_has_begun);
  265. VERIFY(m_referencing_node_count);
  266. --m_referencing_node_count;
  267. if (!m_referencing_node_count && !ref_count()) {
  268. m_deletion_has_begun = true;
  269. delete this;
  270. }
  271. }
  272. unsigned m_referencing_node_count { 0 };
  273. OwnPtr<CSS::StyleComputer> m_style_computer;
  274. RefPtr<CSS::StyleSheetList> m_style_sheets;
  275. RefPtr<Node> m_hovered_node;
  276. RefPtr<Node> m_inspected_node;
  277. RefPtr<Node> m_active_favicon;
  278. WeakPtr<HTML::BrowsingContext> m_browsing_context;
  279. AK::URL m_url;
  280. RefPtr<HTML::Window> m_window;
  281. RefPtr<Layout::InitialContainingBlock> m_layout_root;
  282. Optional<Color> m_link_color;
  283. Optional<Color> m_active_link_color;
  284. Optional<Color> m_visited_link_color;
  285. RefPtr<Core::Timer> m_style_update_timer;
  286. RefPtr<Core::Timer> m_layout_update_timer;
  287. RefPtr<HTML::HTMLParser> m_parser;
  288. bool m_active_parser_was_aborted { false };
  289. String m_source;
  290. OwnPtr<JS::Interpreter> m_interpreter;
  291. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  292. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  293. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  294. QuirksMode m_quirks_mode { QuirksMode::No };
  295. bool m_editable { false };
  296. WeakPtr<Element> m_focused_element;
  297. WeakPtr<Element> m_active_element;
  298. bool m_created_for_appropriate_template_contents { false };
  299. RefPtr<Document> m_associated_inert_template_document;
  300. HTML::DocumentReadyState m_readiness { HTML::DocumentReadyState::Loading };
  301. String m_content_type { "application/xml" };
  302. Optional<String> m_encoding;
  303. bool m_ready_for_post_load_tasks { false };
  304. NonnullOwnPtr<DOMImplementation> m_implementation;
  305. RefPtr<HTML::HTMLScriptElement> m_current_script;
  306. bool m_should_invalidate_styles_on_attribute_changes { true };
  307. u32 m_ignore_destructive_writes_counter { 0 };
  308. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-counter
  309. u32 m_unload_counter { 0 };
  310. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter
  311. u32 m_throw_on_dynamic_markup_insertion_counter { 0 };
  312. // https://html.spec.whatwg.org/multipage/semantics.html#script-blocking-style-sheet-counter
  313. u32 m_script_blocking_style_sheet_counter { 0 };
  314. NonnullRefPtr<HTML::History> m_history;
  315. size_t m_number_of_things_delaying_the_load_event { 0 };
  316. // https://html.spec.whatwg.org/#page-showing
  317. bool m_page_showing { false };
  318. // Used by run_the_resize_steps().
  319. Gfx::IntSize m_last_viewport_size;
  320. // Used by evaluate_media_queries_and_report_changes().
  321. Vector<WeakPtr<CSS::MediaQueryList>> m_media_query_lists;
  322. bool m_needs_layout { false };
  323. bool m_needs_full_style_update { false };
  324. HashTable<NodeIterator*> m_node_iterators;
  325. };
  326. }