Document.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/Function.h>
  29. #include <AK/NonnullRefPtrVector.h>
  30. #include <AK/OwnPtr.h>
  31. #include <AK/String.h>
  32. #include <AK/URL.h>
  33. #include <AK/WeakPtr.h>
  34. #include <LibCore/Forward.h>
  35. #include <LibJS/Forward.h>
  36. #include <LibWeb/Bindings/ScriptExecutionContext.h>
  37. #include <LibWeb/Bindings/WindowObject.h>
  38. #include <LibWeb/CSS/CSSStyleSheet.h>
  39. #include <LibWeb/CSS/StyleResolver.h>
  40. #include <LibWeb/CSS/StyleSheetList.h>
  41. #include <LibWeb/Cookie/Cookie.h>
  42. #include <LibWeb/DOM/DOMImplementation.h>
  43. #include <LibWeb/DOM/ExceptionOr.h>
  44. #include <LibWeb/DOM/NonElementParentNode.h>
  45. #include <LibWeb/DOM/ParentNode.h>
  46. #include <LibWeb/HTML/HTMLScriptElement.h>
  47. namespace Web::DOM {
  48. enum class QuirksMode {
  49. No,
  50. Limited,
  51. Yes
  52. };
  53. class Document
  54. : public ParentNode
  55. , public NonElementParentNode<Document>
  56. , public HTML::GlobalEventHandlers
  57. , public Bindings::ScriptExecutionContext {
  58. public:
  59. using WrapperType = Bindings::DocumentWrapper;
  60. static NonnullRefPtr<Document> create(const URL& url = "about:blank")
  61. {
  62. return adopt(*new Document(url));
  63. }
  64. static NonnullRefPtr<Document> create_with_global_object(Bindings::WindowObject&)
  65. {
  66. return Document::create();
  67. }
  68. virtual ~Document() override;
  69. String cookie(Cookie::Source = Cookie::Source::NonHttp);
  70. void set_cookie(String, Cookie::Source = Cookie::Source::NonHttp);
  71. bool should_invalidate_styles_on_attribute_changes() const { return m_should_invalidate_styles_on_attribute_changes; }
  72. void set_should_invalidate_styles_on_attribute_changes(bool b) { m_should_invalidate_styles_on_attribute_changes = b; }
  73. void set_url(const URL& url) { m_url = url; }
  74. URL url() const { return m_url; }
  75. Origin origin() const;
  76. void set_origin(const Origin& origin);
  77. bool is_scripting_enabled() const { return true; }
  78. URL complete_url(const String&) const;
  79. CSS::StyleResolver& style_resolver() { return *m_style_resolver; }
  80. const CSS::StyleResolver& style_resolver() const { return *m_style_resolver; }
  81. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  82. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  83. NonnullRefPtr<CSS::StyleSheetList> style_sheets_for_bindings() { return *m_style_sheets; }
  84. virtual FlyString node_name() const override { return "#document"; }
  85. void set_hovered_node(Node*);
  86. Node* hovered_node() { return m_hovered_node; }
  87. const Node* hovered_node() const { return m_hovered_node; }
  88. void set_inspected_node(Node*);
  89. Node* inspected_node() { return m_inspected_node; }
  90. const Node* inspected_node() const { return m_inspected_node; }
  91. Element* document_element();
  92. const Element* document_element() const;
  93. const HTML::HTMLHtmlElement* html_element() const;
  94. const HTML::HTMLHeadElement* head() const;
  95. const HTML::HTMLElement* body() const;
  96. ExceptionOr<void> set_body(HTML::HTMLElement& new_body);
  97. String title() const;
  98. void set_title(const String&);
  99. void attach_to_frame(Badge<Frame>, Frame&);
  100. void detach_from_frame(Badge<Frame>, Frame&);
  101. Frame* frame() { return m_frame.ptr(); }
  102. const Frame* frame() const { return m_frame.ptr(); }
  103. Page* page();
  104. const Page* page() const;
  105. Color background_color(const Gfx::Palette&) const;
  106. RefPtr<Gfx::Bitmap> background_image() const;
  107. CSS::Repeat background_repeat_x() const;
  108. CSS::Repeat background_repeat_y() const;
  109. Color link_color() const;
  110. void set_link_color(Color);
  111. Color active_link_color() const;
  112. void set_active_link_color(Color);
  113. Color visited_link_color() const;
  114. void set_visited_link_color(Color);
  115. void force_layout();
  116. void invalidate_layout();
  117. void update_style();
  118. void update_layout();
  119. virtual bool is_child_allowed(const Node&) const override;
  120. const Layout::InitialContainingBlockBox* layout_node() const;
  121. Layout::InitialContainingBlockBox* layout_node();
  122. void schedule_style_update();
  123. void schedule_forced_layout();
  124. NonnullRefPtrVector<Element> get_elements_by_name(const String&) const;
  125. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  126. NonnullRefPtrVector<Element> get_elements_by_class_name(const FlyString&) const;
  127. const String& source() const { return m_source; }
  128. void set_source(const String& source) { m_source = source; }
  129. virtual JS::Interpreter& interpreter() override;
  130. JS::Value run_javascript(const StringView& source, const StringView& filename = "(unknown)");
  131. NonnullRefPtr<Element> create_element(const String& tag_name);
  132. NonnullRefPtr<Element> create_element_ns(const String& namespace_, const String& qualifed_name);
  133. NonnullRefPtr<DocumentFragment> create_document_fragment();
  134. NonnullRefPtr<Text> create_text_node(const String& data);
  135. NonnullRefPtr<Comment> create_comment(const String& data);
  136. NonnullRefPtr<Range> create_range();
  137. NonnullRefPtr<Event> create_event(const String& interface);
  138. void set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement*);
  139. HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  140. NonnullRefPtr<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>);
  141. void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  142. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>);
  143. void add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);
  144. NonnullRefPtrVector<HTML::HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>);
  145. QuirksMode mode() const { return m_quirks_mode; }
  146. bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
  147. void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
  148. void adopt_node(Node&);
  149. ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
  150. const DocumentType* doctype() const;
  151. const String& compat_mode() const;
  152. void set_editable(bool editable) { m_editable = editable; }
  153. virtual bool is_editable() const final;
  154. Element* focused_element() { return m_focused_element; }
  155. const Element* focused_element() const { return m_focused_element; }
  156. void set_focused_element(Element*);
  157. bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
  158. void set_created_for_appropriate_template_contents(bool value) { m_created_for_appropriate_template_contents = value; }
  159. Document* associated_inert_template_document() { return m_associated_inert_template_document; }
  160. const Document* associated_inert_template_document() const { return m_associated_inert_template_document; }
  161. void set_associated_inert_template_document(Document& document) { m_associated_inert_template_document = document; }
  162. const String& ready_state() const { return m_ready_state; }
  163. void set_ready_state(const String&);
  164. void ref_from_node(Badge<Node>)
  165. {
  166. increment_referencing_node_count();
  167. }
  168. void unref_from_node(Badge<Node>)
  169. {
  170. decrement_referencing_node_count();
  171. }
  172. void removed_last_ref();
  173. Window& window() { return *m_window; }
  174. Window* default_view() { return m_window; }
  175. const String& content_type() const { return m_content_type; }
  176. void set_content_type(const String& content_type) { m_content_type = content_type; }
  177. const String& encoding() const { return m_encoding; }
  178. void set_encoding(const String& encoding) { m_encoding = encoding; }
  179. // NOTE: These are intended for the JS bindings
  180. const String& character_set() const { return encoding(); }
  181. const String& charset() const { return encoding(); }
  182. const String& input_encoding() const { return encoding(); }
  183. bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
  184. void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
  185. void completely_finish_loading();
  186. const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
  187. RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
  188. void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = script; }
  189. u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }
  190. void increment_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter++; }
  191. void decrement_ignore_destructive_writes_counter() { m_ignore_destructive_writes_counter--; }
  192. virtual EventTarget* get_parent(const Event&) override;
  193. private:
  194. explicit Document(const URL&);
  195. // ^DOM::Node
  196. virtual RefPtr<Layout::Node> create_layout_node() override;
  197. // ^HTML::GlobalEventHandlers
  198. virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }
  199. void tear_down_layout_tree();
  200. void increment_referencing_node_count()
  201. {
  202. VERIFY(!m_deletion_has_begun);
  203. ++m_referencing_node_count;
  204. }
  205. void decrement_referencing_node_count()
  206. {
  207. VERIFY(!m_deletion_has_begun);
  208. VERIFY(m_referencing_node_count);
  209. --m_referencing_node_count;
  210. if (!m_referencing_node_count && !ref_count()) {
  211. m_deletion_has_begun = true;
  212. delete this;
  213. }
  214. }
  215. unsigned m_referencing_node_count { 0 };
  216. OwnPtr<CSS::StyleResolver> m_style_resolver;
  217. RefPtr<CSS::StyleSheetList> m_style_sheets;
  218. RefPtr<Node> m_hovered_node;
  219. RefPtr<Node> m_inspected_node;
  220. WeakPtr<Frame> m_frame;
  221. URL m_url;
  222. RefPtr<Window> m_window;
  223. RefPtr<Layout::InitialContainingBlockBox> m_layout_root;
  224. Optional<Color> m_link_color;
  225. Optional<Color> m_active_link_color;
  226. Optional<Color> m_visited_link_color;
  227. RefPtr<Core::Timer> m_style_update_timer;
  228. RefPtr<Core::Timer> m_forced_layout_timer;
  229. String m_source;
  230. OwnPtr<JS::Interpreter> m_interpreter;
  231. RefPtr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;
  232. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  233. NonnullRefPtrVector<HTML::HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  234. QuirksMode m_quirks_mode { QuirksMode::No };
  235. bool m_editable { false };
  236. WeakPtr<Element> m_focused_element;
  237. bool m_created_for_appropriate_template_contents { false };
  238. RefPtr<Document> m_associated_inert_template_document;
  239. String m_ready_state { "loading" };
  240. String m_content_type { "application/xml" };
  241. String m_encoding { "UTF-8" };
  242. bool m_ready_for_post_load_tasks { false };
  243. NonnullRefPtr<DOMImplementation> m_implementation;
  244. RefPtr<HTML::HTMLScriptElement> m_current_script;
  245. bool m_should_invalidate_styles_on_attribute_changes { true };
  246. u32 m_ignore_destructive_writes_counter { 0 };
  247. };
  248. }