Document.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/String.h>
  13. #include <AK/Vector.h>
  14. #include <AK/WeakPtr.h>
  15. #include <LibCore/Forward.h>
  16. #include <LibJS/Forward.h>
  17. #include <LibURL/URL.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/LazyLoadingElement.h>
  29. #include <LibWeb/HTML/Origin.h>
  30. #include <LibWeb/HTML/SandboxingFlagSet.h>
  31. #include <LibWeb/HTML/Scripting/Environments.h>
  32. #include <LibWeb/HTML/SharedImageRequest.h>
  33. #include <LibWeb/HTML/VisibilityState.h>
  34. #include <LibWeb/WebIDL/ExceptionOr.h>
  35. #include <LibWeb/WebIDL/ObservableArray.h>
  36. namespace Web::DOM {
  37. enum class QuirksMode {
  38. No,
  39. Limited,
  40. Yes
  41. };
  42. // https://html.spec.whatwg.org/multipage/dom.html#document-load-timing-info
  43. struct DocumentLoadTimingInfo {
  44. // https://html.spec.whatwg.org/multipage/dom.html#navigation-start-time
  45. double navigation_start_time { 0 };
  46. // https://html.spec.whatwg.org/multipage/dom.html#dom-interactive-time
  47. double dom_interactive_time { 0 };
  48. // https://html.spec.whatwg.org/multipage/dom.html#dom-content-loaded-event-start-time
  49. double dom_content_loaded_event_start_time { 0 };
  50. // https://html.spec.whatwg.org/multipage/dom.html#dom-content-loaded-event-end-time
  51. double dom_content_loaded_event_end_time { 0 };
  52. // https://html.spec.whatwg.org/multipage/dom.html#dom-complete-time
  53. double dom_complete_time { 0 };
  54. // https://html.spec.whatwg.org/multipage/dom.html#load-event-start-time
  55. double load_event_start_time { 0 };
  56. // https://html.spec.whatwg.org/multipage/dom.html#load-event-end-time
  57. double load_event_end_time { 0 };
  58. };
  59. // https://html.spec.whatwg.org/multipage/dom.html#document-unload-timing-info
  60. struct DocumentUnloadTimingInfo {
  61. // https://html.spec.whatwg.org/multipage/dom.html#unload-event-start-time
  62. double unload_event_start_time { 0 };
  63. // https://html.spec.whatwg.org/multipage/dom.html#unload-event-end-time
  64. double unload_event_end_time { 0 };
  65. };
  66. struct ElementCreationOptions {
  67. Optional<String> is;
  68. };
  69. enum class PolicyControlledFeature {
  70. Autoplay,
  71. };
  72. class Document
  73. : public ParentNode
  74. , public NonElementParentNode<Document>
  75. , public HTML::GlobalEventHandlers {
  76. WEB_PLATFORM_OBJECT(Document, ParentNode);
  77. JS_DECLARE_ALLOCATOR(Document);
  78. public:
  79. enum class Type {
  80. XML,
  81. HTML
  82. };
  83. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create_and_initialize(Type, String content_type, HTML::NavigationParams&);
  84. [[nodiscard]] static JS::NonnullGCPtr<Document> create(JS::Realm&, URL::URL const& url = "about:blank"sv);
  85. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> construct_impl(JS::Realm&);
  86. virtual ~Document() override;
  87. // AD-HOC: This number increments whenever a node is added or removed from the document, or an element attribute changes.
  88. // It can be used as a crude invalidation mechanism for caches that depend on the DOM structure.
  89. u64 dom_tree_version() const { return m_dom_tree_version; }
  90. void bump_dom_tree_version() { ++m_dom_tree_version; }
  91. WebIDL::ExceptionOr<void> populate_with_html_head_and_body();
  92. JS::GCPtr<Selection::Selection> get_selection() const;
  93. String cookie(Cookie::Source = Cookie::Source::NonHttp);
  94. void set_cookie(StringView, Cookie::Source = Cookie::Source::NonHttp);
  95. String referrer() const;
  96. void set_referrer(String);
  97. void set_url(const URL::URL& url) { m_url = url; }
  98. URL::URL url() const { return m_url; }
  99. URL::URL fallback_base_url() const;
  100. URL::URL base_url() const;
  101. void update_base_element(Badge<HTML::HTMLBaseElement>);
  102. JS::GCPtr<HTML::HTMLBaseElement const> first_base_element_with_href_in_tree_order() const;
  103. String url_string() const { return MUST(m_url.to_string()); }
  104. String document_uri() const { return url_string(); }
  105. HTML::Origin origin() const;
  106. void set_origin(HTML::Origin const& origin);
  107. HTML::CrossOriginOpenerPolicy const& cross_origin_opener_policy() const { return m_cross_origin_opener_policy; }
  108. void set_cross_origin_opener_policy(HTML::CrossOriginOpenerPolicy policy) { m_cross_origin_opener_policy = move(policy); }
  109. URL::URL parse_url(StringView) const;
  110. CSS::StyleComputer& style_computer() { return *m_style_computer; }
  111. const CSS::StyleComputer& style_computer() const { return *m_style_computer; }
  112. CSS::StyleSheetList& style_sheets();
  113. CSS::StyleSheetList const& style_sheets() const;
  114. void for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const;
  115. CSS::StyleSheetList* style_sheets_for_bindings() { return &style_sheets(); }
  116. virtual FlyString node_name() const override { return "#document"_fly_string; }
  117. void set_hovered_node(Node*);
  118. Node* hovered_node() { return m_hovered_node.ptr(); }
  119. Node const* hovered_node() const { return m_hovered_node.ptr(); }
  120. void set_inspected_node(Node*, Optional<CSS::Selector::PseudoElement::Type>);
  121. Node* inspected_node() { return m_inspected_node.ptr(); }
  122. Node const* inspected_node() const { return m_inspected_node.ptr(); }
  123. Layout::Node* inspected_layout_node();
  124. Layout::Node const* inspected_layout_node() const { return const_cast<Document*>(this)->inspected_layout_node(); }
  125. Element* document_element();
  126. Element const* document_element() const;
  127. HTML::HTMLHtmlElement* html_element();
  128. HTML::HTMLHeadElement* head();
  129. JS::GCPtr<HTML::HTMLTitleElement> title_element();
  130. HTML::HTMLElement* body();
  131. HTML::HTMLHtmlElement const* html_element() const
  132. {
  133. return const_cast<Document*>(this)->html_element();
  134. }
  135. HTML::HTMLHeadElement const* head() const
  136. {
  137. return const_cast<Document*>(this)->head();
  138. }
  139. JS::GCPtr<HTML::HTMLTitleElement const> title_element() const
  140. {
  141. return const_cast<Document*>(this)->title_element();
  142. }
  143. HTML::HTMLElement const* body() const
  144. {
  145. return const_cast<Document*>(this)->body();
  146. }
  147. WebIDL::ExceptionOr<void> set_body(HTML::HTMLElement* new_body);
  148. String title() const;
  149. WebIDL::ExceptionOr<void> set_title(String const&);
  150. HTML::BrowsingContext* browsing_context() { return m_browsing_context.ptr(); }
  151. HTML::BrowsingContext const* browsing_context() const { return m_browsing_context.ptr(); }
  152. void set_browsing_context(HTML::BrowsingContext*);
  153. Page& page();
  154. Page const& page() const;
  155. Color background_color() const;
  156. Vector<CSS::BackgroundLayerData> const* background_layers() const;
  157. Color link_color() const;
  158. void set_link_color(Color);
  159. Color active_link_color() const;
  160. void set_active_link_color(Color);
  161. Color visited_link_color() const;
  162. void set_visited_link_color(Color);
  163. void force_layout();
  164. void update_style();
  165. void update_layout();
  166. void update_paint_and_hit_testing_properties_if_needed();
  167. void update_animated_style_if_needed();
  168. void set_needs_layout();
  169. void invalidate_layout();
  170. void invalidate_stacking_context_tree();
  171. virtual bool is_child_allowed(Node const&) const override;
  172. Layout::Viewport const* layout_node() const;
  173. Layout::Viewport* layout_node();
  174. Painting::ViewportPaintable const* paintable() const;
  175. Painting::ViewportPaintable* paintable();
  176. void schedule_style_update();
  177. void schedule_layout_update();
  178. JS::NonnullGCPtr<HTMLCollection> get_elements_by_name(FlyString const&);
  179. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(StringView);
  180. JS::NonnullGCPtr<HTMLCollection> applets();
  181. JS::NonnullGCPtr<HTMLCollection> anchors();
  182. JS::NonnullGCPtr<HTMLCollection> images();
  183. JS::NonnullGCPtr<HTMLCollection> embeds();
  184. JS::NonnullGCPtr<HTMLCollection> plugins();
  185. JS::NonnullGCPtr<HTMLCollection> links();
  186. JS::NonnullGCPtr<HTMLCollection> forms();
  187. JS::NonnullGCPtr<HTMLCollection> scripts();
  188. JS::NonnullGCPtr<HTMLCollection> all();
  189. String const& source() const { return m_source; }
  190. void set_source(String source) { m_source = move(source); }
  191. HTML::EnvironmentSettingsObject& relevant_settings_object() const;
  192. WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(String const& local_name, Variant<String, ElementCreationOptions> const& options);
  193. WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element_ns(Optional<FlyString> const& namespace_, String const& qualified_name, Variant<String, ElementCreationOptions> const& options);
  194. JS::NonnullGCPtr<DocumentFragment> create_document_fragment();
  195. JS::NonnullGCPtr<Text> create_text_node(String const& data);
  196. WebIDL::ExceptionOr<JS::NonnullGCPtr<CDATASection>> create_cdata_section(String const& data);
  197. JS::NonnullGCPtr<Comment> create_comment(String const& data);
  198. WebIDL::ExceptionOr<JS::NonnullGCPtr<ProcessingInstruction>> create_processing_instruction(String const& target, String const& data);
  199. WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute(String const& local_name);
  200. WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute_ns(Optional<FlyString> const& namespace_, String const& qualified_name);
  201. WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> create_event(StringView interface);
  202. JS::NonnullGCPtr<Range> create_range();
  203. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  204. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script.ptr(); }
  205. JS::NonnullGCPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>);
  206. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  207. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLParser>);
  208. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>>& scripts_to_execute_when_parsing_has_finished() { return m_scripts_to_execute_when_parsing_has_finished; }
  209. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  210. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLParser>);
  211. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>>& scripts_to_execute_as_soon_as_possible() { return m_scripts_to_execute_as_soon_as_possible; }
  212. void add_script_to_execute_in_order_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  213. Vector<JS::Handle<HTML::HTMLScriptElement>> take_scripts_to_execute_in_order_as_soon_as_possible(Badge<HTML::HTMLParser>);
  214. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>>& scripts_to_execute_in_order_as_soon_as_possible() { return m_scripts_to_execute_in_order_as_soon_as_possible; }
  215. QuirksMode mode() const { return m_quirks_mode; }
  216. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  217. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  218. Type document_type() const { return m_type; }
  219. void set_document_type(Type type) { m_type = type; }
  220. // https://dom.spec.whatwg.org/#html-document
  221. bool is_html_document() const { return m_type == Type::HTML; }
  222. // https://dom.spec.whatwg.org/#xml-document
  223. bool is_xml_document() const { return m_type == Type::XML; }
  224. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> import_node(JS::NonnullGCPtr<Node> node, bool deep);
  225. void adopt_node(Node&);
  226. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> adopt_node_binding(JS::NonnullGCPtr<Node>);
  227. DocumentType const* doctype() const;
  228. String const& compat_mode() const;
  229. void set_editable(bool editable) { m_editable = editable; }
  230. virtual bool is_editable() const final;
  231. Element* focused_element() { return m_focused_element.ptr(); }
  232. Element const* focused_element() const { return m_focused_element.ptr(); }
  233. void set_focused_element(Element*);
  234. Element const* active_element() const { return m_active_element.ptr(); }
  235. void set_active_element(Element*);
  236. Element const* target_element() const { return m_target_element.ptr(); }
  237. void set_target_element(Element*);
  238. void scroll_to_the_fragment();
  239. void scroll_to_the_beginning_of_the_document();
  240. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  241. JS::NonnullGCPtr<Document> appropriate_template_contents_owner_document();
  242. StringView ready_state() const;
  243. HTML::DocumentReadyState readiness() const { return m_readiness; }
  244. void update_readiness(HTML::DocumentReadyState);
  245. [[nodiscard]] JS::GCPtr<HTML::Window> window() const { return m_window; }
  246. void set_window(HTML::Window&);
  247. WebIDL::ExceptionOr<void> write(Vector<String> const& strings);
  248. WebIDL::ExceptionOr<void> writeln(Vector<String> const& strings);
  249. WebIDL::ExceptionOr<Document*> open(Optional<String> const& = {}, Optional<String> const& = {});
  250. WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open(StringView url, StringView name, StringView features);
  251. WebIDL::ExceptionOr<void> close();
  252. HTML::Window* default_view() { return m_window.ptr(); }
  253. HTML::Window const* default_view() const { return m_window.ptr(); }
  254. String const& content_type() const { return m_content_type; }
  255. void set_content_type(String content_type) { m_content_type = move(content_type); }
  256. bool has_encoding() const { return m_encoding.has_value(); }
  257. Optional<String> const& encoding() const { return m_encoding; }
  258. String encoding_or_default() const { return m_encoding.value_or("UTF-8"_string); }
  259. void set_encoding(Optional<String> encoding) { m_encoding = move(encoding); }
  260. // NOTE: These are intended for the JS bindings
  261. String character_set() const { return encoding_or_default(); }
  262. String charset() const { return encoding_or_default(); }
  263. String input_encoding() const { return encoding_or_default(); }
  264. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  265. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  266. void completely_finish_loading();
  267. DOMImplementation* implementation();
  268. JS::GCPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script.ptr(); }
  269. void set_current_script(Badge<HTML::HTMLScriptElement>, JS::GCPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
  270. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  271. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  272. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  273. virtual EventTarget* get_parent(Event const&) override;
  274. String dump_dom_tree_as_json() const;
  275. bool has_a_style_sheet_that_is_blocking_scripts() const;
  276. bool is_fully_active() const;
  277. bool is_active() const;
  278. JS::NonnullGCPtr<HTML::History> history();
  279. JS::NonnullGCPtr<HTML::History> history() const;
  280. [[nodiscard]] JS::GCPtr<HTML::Location> location();
  281. bool anything_is_delaying_the_load_event() const;
  282. void increment_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  283. void decrement_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>);
  284. bool page_showing() const { return m_page_showing; }
  285. void set_page_showing(bool value) { m_page_showing = value; }
  286. bool hidden() const;
  287. StringView visibility_state() const;
  288. // https://html.spec.whatwg.org/multipage/interaction.html#update-the-visibility-state
  289. void update_the_visibility_state(HTML::VisibilityState);
  290. // NOTE: This does not fire any events, unlike update_the_visibility_state().
  291. void set_visibility_state(Badge<HTML::BrowsingContext>, HTML::VisibilityState);
  292. void run_the_resize_steps();
  293. void run_the_scroll_steps();
  294. void evaluate_media_queries_and_report_changes();
  295. void add_media_query_list(JS::NonnullGCPtr<CSS::MediaQueryList>);
  296. JS::NonnullGCPtr<CSS::VisualViewport> visual_viewport();
  297. [[nodiscard]] CSSPixelRect viewport_rect() const;
  298. class ViewportClient {
  299. public:
  300. virtual ~ViewportClient() = default;
  301. virtual void did_set_viewport_rect(CSSPixelRect const&) = 0;
  302. };
  303. void register_viewport_client(ViewportClient&);
  304. void unregister_viewport_client(ViewportClient&);
  305. void inform_all_viewport_clients_about_the_current_viewport_rect();
  306. bool has_focus() const;
  307. void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
  308. void detach_parser(Badge<HTML::HTMLParser>);
  309. void set_is_temporary_document_for_fragment_parsing(Badge<HTML::HTMLParser>) { m_temporary_document_for_fragment_parsing = true; }
  310. [[nodiscard]] bool is_temporary_document_for_fragment_parsing() const { return m_temporary_document_for_fragment_parsing; }
  311. static bool is_valid_name(String const&);
  312. struct PrefixAndTagName {
  313. FlyString prefix;
  314. FlyString tag_name;
  315. };
  316. static WebIDL::ExceptionOr<PrefixAndTagName> validate_qualified_name(JS::Realm&, FlyString const& qualified_name);
  317. JS::NonnullGCPtr<NodeIterator> create_node_iterator(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
  318. JS::NonnullGCPtr<TreeWalker> create_tree_walker(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
  319. void register_node_iterator(Badge<NodeIterator>, NodeIterator&);
  320. void unregister_node_iterator(Badge<NodeIterator>, NodeIterator&);
  321. void register_document_observer(Badge<DocumentObserver>, DocumentObserver&);
  322. void unregister_document_observer(Badge<DocumentObserver>, DocumentObserver&);
  323. template<typename Callback>
  324. void for_each_node_iterator(Callback callback)
  325. {
  326. for (auto& node_iterator : m_node_iterators)
  327. callback(*node_iterator);
  328. }
  329. bool needs_full_style_update() const { return m_needs_full_style_update; }
  330. void set_needs_full_style_update(bool b) { m_needs_full_style_update = b; }
  331. bool has_active_favicon() const { return m_active_favicon; }
  332. void check_favicon_after_loading_link_resource();
  333. JS::GCPtr<HTML::CustomElementDefinition> lookup_custom_element_definition(Optional<FlyString> const& namespace_, FlyString const& local_name, Optional<String> const& is) const;
  334. void increment_throw_on_dynamic_markup_insertion_counter(Badge<HTML::HTMLParser>);
  335. void decrement_throw_on_dynamic_markup_insertion_counter(Badge<HTML::HTMLParser>);
  336. // https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
  337. bool is_initial_about_blank() const { return m_is_initial_about_blank; }
  338. void set_is_initial_about_blank(bool b) { m_is_initial_about_blank = b; }
  339. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-about-base-url
  340. Optional<URL::URL> about_base_url() const { return m_about_base_url; }
  341. void set_about_base_url(Optional<URL::URL> url) { m_about_base_url = url; }
  342. String domain() const;
  343. void set_domain(String const&);
  344. auto& pending_scroll_event_targets() { return m_pending_scroll_event_targets; }
  345. auto& pending_scrollend_event_targets() { return m_pending_scrollend_event_targets; }
  346. // https://html.spec.whatwg.org/#completely-loaded
  347. bool is_completely_loaded() const;
  348. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-navigation-id
  349. Optional<String> navigation_id() const;
  350. void set_navigation_id(Optional<String>);
  351. // https://html.spec.whatwg.org/multipage/origin.html#active-sandboxing-flag-set
  352. HTML::SandboxingFlagSet active_sandboxing_flag_set() const;
  353. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
  354. HTML::PolicyContainer policy_container() const;
  355. Vector<JS::Handle<HTML::Navigable>> descendant_navigables();
  356. Vector<JS::Handle<HTML::Navigable>> const descendant_navigables() const;
  357. Vector<JS::Handle<HTML::Navigable>> inclusive_descendant_navigables();
  358. Vector<JS::Handle<HTML::Navigable>> ancestor_navigables();
  359. Vector<JS::Handle<HTML::Navigable>> inclusive_ancestor_navigables();
  360. Vector<JS::Handle<HTML::Navigable>> document_tree_child_navigables();
  361. void destroy();
  362. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#abort-a-document
  363. void abort();
  364. // https://html.spec.whatwg.org/multipage/document-lifecycle.html#unload-a-document
  365. void unload(JS::GCPtr<Document> new_document = nullptr);
  366. // https://html.spec.whatwg.org/multipage/dom.html#active-parser
  367. JS::GCPtr<HTML::HTMLParser> active_parser();
  368. // https://html.spec.whatwg.org/multipage/dom.html#load-timing-info
  369. DocumentLoadTimingInfo& load_timing_info() { return m_load_timing_info; }
  370. DocumentLoadTimingInfo const& load_timing_info() const { return m_load_timing_info; }
  371. void set_load_timing_info(DocumentLoadTimingInfo const& load_timing_info) { m_load_timing_info = load_timing_info; }
  372. // https://html.spec.whatwg.org/multipage/dom.html#previous-document-unload-timing
  373. DocumentUnloadTimingInfo& previous_document_unload_timing() { return m_previous_document_unload_timing; }
  374. DocumentUnloadTimingInfo const& previous_document_unload_timing() const { return m_previous_document_unload_timing; }
  375. void set_previous_document_unload_timing(DocumentUnloadTimingInfo const& previous_document_unload_timing) { m_previous_document_unload_timing = previous_document_unload_timing; }
  376. // https://w3c.github.io/editing/docs/execCommand/
  377. bool exec_command(String command_id, bool show_ui, String value);
  378. bool query_command_enabled(String command_id);
  379. bool query_command_indeterm(String command_id);
  380. bool query_command_state(String command_id);
  381. bool query_command_supported(String command_id);
  382. String query_command_value(String command_id);
  383. bool is_allowed_to_use_feature(PolicyControlledFeature) const;
  384. void did_stop_being_active_document_in_navigable();
  385. String dump_accessibility_tree_as_json();
  386. void make_active();
  387. void set_salvageable(bool value) { m_salvageable = value; }
  388. HTML::ListOfAvailableImages& list_of_available_images();
  389. HTML::ListOfAvailableImages const& list_of_available_images() const;
  390. void register_intersection_observer(Badge<IntersectionObserver::IntersectionObserver>, IntersectionObserver::IntersectionObserver&);
  391. void unregister_intersection_observer(Badge<IntersectionObserver::IntersectionObserver>, IntersectionObserver::IntersectionObserver&);
  392. void register_resize_observer(Badge<ResizeObserver::ResizeObserver>, ResizeObserver::ResizeObserver&);
  393. void unregister_resize_observer(Badge<ResizeObserver::ResizeObserver>, ResizeObserver::ResizeObserver&);
  394. void run_the_update_intersection_observations_steps(HighResolutionTime::DOMHighResTimeStamp time);
  395. void start_intersection_observing_a_lazy_loading_element(Element&);
  396. void shared_declarative_refresh_steps(StringView input, JS::GCPtr<HTML::HTMLMetaElement const> meta_element = nullptr);
  397. struct TopOfTheDocument { };
  398. using IndicatedPart = Variant<Element*, TopOfTheDocument>;
  399. IndicatedPart determine_the_indicated_part() const;
  400. u32 unload_counter() const { return m_unload_counter; }
  401. HTML::SourceSnapshotParams snapshot_source_snapshot_params() const;
  402. void update_for_history_step_application(JS::NonnullGCPtr<HTML::SessionHistoryEntry>, bool do_not_reactivate, size_t script_history_length, size_t script_history_index, Optional<Vector<JS::NonnullGCPtr<HTML::SessionHistoryEntry>>> entries_for_navigation_api = {}, bool update_navigation_api = true);
  403. HashMap<URL::URL, JS::GCPtr<HTML::SharedImageRequest>>& shared_image_requests();
  404. void restore_the_history_object_state(JS::NonnullGCPtr<HTML::SessionHistoryEntry> entry);
  405. JS::NonnullGCPtr<Animations::DocumentTimeline> timeline();
  406. void associate_with_timeline(JS::NonnullGCPtr<Animations::AnimationTimeline>);
  407. void disassociate_with_timeline(JS::NonnullGCPtr<Animations::AnimationTimeline>);
  408. struct PendingAnimationEvent {
  409. JS::NonnullGCPtr<DOM::Event> event;
  410. JS::NonnullGCPtr<Animations::Animation> target;
  411. Optional<double> scheduled_event_time;
  412. };
  413. void append_pending_animation_event(PendingAnimationEvent const&);
  414. void update_animations_and_send_events(Optional<double> const& timestamp);
  415. void remove_replaced_animations();
  416. void ensure_animation_timer();
  417. bool ready_to_run_scripts() const { return m_ready_to_run_scripts; }
  418. JS::GCPtr<HTML::SessionHistoryEntry> latest_entry() const { return m_latest_entry; }
  419. void set_latest_entry(JS::GCPtr<HTML::SessionHistoryEntry> e) { m_latest_entry = e; }
  420. void element_id_changed(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  421. void element_with_id_was_added(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  422. void element_with_id_was_removed(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  423. void element_name_changed(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  424. void element_with_name_was_added(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  425. void element_with_name_was_removed(Badge<DOM::Element>, JS::NonnullGCPtr<DOM::Element> element);
  426. void add_form_associated_element_with_form_attribute(HTML::FormAssociatedElement&);
  427. void remove_form_associated_element_with_form_attribute(HTML::FormAssociatedElement&);
  428. bool design_mode_enabled_state() const { return m_design_mode_enabled; }
  429. void set_design_mode_enabled_state(bool);
  430. String design_mode() const;
  431. WebIDL::ExceptionOr<void> set_design_mode(String const&);
  432. Element const* element_from_point(double x, double y);
  433. Vector<JS::NonnullGCPtr<Element>> elements_from_point(double x, double y);
  434. JS::GCPtr<Element const> scrolling_element() const;
  435. void set_needs_to_resolve_paint_only_properties() { m_needs_to_resolve_paint_only_properties = true; }
  436. void set_needs_animated_style_update() { m_needs_animated_style_update = true; }
  437. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
  438. virtual Vector<FlyString> supported_property_names() const override;
  439. Vector<JS::NonnullGCPtr<DOM::Element>> const& potentially_named_elements() const { return m_potentially_named_elements; }
  440. void gather_active_observations_at_depth(size_t depth);
  441. [[nodiscard]] size_t broadcast_active_resize_observations();
  442. [[nodiscard]] bool has_active_resize_observations();
  443. [[nodiscard]] bool has_skipped_resize_observations();
  444. JS::NonnullGCPtr<WebIDL::ObservableArray> adopted_style_sheets() const;
  445. WebIDL::ExceptionOr<void> set_adopted_style_sheets(JS::Value);
  446. void register_shadow_root(Badge<DOM::ShadowRoot>, DOM::ShadowRoot&);
  447. void unregister_shadow_root(Badge<DOM::ShadowRoot>, DOM::ShadowRoot&);
  448. void for_each_shadow_root(Function<void(DOM::ShadowRoot&)>&& callback);
  449. protected:
  450. virtual void initialize(JS::Realm&) override;
  451. virtual void visit_edges(Cell::Visitor&) override;
  452. Document(JS::Realm&, URL::URL const&);
  453. private:
  454. // ^HTML::GlobalEventHandlers
  455. virtual JS::GCPtr<EventTarget> global_event_handlers_to_event_target(FlyString const&) final { return *this; }
  456. void tear_down_layout_tree();
  457. void run_unloading_cleanup_steps();
  458. void evaluate_media_rules();
  459. WebIDL::ExceptionOr<void> run_the_document_write_steps(StringView);
  460. void queue_intersection_observer_task();
  461. 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);
  462. Element* find_a_potential_indicated_element(FlyString const& fragment) const;
  463. void dispatch_events_for_animation_if_necessary(JS::NonnullGCPtr<Animations::Animation>);
  464. JS::NonnullGCPtr<Page> m_page;
  465. OwnPtr<CSS::StyleComputer> m_style_computer;
  466. JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
  467. JS::GCPtr<Node> m_hovered_node;
  468. JS::GCPtr<Node> m_inspected_node;
  469. Optional<CSS::Selector::PseudoElement::Type> m_inspected_pseudo_element;
  470. JS::GCPtr<Node> m_active_favicon;
  471. WeakPtr<HTML::BrowsingContext> m_browsing_context;
  472. URL::URL m_url;
  473. JS::GCPtr<HTML::Window> m_window;
  474. JS::GCPtr<Layout::Viewport> m_layout_root;
  475. Optional<Color> m_link_color;
  476. Optional<Color> m_active_link_color;
  477. Optional<Color> m_visited_link_color;
  478. JS::GCPtr<HTML::HTMLParser> m_parser;
  479. bool m_active_parser_was_aborted { false };
  480. String m_source;
  481. JS::GCPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  482. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>> m_scripts_to_execute_when_parsing_has_finished;
  483. // https://html.spec.whatwg.org/multipage/scripting.html#list-of-scripts-that-will-execute-in-order-as-soon-as-possible
  484. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>> m_scripts_to_execute_in_order_as_soon_as_possible;
  485. // https://html.spec.whatwg.org/multipage/scripting.html#set-of-scripts-that-will-execute-as-soon-as-possible
  486. Vector<JS::NonnullGCPtr<HTML::HTMLScriptElement>> m_scripts_to_execute_as_soon_as_possible;
  487. QuirksMode m_quirks_mode { QuirksMode::No };
  488. // https://dom.spec.whatwg.org/#concept-document-type
  489. Type m_type { Type::XML };
  490. bool m_editable { false };
  491. JS::GCPtr<Element> m_focused_element;
  492. JS::GCPtr<Element> m_active_element;
  493. JS::GCPtr<Element> m_target_element;
  494. bool m_created_for_appropriate_template_contents { false };
  495. JS::GCPtr<Document> m_associated_inert_template_document;
  496. JS::GCPtr<Document> m_appropriate_template_contents_owner_document;
  497. HTML::DocumentReadyState m_readiness { HTML::DocumentReadyState::Loading };
  498. String m_content_type { "application/xml"_string };
  499. Optional<String> m_encoding;
  500. bool m_ready_for_post_load_tasks { false };
  501. JS::GCPtr<DOMImplementation> m_implementation;
  502. JS::GCPtr<HTML::HTMLScriptElement> m_current_script;
  503. bool m_should_invalidate_styles_on_attribute_changes { true };
  504. u32 m_ignore_destructive_writes_counter { 0 };
  505. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-counter
  506. u32 m_unload_counter { 0 };
  507. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter
  508. u32 m_throw_on_dynamic_markup_insertion_counter { 0 };
  509. // https://html.spec.whatwg.org/multipage/semantics.html#script-blocking-style-sheet-counter
  510. u32 m_script_blocking_style_sheet_counter { 0 };
  511. JS::GCPtr<HTML::History> m_history;
  512. size_t m_number_of_things_delaying_the_load_event { 0 };
  513. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#concept-document-salvageable
  514. bool m_salvageable { true };
  515. // https://html.spec.whatwg.org/#page-showing
  516. bool m_page_showing { false };
  517. // Used by run_the_resize_steps().
  518. Gfx::IntSize m_last_viewport_size;
  519. HashTable<ViewportClient*> m_viewport_clients;
  520. // https://w3c.github.io/csswg-drafts/cssom-view-1/#document-pending-scroll-event-targets
  521. Vector<JS::NonnullGCPtr<EventTarget>> m_pending_scroll_event_targets;
  522. // https://w3c.github.io/csswg-drafts/cssom-view-1/#document-pending-scrollend-event-targets
  523. Vector<JS::NonnullGCPtr<EventTarget>> m_pending_scrollend_event_targets;
  524. // Used by evaluate_media_queries_and_report_changes().
  525. Vector<WeakPtr<CSS::MediaQueryList>> m_media_query_lists;
  526. bool m_needs_layout { false };
  527. bool m_needs_full_style_update { false };
  528. bool m_needs_animated_style_update { false };
  529. HashTable<JS::GCPtr<NodeIterator>> m_node_iterators;
  530. HashTable<JS::NonnullGCPtr<DocumentObserver>> m_document_observers;
  531. // https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
  532. bool m_is_initial_about_blank { false };
  533. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-about-base-url
  534. Optional<URL::URL> m_about_base_url;
  535. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-coop
  536. HTML::CrossOriginOpenerPolicy m_cross_origin_opener_policy;
  537. // https://html.spec.whatwg.org/multipage/dom.html#the-document's-referrer
  538. String m_referrer;
  539. // https://dom.spec.whatwg.org/#concept-document-origin
  540. HTML::Origin m_origin;
  541. JS::GCPtr<HTMLCollection> m_applets;
  542. JS::GCPtr<HTMLCollection> m_anchors;
  543. JS::GCPtr<HTMLCollection> m_images;
  544. JS::GCPtr<HTMLCollection> m_embeds;
  545. JS::GCPtr<HTMLCollection> m_links;
  546. JS::GCPtr<HTMLCollection> m_forms;
  547. JS::GCPtr<HTMLCollection> m_scripts;
  548. JS::GCPtr<HTMLCollection> m_all;
  549. // https://html.spec.whatwg.org/#completely-loaded-time
  550. Optional<AK::UnixDateTime> m_completely_loaded_time;
  551. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-navigation-id
  552. Optional<String> m_navigation_id;
  553. // https://html.spec.whatwg.org/multipage/origin.html#active-sandboxing-flag-set
  554. HTML::SandboxingFlagSet m_active_sandboxing_flag_set;
  555. // https://html.spec.whatwg.org/multipage/dom.html#concept-document-policy-container
  556. HTML::PolicyContainer m_policy_container;
  557. // https://html.spec.whatwg.org/multipage/interaction.html#visibility-state
  558. HTML::VisibilityState m_visibility_state { HTML::VisibilityState::Hidden };
  559. // https://html.spec.whatwg.org/multipage/dom.html#load-timing-info
  560. DocumentLoadTimingInfo m_load_timing_info;
  561. // https://html.spec.whatwg.org/multipage/dom.html#previous-document-unload-timing
  562. DocumentUnloadTimingInfo m_previous_document_unload_timing;
  563. // https://w3c.github.io/selection-api/#dfn-selection
  564. JS::GCPtr<Selection::Selection> m_selection;
  565. // NOTE: This is a cache to make finding the first <base href> element O(1).
  566. JS::GCPtr<HTML::HTMLBaseElement const> m_first_base_element_with_href_in_tree_order;
  567. // https://html.spec.whatwg.org/multipage/images.html#list-of-available-images
  568. JS::GCPtr<HTML::ListOfAvailableImages> m_list_of_available_images;
  569. JS::GCPtr<CSS::VisualViewport> m_visual_viewport;
  570. // NOTE: Not in the spec per say, but Document must be able to access all IntersectionObservers whose root is in the document.
  571. OrderedHashTable<JS::NonnullGCPtr<IntersectionObserver::IntersectionObserver>> m_intersection_observers;
  572. // https://www.w3.org/TR/intersection-observer/#document-intersectionobservertaskqueued
  573. // Each document has an IntersectionObserverTaskQueued flag which is initialized to false.
  574. bool m_intersection_observer_task_queued { false };
  575. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-intersection-observer
  576. // Each Document has a lazy load intersection observer, initially set to null but can be set to an IntersectionObserver instance.
  577. JS::GCPtr<IntersectionObserver::IntersectionObserver> m_lazy_load_intersection_observer;
  578. Vector<JS::NonnullGCPtr<ResizeObserver::ResizeObserver>> m_resize_observers;
  579. // https://html.spec.whatwg.org/multipage/semantics.html#will-declaratively-refresh
  580. // A Document object has an associated will declaratively refresh (a boolean). It is initially false.
  581. bool m_will_declaratively_refresh { false };
  582. RefPtr<Core::Timer> m_active_refresh_timer;
  583. bool m_temporary_document_for_fragment_parsing { false };
  584. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#latest-entry
  585. JS::GCPtr<HTML::SessionHistoryEntry> m_latest_entry;
  586. HashMap<URL::URL, JS::GCPtr<HTML::SharedImageRequest>> m_shared_image_requests;
  587. // https://www.w3.org/TR/web-animations-1/#timeline-associated-with-a-document
  588. HashTable<JS::NonnullGCPtr<Animations::AnimationTimeline>> m_associated_animation_timelines;
  589. // https://www.w3.org/TR/web-animations-1/#document-default-document-timeline
  590. JS::GCPtr<Animations::DocumentTimeline> m_default_timeline;
  591. // https://www.w3.org/TR/web-animations-1/#pending-animation-event-queue
  592. Vector<PendingAnimationEvent> m_pending_animation_event_queue;
  593. RefPtr<Core::Timer> m_animation_driver_timer;
  594. bool m_needs_to_call_page_did_load { false };
  595. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#scripts-may-run-for-the-newly-created-document
  596. bool m_ready_to_run_scripts { false };
  597. Vector<HTML::FormAssociatedElement*> m_form_associated_elements_with_form_attribute;
  598. Vector<JS::NonnullGCPtr<DOM::Element>> m_potentially_named_elements;
  599. bool m_design_mode_enabled { false };
  600. bool m_needs_to_resolve_paint_only_properties { true };
  601. mutable JS::GCPtr<WebIDL::ObservableArray> m_adopted_style_sheets;
  602. Vector<JS::NonnullGCPtr<DOM::ShadowRoot>> m_shadow_roots;
  603. u64 m_dom_tree_version { 0 };
  604. };
  605. template<>
  606. inline bool Node::fast_is<Document>() const { return is_document(); }
  607. }