Document.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/Function.h>
  28. #include <AK/NonnullRefPtrVector.h>
  29. #include <AK/OwnPtr.h>
  30. #include <AK/String.h>
  31. #include <AK/URL.h>
  32. #include <AK/WeakPtr.h>
  33. #include <LibCore/Forward.h>
  34. #include <LibHTML/CSS/StyleResolver.h>
  35. #include <LibHTML/CSS/StyleSheet.h>
  36. #include <LibHTML/DOM/ParentNode.h>
  37. class Frame;
  38. class HTMLBodyElement;
  39. class HTMLHtmlElement;
  40. class HTMLHeadElement;
  41. class LayoutDocument;
  42. class LayoutNode;
  43. class StyleResolver;
  44. class StyleSheet;
  45. class Document : public ParentNode {
  46. public:
  47. Document();
  48. virtual ~Document() override;
  49. void set_url(const URL& url) { m_url = url; }
  50. const URL& url() const { return m_url; }
  51. URL complete_url(const String&) const;
  52. void fixup();
  53. StyleResolver& style_resolver() { return *m_style_resolver; }
  54. const StyleResolver& style_resolver() const { return *m_style_resolver; }
  55. void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); }
  56. const NonnullRefPtrVector<StyleSheet>& stylesheets() const { return m_sheets; }
  57. virtual String tag_name() const override { return "#document"; }
  58. void set_hovered_node(Node*);
  59. Node* hovered_node() { return m_hovered_node; }
  60. const Node* hovered_node() const { return m_hovered_node; }
  61. void set_inspected_node(Node*);
  62. Node* inspected_node() { return m_inspected_node; }
  63. const Node* inspected_node() const { return m_inspected_node; }
  64. const HTMLHtmlElement* document_element() const;
  65. const HTMLHeadElement* head() const;
  66. const HTMLBodyElement* body() const;
  67. String title() const;
  68. void attach_to_frame(Badge<Frame>, Frame&);
  69. void detach_from_frame(Badge<Frame>, Frame&);
  70. Frame* frame() { return m_frame.ptr(); }
  71. const Frame* frame() const { return m_frame.ptr(); }
  72. Color background_color(const Gfx::Palette&) const;
  73. RefPtr<Gfx::Bitmap> background_image() const;
  74. Color link_color() const;
  75. void set_link_color(Color);
  76. Color active_link_color() const;
  77. void set_active_link_color(Color);
  78. Color visited_link_color() const;
  79. void set_visited_link_color(Color);
  80. void layout();
  81. void force_layout();
  82. void update_style();
  83. void update_layout();
  84. Function<void()> on_layout_updated;
  85. virtual bool is_child_allowed(const Node&) const override;
  86. const LayoutDocument* layout_node() const;
  87. LayoutDocument* layout_node();
  88. void schedule_style_update();
  89. const Element* get_element_by_id(const String&) const;
  90. Vector<const Element*> get_elements_by_name(const String&) const;
  91. const String& source() const { return m_source; }
  92. void set_source(const String& source) { m_source = source; }
  93. private:
  94. virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  95. OwnPtr<StyleResolver> m_style_resolver;
  96. NonnullRefPtrVector<StyleSheet> m_sheets;
  97. RefPtr<Node> m_hovered_node;
  98. RefPtr<Node> m_inspected_node;
  99. WeakPtr<Frame> m_frame;
  100. URL m_url;
  101. RefPtr<LayoutDocument> m_layout_root;
  102. Optional<Color> m_link_color;
  103. Optional<Color> m_active_link_color;
  104. Optional<Color> m_visited_link_color;
  105. RefPtr<Core::Timer> m_style_update_timer;
  106. String m_source;
  107. };
  108. template<>
  109. inline bool is<Document>(const Node& node)
  110. {
  111. return node.is_document();
  112. }