Document.h 17 KB

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