Document.h 17 KB

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