Document.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/CSS/StyleResolver.h>
  37. #include <LibWeb/CSS/StyleSheet.h>
  38. #include <LibWeb/CSS/StyleSheetList.h>
  39. #include <LibWeb/DOM/NonElementParentNode.h>
  40. #include <LibWeb/DOM/ParentNode.h>
  41. namespace Web {
  42. class Document
  43. : public ParentNode
  44. , public NonElementParentNode<Document> {
  45. public:
  46. using WrapperType = Bindings::DocumentWrapper;
  47. explicit Document(const URL& = {});
  48. virtual ~Document() override;
  49. void set_url(const URL& url) { m_url = url; }
  50. URL url() const { return m_url; }
  51. Origin origin() const;
  52. bool is_scripting_enabled() const { return true; }
  53. URL complete_url(const String&) const;
  54. void fixup();
  55. StyleResolver& style_resolver() { return *m_style_resolver; }
  56. const StyleResolver& style_resolver() const { return *m_style_resolver; }
  57. CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
  58. const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
  59. virtual FlyString tag_name() const override { return "#document"; }
  60. void set_hovered_node(Node*);
  61. Node* hovered_node() { return m_hovered_node; }
  62. const Node* hovered_node() const { return m_hovered_node; }
  63. void set_inspected_node(Node*);
  64. Node* inspected_node() { return m_inspected_node; }
  65. const Node* inspected_node() const { return m_inspected_node; }
  66. const HTMLHtmlElement* document_element() const;
  67. const HTMLHeadElement* head() const;
  68. const HTMLBodyElement* body() const;
  69. String title() const;
  70. void attach_to_frame(Badge<Frame>, Frame&);
  71. void detach_from_frame(Badge<Frame>, Frame&);
  72. Frame* frame() { return m_frame.ptr(); }
  73. const Frame* frame() const { return m_frame.ptr(); }
  74. Color background_color(const Gfx::Palette&) const;
  75. RefPtr<Gfx::Bitmap> background_image() const;
  76. Color link_color() const;
  77. void set_link_color(Color);
  78. Color active_link_color() const;
  79. void set_active_link_color(Color);
  80. Color visited_link_color() const;
  81. void set_visited_link_color(Color);
  82. void layout();
  83. void force_layout();
  84. void invalidate_layout();
  85. void update_style();
  86. void update_layout();
  87. virtual bool is_child_allowed(const Node&) const override;
  88. const LayoutDocument* layout_node() const;
  89. LayoutDocument* layout_node();
  90. void schedule_style_update();
  91. Vector<const Element*> get_elements_by_name(const String&) const;
  92. RefPtr<Element> query_selector(const StringView&);
  93. NonnullRefPtrVector<Element> query_selector_all(const StringView&);
  94. const String& source() const { return m_source; }
  95. void set_source(const String& source) { m_source = source; }
  96. JS::Interpreter& interpreter();
  97. JS::Value run_javascript(const StringView&);
  98. NonnullRefPtr<Element> create_element(const String& tag_name);
  99. NonnullRefPtr<Text> create_text_node(const String& data);
  100. void set_pending_parsing_blocking_script(Badge<HTMLScriptElement>, HTMLScriptElement*);
  101. HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; }
  102. NonnullRefPtr<HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTMLDocumentParser>);
  103. void add_script_to_execute_when_parsing_has_finished(Badge<HTMLScriptElement>, HTMLScriptElement&);
  104. NonnullRefPtrVector<HTMLScriptElement> take_scripts_to_execute_when_parsing_has_finished(Badge<HTMLDocumentParser>);
  105. void add_script_to_execute_as_soon_as_possible(Badge<HTMLScriptElement>, HTMLScriptElement&);
  106. NonnullRefPtrVector<HTMLScriptElement> take_scripts_to_execute_as_soon_as_possible(Badge<HTMLDocumentParser>);
  107. bool in_quirks_mode() const { return m_quirks_mode; }
  108. void set_quirks_mode(bool mode) { m_quirks_mode = mode; }
  109. private:
  110. virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  111. OwnPtr<StyleResolver> m_style_resolver;
  112. RefPtr<CSS::StyleSheetList> m_style_sheets;
  113. RefPtr<Node> m_hovered_node;
  114. RefPtr<Node> m_inspected_node;
  115. WeakPtr<Frame> m_frame;
  116. URL m_url;
  117. RefPtr<Window> m_window;
  118. RefPtr<LayoutDocument> m_layout_root;
  119. Optional<Color> m_link_color;
  120. Optional<Color> m_active_link_color;
  121. Optional<Color> m_visited_link_color;
  122. RefPtr<Core::Timer> m_style_update_timer;
  123. String m_source;
  124. OwnPtr<JS::Interpreter> m_interpreter;
  125. RefPtr<HTMLScriptElement> m_pending_parsing_blocking_script;
  126. NonnullRefPtrVector<HTMLScriptElement> m_scripts_to_execute_when_parsing_has_finished;
  127. NonnullRefPtrVector<HTMLScriptElement> m_scripts_to_execute_as_soon_as_possible;
  128. bool m_quirks_mode { false };
  129. };
  130. template<>
  131. inline bool is<Document>(const Node& node)
  132. {
  133. return node.is_document();
  134. }
  135. }