Document.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Function.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/OwnPtr.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/CSS/CSSStyleSheet.h>
  19. #include <LibWeb/CSS/StyleSheetList.h>
  20. #include <LibWeb/Cookie/Cookie.h>
  21. #include <LibWeb/DOM/NonElementParentNode.h>
  22. #include <LibWeb/DOM/ParentNode.h>
  23. #include <LibWeb/HTML/BrowsingContext.h>
  24. #include <LibWeb/HTML/CrossOrigin/CrossOriginOpenerPolicy.h>
  25. #include <LibWeb/HTML/DocumentReadyState.h>
  26. #include <LibWeb/HTML/HTMLScriptElement.h>
  27. #include <LibWeb/HTML/History.h>
  28. #include <LibWeb/HTML/Origin.h>
  29. #include <LibWeb/HTML/SandboxingFlagSet.h>
  30. #include <LibWeb/HTML/Scripting/Environments.h>
  31. #include <LibWeb/HTML/VisibilityState.h>
  32. #include <LibWeb/HTML/Window.h>
  33. #include <LibWeb/HTML/WindowProxy.h>
  34. #include <LibWeb/WebIDL/ExceptionOr.h>
  35. namespace Web::DOM {
  36. enum class QuirksMode {
  37. No,
  38. Limited,
  39. Yes
  40. };
  41. // https://html.spec.whatwg.org/multipage/dom.html#document-load-timing-info
  42. struct DocumentLoadTimingInfo {
  43. // https://html.spec.whatwg.org/multipage/dom.html#navigation-start-time
  44. double navigation_start_time { 0 };
  45. // https://html.spec.whatwg.org/multipage/dom.html#dom-interactive-time
  46. double dom_interactive_time { 0 };
  47. // https://html.spec.whatwg.org/multipage/dom.html#dom-content-loaded-event-start-time
  48. double dom_content_loaded_event_start_time { 0 };
  49. // https://html.spec.whatwg.org/multipage/dom.html#dom-content-loaded-event-end-time
  50. double dom_content_loaded_event_end_time { 0 };
  51. // https://html.spec.whatwg.org/multipage/dom.html#dom-complete-time
  52. double dom_complete_time { 0 };
  53. // https://html.spec.whatwg.org/multipage/dom.html#load-event-start-time
  54. double load_event_start_time { 0 };
  55. // https://html.spec.whatwg.org/multipage/dom.html#load-event-end-time
  56. double load_event_end_time { 0 };
  57. };
  58. // https://html.spec.whatwg.org/multipage/dom.html#document-unload-timing-info
  59. struct DocumentUnloadTimingInfo {
  60. // https://html.spec.whatwg.org/multipage/dom.html#unload-event-start-time
  61. double unload_event_start_time { 0 };
  62. // https://html.spec.whatwg.org/multipage/dom.html#unload-event-end-time
  63. double unload_event_end_time { 0 };
  64. };
  65. struct ElementCreationOptions {
  66. DeprecatedString is;
  67. };
  68. enum class PolicyControlledFeature {
  69. Autoplay,
  70. };
  71. class Document
  72. : public ParentNode
  73. , public NonElementParentNode<Document>
  74. , public HTML::GlobalEventHandlers {
  75. WEB_PLATFORM_OBJECT(Document, ParentNode);
  76. public:
  77. enum class Type {
  78. XML,
  79. HTML
  80. };
  81. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create_and_initialize(Type, DeprecatedString content_type, HTML::NavigationParams);
  82. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create(JS::Realm&, AK::URL const& url = "about:blank"sv);
  83. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> construct_impl(JS::Realm&);
  84. virtual ~Document() override;
  85. JS::GCPtr<Selection::Selection> get_selection() const;
  86. DeprecatedString cookie(Cookie::Source = Cookie::Source::NonHttp);
  87. void set_cookie(DeprecatedString const&, Cookie::Source = Cookie::Source::NonHttp);
  88. DeprecatedString referrer() const;
  89. void set_referrer(DeprecatedString);
  90. void set_url(const AK::URL& url) { m_url = url; }
  91. AK::URL url() const { return m_url; }
  92. AK::URL fallback_base_url() const;
  93. AK::URL base_url() const;
  94. void update_base_element(Badge<HTML::HTMLBaseElement>);
  95. JS::GCPtr<HTML::HTMLBaseElement const> first_base_element_with_href_in_tree_order() const;
  96. DeprecatedString url_string() const { return m_url.to_deprecated_string(); }
  97. DeprecatedString document_uri() const { return m_url.to_deprecated_string(); }
  98. HTML::Origin origin() const;
  99. void set_origin(HTML::Origin const& origin);
  100. HTML::CrossOriginOpenerPolicy const& cross_origin_opener_policy() const { return m_cross_origin_opener_policy; }
  101. void set_cross_origin_opener_policy(HTML::CrossOriginOpenerPolicy policy) { m_cross_origin_opener_policy = move(policy); }
  102. AK::URL parse_url(StringView) const;
  103. CSS::StyleComputer& style_computer() { return *m_style_computer; }
  104. const CSS::StyleComputer& style_computer() const { return *m_style_computer; }
  105. CSS::StyleSheetList& style_sheets();
  106. CSS::StyleSheetList const& style_sheets() const;
  107. CSS::StyleSheetList* style_sheets_for_bindings() { return &style_sheets(); }
  108. virtual DeprecatedFlyString node_name() const override { return "#document"; }
  109. void set_hovered_node(Node*);
  110. Node* hovered_node() { return m_hovered_node.ptr(); }
  111. Node const* hovered_node() const { return m_hovered_node.ptr(); }
  112. void set_inspected_node(Node*, Optional<CSS::Selector::PseudoElement>);
  113. Node* inspected_node() { return m_inspected_node.ptr(); }
  114. Node const* inspected_node() const { return m_inspected_node.ptr(); }
  115. Layout::Node* inspected_layout_node();
  116. Layout::Node const* inspected_layout_node() const { return const_cast<Document*>(this)->inspected_layout_node(); }
  117. Element* document_element();
  118. Element const* document_element() const;
  119. HTML::HTMLHtmlElement* html_element();
  120. HTML::HTMLHeadElement* head();
  121. JS::GCPtr<HTML::HTMLTitleElement> title_element();
  122. HTML::HTMLElement* body();
  123. HTML::HTMLHtmlElement const* html_element() const
  124. {
  125. return const_cast<Document*>(this)->html_element();
  126. }
  127. HTML::HTMLHeadElement const* head() const
  128. {
  129. return const_cast<Document*>(this)->head();
  130. }
  131. JS::GCPtr<HTML::HTMLTitleElement const> title_element() const
  132. {
  133. return const_cast<Document*>(this)->title_element();
  134. }
  135. HTML::HTMLElement const* body() const
  136. {
  137. return const_cast<Document*>(this)->body();
  138. }
  139. WebIDL::ExceptionOr<void> set_body(HTML::HTMLElement* new_body);
  140. DeprecatedString title() const;
  141. WebIDL::ExceptionOr<void> set_title(DeprecatedString const&);
  142. HTML::BrowsingContext* browsing_context() { return m_browsing_context.ptr(); }
  143. HTML::BrowsingContext const* browsing_context() const { return m_browsing_context.ptr(); }
  144. void set_browsing_context(HTML::BrowsingContext*);
  145. Page* page();
  146. Page const* page() const;
  147. Color background_color() const;
  148. Vector<CSS::BackgroundLayerData> const* background_layers() const;
  149. Color link_color() const;
  150. void set_link_color(Color);
  151. Color active_link_color() const;
  152. void set_active_link_color(Color);
  153. Color visited_link_color() const;
  154. void set_visited_link_color(Color);
  155. void force_layout();
  156. void update_style();
  157. void update_layout();
  158. void set_needs_layout();
  159. void invalidate_layout();
  160. void invalidate_stacking_context_tree();
  161. virtual bool is_child_allowed(Node const&) const override;
  162. Layout::Viewport const* layout_node() const;
  163. Layout::Viewport* layout_node();
  164. void schedule_style_update();
  165. void schedule_layout_update();
  166. JS::NonnullGCPtr<HTMLCollection> get_elements_by_name(DeprecatedString const&);
  167. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(DeprecatedFlyString const&);
  168. JS::NonnullGCPtr<HTMLCollection> applets();
  169. JS::NonnullGCPtr<HTMLCollection> anchors();
  170. JS::NonnullGCPtr<HTMLCollection> images();
  171. JS::NonnullGCPtr<HTMLCollection> embeds();
  172. JS::NonnullGCPtr<HTMLCollection> plugins();
  173. JS::NonnullGCPtr<HTMLCollection> links();
  174. JS::NonnullGCPtr<HTMLCollection> forms();
  175. JS::NonnullGCPtr<HTMLCollection> scripts();
  176. JS::NonnullGCPtr<HTMLCollection> all();
  177. DeprecatedString const& source() const { return m_source; }
  178. void set_source(DeprecatedString source) { m_source = move(source); }
  179. HTML::EnvironmentSettingsObject& relevant_settings_object();
  180. void navigate_to_javascript_url(StringView url);
  181. void evaluate_javascript_url(StringView url);
  182. WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(DeprecatedString const& local_name, Variant<DeprecatedString, ElementCreationOptions> const& options);
  183. WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, Variant<DeprecatedString, ElementCreationOptions> const& options);
  184. JS::NonnullGCPtr<DocumentFragment> create_document_fragment();
  185. JS::NonnullGCPtr<Text> create_text_node(DeprecatedString const& data);
  186. JS::NonnullGCPtr<Comment> create_comment(DeprecatedString const& data);
  187. WebIDL::ExceptionOr<JS::NonnullGCPtr<ProcessingInstruction>> create_processing_instruction(DeprecatedString const& target, DeprecatedString const& data);
  188. WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute(DeprecatedString const& local_name);
  189. WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name);
  190. WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> create_event(DeprecatedString const& interface);
  191. JS::NonnullGCPtr<Range> create_range();
  192. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  193. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script.ptr(); }
  194. JS::NonnullGCPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>);
  195. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  196. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLParser>);
  197. Vector<JS::Handle<HTML::HTMLScriptElement>>& scripts_to_execute_when_parsing_has_finished() { return m_scripts_to_execute_when_parsing_has_finished; }
  198. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  199. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLParser>);
  200. Vector<JS::Handle<HTML::HTMLScriptElement>>& scripts_to_execute_as_soon_as_possible() { return m_scripts_to_execute_as_soon_as_possible; }
  201. void add_script_to_execute_in_order_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  202. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_in_order_as_soon_as_possible(Badge<HTML::HTMLParser>);
  203. Vector<JS::Handle<HTML::HTMLScriptElement>>& scripts_to_execute_in_order_as_soon_as_possible() { return m_scripts_to_execute_in_order_as_soon_as_possible; }
  204. QuirksMode mode() const { return m_quirks_mode; }
  205. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  206. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  207. Type document_type() const { return m_type; }
  208. void set_document_type(Type type) { m_type = type; }
  209. // https://dom.spec.whatwg.org/#html-document
  210. bool is_html_document() const { return m_type == Type::HTML; }
  211. // https://dom.spec.whatwg.org/#xml-document
  212. bool is_xml_document() const { return m_type == Type::XML; }
  213. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> import_node(JS::NonnullGCPtr<Node> node, bool deep);
  214. void adopt_node(Node&);
  215. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> adopt_node_binding(JS::NonnullGCPtr<Node>);
  216. DocumentType const* doctype() const;
  217. DeprecatedString const& compat_mode() const;
  218. void set_editable(bool editable) { m_editable = editable; }
  219. virtual bool is_editable() const final;
  220. Element* focused_element() { return m_focused_element.ptr(); }
  221. Element const* focused_element() const { return m_focused_element.ptr(); }
  222. void set_focused_element(Element*);
  223. Element const* active_element() const { return m_active_element.ptr(); }
  224. void set_active_element(Element*);
  225. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  226. JS::NonnullGCPtr<Document> appropriate_template_contents_owner_document();
  227. DeprecatedString ready_state() const;
  228. HTML::DocumentReadyState readiness() const { return m_readiness; }
  229. void update_readiness(HTML::DocumentReadyState);
  230. HTML::Window& window() const { return const_cast<HTML::Window&>(*m_window); }
  231. void set_window(HTML::Window&);
  232. WebIDL::ExceptionOr<void> write(Vector<DeprecatedString> const& strings);
  233. WebIDL::ExceptionOr<void> writeln(Vector<DeprecatedString> const& strings);
  234. WebIDL::ExceptionOr<Document*> open(DeprecatedString const& = "", DeprecatedString const& = "");
  235. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open(DeprecatedString const& url, DeprecatedString const& name, DeprecatedString const& features);
  236. WebIDL::ExceptionOr<void> close();
  237. HTML::Window* default_view() { return m_window.ptr(); }
  238. HTML::Window const* default_view() const { return m_window.ptr(); }
  239. DeprecatedString const& content_type() const { return m_content_type; }
  240. void set_content_type(DeprecatedString const& content_type) { m_content_type = content_type; }
  241. bool has_encoding() const { return m_encoding.has_value(); }
  242. Optional<DeprecatedString> const& encoding() const { return m_encoding; }
  243. DeprecatedString encoding_or_default() const { return m_encoding.value_or("UTF-8"); }
  244. void set_encoding(Optional<DeprecatedString> const& encoding) { m_encoding = encoding; }
  245. // NOTE: These are intended for the JS bindings
  246. DeprecatedString character_set() const { return encoding_or_default(); }
  247. DeprecatedString charset() const { return encoding_or_default(); }
  248. DeprecatedString input_encoding() const { return encoding_or_default(); }
  249. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  250. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  251. void completely_finish_loading();
  252. DOMImplementation* implementation();
  253. JS::GCPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script.ptr(); }
  254. void set_current_script(Badge<HTML::HTMLScriptElement>, JS::GCPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
  255. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  256. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  257. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  258. virtual EventTarget* get_parent(Event const&) override;
  259. DeprecatedString dump_dom_tree_as_json() const;
  260. bool has_a_style_sheet_that_is_blocking_scripts() const;
  261. bool is_fully_active() const;
  262. bool is_active() const;
  263. JS::NonnullGCPtr<HTML::History> history();
  264. JS::NonnullGCPtr<HTML::History> history() const;
  265. WebIDL::ExceptionOr<JS::GCPtr<HTML::Location>> location();
  266. size_t number_of_things_delaying_the_load_event() { return m_number_of_things_delaying_the_load_event; }
  267. void increment_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  268. void decrement_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  269. bool page_showing() const { return m_page_showing; }
  270. void set_page_showing(bool value) { m_page_showing = value; }
  271. bool hidden() const;
  272. DeprecatedString visibility_state() const;
  273. // https://html.spec.whatwg.org/multipage/interaction.html#update-the-visibility-state
  274. void update_the_visibility_state(HTML::VisibilityState);
  275. // NOTE: This does not fire any events, unlike update_the_visibility_state().
  276. void set_visibility_state(Badge<HTML::BrowsingContext>, HTML::VisibilityState);
  277. void run_the_resize_steps();
  278. void run_the_scroll_steps();
  279. void evaluate_media_queries_and_report_changes();
  280. void add_media_query_list(JS::NonnullGCPtr<CSS::MediaQueryList>);
  281. bool has_focus() const;
  282. void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
  283. void detach_parser(Badge<HTML::HTMLParser>);
  284. static bool is_valid_name(DeprecatedString const&);
  285. struct PrefixAndTagName {
  286. DeprecatedFlyString prefix;
  287. DeprecatedFlyString tag_name;
  288. };
  289. static WebIDL::ExceptionOr<PrefixAndTagName> validate_qualified_name(JS::Realm&, DeprecatedString const& qualified_name);
  290. JS::NonnullGCPtr<NodeIterator> create_node_iterator(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
  291. JS::NonnullGCPtr<TreeWalker> create_tree_walker(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
  292. void register_node_iterator(Badge<NodeIterator>, NodeIterator&);
  293. void unregister_node_iterator(Badge<NodeIterator>, NodeIterator&);
  294. void register_document_observer(Badge<DocumentObserver>, DocumentObserver&);
  295. void unregister_document_observer(Badge<DocumentObserver>, DocumentObserver&);
  296. template<typename Callback>
  297. void for_each_node_iterator(Callback callback)
  298. {
  299. for (auto& node_iterator : m_node_iterators)
  300. callback(*node_iterator);
  301. }
  302. bool needs_full_style_update() const { return m_needs_full_style_update; }
  303. void set_needs_full_style_update(bool b) { m_needs_full_style_update = b; }
  304. bool has_active_favicon() const { return m_active_favicon; }
  305. void check_favicon_after_loading_link_resource();
  306. JS::GCPtr<HTML::CustomElementDefinition> lookup_custom_element_definition(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& local_name, Optional<String> const& is) const;
  307. void increment_throw_on_dynamic_markup_insertion_counter(Badge<HTML::HTMLParser>);
  308. void decrement_throw_on_dynamic_markup_insertion_counter(Badge<HTML::HTMLParser>);
  309. // https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
  310. bool is_initial_about_blank() const { return m_is_initial_about_blank; }
  311. void set_is_initial_about_blank(bool b) { m_is_initial_about_blank = b; }
  312. DeprecatedString domain() const;
  313. void set_domain(DeprecatedString const& domain);
  314. auto& pending_scroll_event_targets() { return m_pending_scroll_event_targets; }
  315. auto& pending_scrollend_event_targets() { return m_pending_scrollend_event_targets; }
  316. // https://html.spec.whatwg.org/#completely-loaded
  317. bool is_completely_loaded() const;
  318. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-navigation-id
  319. Optional<String> navigation_id() const;
  320. void set_navigation_id(Optional<String>);
  321. // https://html.spec.whatwg.org/multipage/origin.html#active-sandboxing-flag-set
  322. HTML::SandboxingFlagSet active_sandboxing_flag_set() const;
  323. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
  324. HTML::PolicyContainer policy_container() const;
  325. // https://html.spec.whatwg.org/multipage/browsers.html#list-of-the-descendant-browsing-contexts
  326. Vector<JS::Handle<HTML::BrowsingContext>> list_of_descendant_browsing_contexts() const;
  327. Vector<JS::Handle<HTML::Navigable>> descendant_navigables();
  328. Vector<JS::Handle<HTML::Navigable>> inclusive_descendant_navigables();
  329. void destroy();
  330. // https://html.spec.whatwg.org/multipage/window-object.html#discard-a-document
  331. void discard();
  332. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#abort-a-document
  333. void abort();
  334. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-a-document
  335. void unload(bool recursive_flag = false, Optional<DocumentUnloadTimingInfo> = {});
  336. // https://html.spec.whatwg.org/multipage/dom.html#active-parser
  337. JS::GCPtr<HTML::HTMLParser> active_parser();
  338. // https://html.spec.whatwg.org/multipage/dom.html#load-timing-info
  339. DocumentLoadTimingInfo& load_timing_info() { return m_load_timing_info; }
  340. DocumentLoadTimingInfo const& load_timing_info() const { return m_load_timing_info; }
  341. void set_load_timing_info(DocumentLoadTimingInfo const& load_timing_info) { m_load_timing_info = load_timing_info; }
  342. // https://html.spec.whatwg.org/multipage/dom.html#previous-document-unload-timing
  343. DocumentUnloadTimingInfo& previous_document_unload_timing() { return m_previous_document_unload_timing; }
  344. DocumentUnloadTimingInfo const& previous_document_unload_timing() const { return m_previous_document_unload_timing; }
  345. void set_previous_document_unload_timing(DocumentUnloadTimingInfo const& previous_document_unload_timing) { m_previous_document_unload_timing = previous_document_unload_timing; }
  346. bool is_allowed_to_use_feature(PolicyControlledFeature) const;
  347. void did_stop_being_active_document_in_browsing_context(Badge<HTML::BrowsingContext>);
  348. bool query_command_supported(DeprecatedString const&) const;
  349. DeprecatedString dump_accessibility_tree_as_json();
  350. void make_active();
  351. void set_salvageable(bool value) { m_salvageable = value; }
  352. HTML::ListOfAvailableImages& list_of_available_images();
  353. HTML::ListOfAvailableImages const& list_of_available_images() const;
  354. void register_intersection_observer(Badge<IntersectionObserver::IntersectionObserver>, IntersectionObserver::IntersectionObserver&);
  355. void unregister_intersection_observer(Badge<IntersectionObserver::IntersectionObserver>, IntersectionObserver::IntersectionObserver&);
  356. void run_the_update_intersection_observations_steps(HighResolutionTime::DOMHighResTimeStamp time);
  357. void start_intersection_observing_a_lazy_loading_element(Element& element);
  358. protected:
  359. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  360. virtual void visit_edges(Cell::Visitor&) override;
  361. Document(JS::Realm&, AK::URL const&);
  362. private:
  363. // ^HTML::GlobalEventHandlers
  364. virtual EventTarget& global_event_handlers_to_event_target(FlyString const&) final { return *this; }
  365. void tear_down_layout_tree();
  366. void evaluate_media_rules();
  367. WebIDL::ExceptionOr<void> run_the_document_write_steps(DeprecatedString);
  368. void queue_intersection_observer_task();
  369. void queue_an_intersection_observer_entry(IntersectionObserver::IntersectionObserver&, HighResolutionTime::DOMHighResTimeStamp time, JS::NonnullGCPtr<Geometry::DOMRectReadOnly> root_bounds, JS::NonnullGCPtr<Geometry::DOMRectReadOnly> bounding_client_rect, JS::NonnullGCPtr<Geometry::DOMRectReadOnly> intersection_rect, bool is_intersecting, double intersection_ratio, JS::NonnullGCPtr<Element> target);
  370. OwnPtr<CSS::StyleComputer> m_style_computer;
  371. JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
  372. JS::GCPtr<Node> m_hovered_node;
  373. JS::GCPtr<Node> m_inspected_node;
  374. Optional<CSS::Selector::PseudoElement> m_inspected_pseudo_element;
  375. JS::GCPtr<Node> m_active_favicon;
  376. WeakPtr<HTML::BrowsingContext> m_browsing_context;
  377. AK::URL m_url;
  378. JS::GCPtr<HTML::Window> m_window;
  379. JS::GCPtr<Layout::Viewport> m_layout_root;
  380. Optional<Color> m_link_color;
  381. Optional<Color> m_active_link_color;
  382. Optional<Color> m_visited_link_color;
  383. RefPtr<Core::Timer> m_style_update_timer;
  384. RefPtr<Core::Timer> m_layout_update_timer;
  385. JS::GCPtr<HTML::HTMLParser> m_parser;
  386. bool m_active_parser_was_aborted { false };
  387. DeprecatedString m_source;
  388. JS::GCPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  389. Vector<JS::Handle<HTML::HTMLScriptElement>> m_scripts_to_execute_when_parsing_has_finished;
  390. // https://html.spec.whatwg.org/multipage/scripting.html#list-of-scripts-that-will-execute-in-order-as-soon-as-possible
  391. Vector<JS::Handle<HTML::HTMLScriptElement>> m_scripts_to_execute_in_order_as_soon_as_possible;
  392. // https://html.spec.whatwg.org/multipage/scripting.html#set-of-scripts-that-will-execute-as-soon-as-possible
  393. Vector<JS::Handle<HTML::HTMLScriptElement>> m_scripts_to_execute_as_soon_as_possible;
  394. QuirksMode m_quirks_mode { QuirksMode::No };
  395. // https://dom.spec.whatwg.org/#concept-document-type
  396. Type m_type { Type::HTML };
  397. bool m_editable { false };
  398. JS::GCPtr<Element> m_focused_element;
  399. JS::GCPtr<Element> m_active_element;
  400. bool m_created_for_appropriate_template_contents { false };
  401. JS::GCPtr<Document> m_associated_inert_template_document;
  402. JS::GCPtr<Document> m_appropriate_template_contents_owner_document;
  403. HTML::DocumentReadyState m_readiness { HTML::DocumentReadyState::Loading };
  404. DeprecatedString m_content_type { "application/xml" };
  405. Optional<DeprecatedString> m_encoding;
  406. bool m_ready_for_post_load_tasks { false };
  407. JS::GCPtr<DOMImplementation> m_implementation;
  408. JS::GCPtr<HTML::HTMLScriptElement> m_current_script;
  409. bool m_should_invalidate_styles_on_attribute_changes { true };
  410. u32 m_ignore_destructive_writes_counter { 0 };
  411. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-counter
  412. u32 m_unload_counter { 0 };
  413. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter
  414. u32 m_throw_on_dynamic_markup_insertion_counter { 0 };
  415. // https://html.spec.whatwg.org/multipage/semantics.html#script-blocking-style-sheet-counter
  416. u32 m_script_blocking_style_sheet_counter { 0 };
  417. JS::GCPtr<HTML::History> m_history;
  418. size_t m_number_of_things_delaying_the_load_event { 0 };
  419. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#concept-document-salvageable
  420. bool m_salvageable { true };
  421. // https://html.spec.whatwg.org/#page-showing
  422. bool m_page_showing { false };
  423. // Used by run_the_resize_steps().
  424. Gfx::IntSize m_last_viewport_size;
  425. // https://w3c.github.io/csswg-drafts/cssom-view-1/#document-pending-scroll-event-targets
  426. Vector<JS::NonnullGCPtr<EventTarget>> m_pending_scroll_event_targets;
  427. // https://w3c.github.io/csswg-drafts/cssom-view-1/#document-pending-scrollend-event-targets
  428. Vector<JS::NonnullGCPtr<EventTarget>> m_pending_scrollend_event_targets;
  429. // Used by evaluate_media_queries_and_report_changes().
  430. Vector<WeakPtr<CSS::MediaQueryList>> m_media_query_lists;
  431. bool m_needs_layout { false };
  432. bool m_needs_full_style_update { false };
  433. HashTable<JS::GCPtr<NodeIterator>> m_node_iterators;
  434. HashTable<JS::NonnullGCPtr<DocumentObserver>> m_document_observers;
  435. // https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
  436. bool m_is_initial_about_blank { false };
  437. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-coop
  438. HTML::CrossOriginOpenerPolicy m_cross_origin_opener_policy;
  439. // https://html.spec.whatwg.org/multipage/dom.html#the-document's-referrer
  440. DeprecatedString m_referrer { "" };
  441. // https://dom.spec.whatwg.org/#concept-document-origin
  442. HTML::Origin m_origin;
  443. JS::GCPtr<HTMLCollection> m_applets;
  444. JS::GCPtr<HTMLCollection> m_anchors;
  445. JS::GCPtr<HTMLCollection> m_images;
  446. JS::GCPtr<HTMLCollection> m_embeds;
  447. JS::GCPtr<HTMLCollection> m_links;
  448. JS::GCPtr<HTMLCollection> m_forms;
  449. JS::GCPtr<HTMLCollection> m_scripts;
  450. JS::GCPtr<HTMLCollection> m_all;
  451. // https://html.spec.whatwg.org/#completely-loaded-time
  452. Optional<AK::UnixDateTime> m_completely_loaded_time;
  453. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-navigation-id
  454. Optional<String> m_navigation_id;
  455. // https://html.spec.whatwg.org/multipage/origin.html#active-sandboxing-flag-set
  456. HTML::SandboxingFlagSet m_active_sandboxing_flag_set;
  457. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
  458. HTML::PolicyContainer m_policy_container;
  459. // https://html.spec.whatwg.org/multipage/interaction.html#visibility-state
  460. HTML::VisibilityState m_visibility_state { HTML::VisibilityState::Hidden };
  461. // https://html.spec.whatwg.org/multipage/dom.html#load-timing-info
  462. DocumentLoadTimingInfo m_load_timing_info;
  463. // https://html.spec.whatwg.org/multipage/dom.html#previous-document-unload-timing
  464. DocumentUnloadTimingInfo m_previous_document_unload_timing;
  465. // https://w3c.github.io/selection-api/#dfn-selection
  466. JS::GCPtr<Selection::Selection> m_selection;
  467. // NOTE: This is a cache to make finding the first <base href> element O(1).
  468. JS::GCPtr<HTML::HTMLBaseElement const> m_first_base_element_with_href_in_tree_order;
  469. // https://html.spec.whatwg.org/multipage/images.html#list-of-available-images
  470. OwnPtr<HTML::ListOfAvailableImages> m_list_of_available_images;
  471. // NOTE: Not in the spec per say, but Document must be able to access all IntersectionObservers whose root is in the document.
  472. OrderedHashTable<JS::NonnullGCPtr<IntersectionObserver::IntersectionObserver>> m_intersection_observers;
  473. // https://www.w3.org/TR/intersection-observer/#document-intersectionobservertaskqueued
  474. // Each document has an IntersectionObserverTaskQueued flag which is initialized to false.
  475. bool m_intersection_observer_task_queued { false };
  476. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-intersection-observer
  477. // Each Document has a lazy load intersection observer, initially set to null but can be set to an IntersectionObserver instance.
  478. JS::GCPtr<IntersectionObserver::IntersectionObserver> m_lazy_load_intersection_observer;
  479. };
  480. template<>
  481. inline bool Node::fast_is<Document>() const { return is_document(); }
  482. }