Document.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <AK/FileSystemPath.h>
  2. #include <AK/StringBuilder.h>
  3. #include <LibCore/CTimer.h>
  4. #include <LibHTML/CSS/StyleResolver.h>
  5. #include <LibHTML/DOM/Document.h>
  6. #include <LibHTML/DOM/DocumentType.h>
  7. #include <LibHTML/DOM/Element.h>
  8. #include <LibHTML/DOM/ElementFactory.h>
  9. #include <LibHTML/DOM/HTMLBodyElement.h>
  10. #include <LibHTML/DOM/HTMLHeadElement.h>
  11. #include <LibHTML/DOM/HTMLHtmlElement.h>
  12. #include <LibHTML/DOM/HTMLTitleElement.h>
  13. #include <LibHTML/Frame.h>
  14. #include <LibHTML/Layout/LayoutDocument.h>
  15. #include <LibHTML/Layout/LayoutTreeBuilder.h>
  16. #include <stdio.h>
  17. Document::Document()
  18. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  19. , m_style_resolver(make<StyleResolver>(*this))
  20. {
  21. m_style_update_timer = CTimer::construct();
  22. m_style_update_timer->set_single_shot(true);
  23. m_style_update_timer->set_interval(0);
  24. m_style_update_timer->on_timeout = [this] {
  25. update_style();
  26. };
  27. }
  28. Document::~Document()
  29. {
  30. }
  31. void Document::schedule_style_update()
  32. {
  33. if (m_style_update_timer->is_active())
  34. return;
  35. m_style_update_timer->start();
  36. }
  37. bool Document::is_child_allowed(const Node& node) const
  38. {
  39. switch (node.type()) {
  40. case NodeType::DOCUMENT_NODE:
  41. case NodeType::TEXT_NODE:
  42. return false;
  43. case NodeType::COMMENT_NODE:
  44. return true;
  45. case NodeType::DOCUMENT_TYPE_NODE:
  46. return !first_child_of_type<DocumentType>();
  47. case NodeType::ELEMENT_NODE:
  48. return !first_child_of_type<Element>();
  49. default:
  50. return false;
  51. }
  52. }
  53. void Document::fixup()
  54. {
  55. if (!first_child() || !is<DocumentType>(*first_child()))
  56. prepend_child(adopt(*new DocumentType(*this)));
  57. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  58. return;
  59. auto body = create_element(*this, "body");
  60. auto html = create_element(*this, "html");
  61. html->append_child(body);
  62. this->donate_all_children_to(body);
  63. this->append_child(html);
  64. }
  65. const HTMLHtmlElement* Document::document_element() const
  66. {
  67. return first_child_of_type<HTMLHtmlElement>();
  68. }
  69. const HTMLHeadElement* Document::head() const
  70. {
  71. auto* html = document_element();
  72. if (!html)
  73. return nullptr;
  74. return html->first_child_of_type<HTMLHeadElement>();
  75. }
  76. const HTMLBodyElement* Document::body() const
  77. {
  78. auto* html = document_element();
  79. if (!html)
  80. return nullptr;
  81. return html->first_child_of_type<HTMLBodyElement>();
  82. }
  83. String Document::title() const
  84. {
  85. auto* head_element = head();
  86. if (!head_element)
  87. return {};
  88. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  89. if (!title_element)
  90. return {};
  91. return title_element->text_content();
  92. }
  93. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  94. {
  95. m_frame = frame.make_weak_ptr();
  96. layout();
  97. }
  98. void Document::detach_from_frame(Badge<Frame>, Frame&)
  99. {
  100. m_layout_root = nullptr;
  101. m_frame = nullptr;
  102. }
  103. Color Document::background_color() const
  104. {
  105. auto* body_element = body();
  106. if (!body_element)
  107. return Color::White;
  108. auto* body_layout_node = body_element->layout_node();
  109. if (!body_layout_node)
  110. return Color::White;
  111. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  112. if (!background_color.has_value() || !background_color.value()->is_color())
  113. return Color::White;
  114. return background_color.value()->to_color(*this);
  115. }
  116. RefPtr<GraphicsBitmap> Document::background_image() const
  117. {
  118. auto* body_element = body();
  119. if (!body_element)
  120. return {};
  121. auto* body_layout_node = body_element->layout_node();
  122. if (!body_layout_node)
  123. return {};
  124. auto background_image = body_layout_node->style().property(CSS::PropertyID::BackgroundImage);
  125. if (!background_image.has_value() || !background_image.value()->is_image())
  126. return {};
  127. auto& image_value = static_cast<const ImageStyleValue&>(*background_image.value());
  128. if (!image_value.bitmap())
  129. return {};
  130. return *image_value.bitmap();
  131. }
  132. URL Document::complete_url(const String& string) const
  133. {
  134. URL url(string);
  135. if (url.is_valid())
  136. return url;
  137. FileSystemPath fspath(m_url.path());
  138. StringBuilder builder;
  139. builder.append('/');
  140. bool document_url_ends_in_slash = m_url.path()[m_url.path().length() - 1] == '/';
  141. for (int i = 0; i < fspath.parts().size(); ++i) {
  142. if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash)
  143. break;
  144. builder.append(fspath.parts()[i]);
  145. builder.append('/');
  146. }
  147. builder.append(string);
  148. auto built = builder.to_string();
  149. fspath = FileSystemPath(built);
  150. url = m_url;
  151. url.set_path(fspath.string());
  152. return url;
  153. }
  154. void Document::layout()
  155. {
  156. if (!m_layout_root) {
  157. LayoutTreeBuilder tree_builder;
  158. m_layout_root = tree_builder.build(*this);
  159. }
  160. m_layout_root->layout();
  161. }
  162. void Document::update_style()
  163. {
  164. for_each_in_subtree([&](Node& node) {
  165. if (!node.needs_style_update())
  166. return;
  167. to<Element>(node).recompute_style();
  168. });
  169. update_layout();
  170. }
  171. void Document::update_layout()
  172. {
  173. layout();
  174. if (on_layout_updated)
  175. on_layout_updated();
  176. }
  177. RefPtr<LayoutNode> Document::create_layout_node(const StyleProperties*) const
  178. {
  179. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  180. }
  181. void Document::set_link_color(Color color)
  182. {
  183. m_link_color = color;
  184. }
  185. void Document::set_active_link_color(Color color)
  186. {
  187. m_active_link_color = color;
  188. }
  189. void Document::set_visited_link_color(Color color)
  190. {
  191. m_visited_link_color = color;
  192. }
  193. const LayoutDocument* Document::layout_node() const
  194. {
  195. return static_cast<const LayoutDocument*>(Node::layout_node());
  196. }
  197. void Document::set_hovered_node(Node* node)
  198. {
  199. if (m_hovered_node == node)
  200. return;
  201. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  202. m_hovered_node = node;
  203. invalidate_style();
  204. }