Document.h 16 KB

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