Document.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <AK/FileSystemPath.h>
  2. #include <AK/StringBuilder.h>
  3. #include <LibCore/CTimer.h>
  4. #include <LibGUI/GApplication.h>
  5. #include <LibHTML/CSS/StyleResolver.h>
  6. #include <LibHTML/DOM/Document.h>
  7. #include <LibHTML/DOM/DocumentType.h>
  8. #include <LibHTML/DOM/Element.h>
  9. #include <LibHTML/DOM/ElementFactory.h>
  10. #include <LibHTML/DOM/HTMLBodyElement.h>
  11. #include <LibHTML/DOM/HTMLHeadElement.h>
  12. #include <LibHTML/DOM/HTMLHtmlElement.h>
  13. #include <LibHTML/DOM/HTMLTitleElement.h>
  14. #include <LibHTML/Frame.h>
  15. #include <LibHTML/HtmlView.h>
  16. #include <LibHTML/Layout/LayoutDocument.h>
  17. #include <LibHTML/Layout/LayoutTreeBuilder.h>
  18. #include <stdio.h>
  19. Document::Document()
  20. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  21. , m_style_resolver(make<StyleResolver>(*this))
  22. {
  23. m_style_update_timer = CTimer::construct();
  24. m_style_update_timer->set_single_shot(true);
  25. m_style_update_timer->set_interval(0);
  26. m_style_update_timer->on_timeout = [this] {
  27. update_style();
  28. };
  29. }
  30. Document::~Document()
  31. {
  32. }
  33. void Document::schedule_style_update()
  34. {
  35. if (m_style_update_timer->is_active())
  36. return;
  37. m_style_update_timer->start();
  38. }
  39. bool Document::is_child_allowed(const Node& node) const
  40. {
  41. switch (node.type()) {
  42. case NodeType::DOCUMENT_NODE:
  43. case NodeType::TEXT_NODE:
  44. return false;
  45. case NodeType::COMMENT_NODE:
  46. return true;
  47. case NodeType::DOCUMENT_TYPE_NODE:
  48. return !first_child_of_type<DocumentType>();
  49. case NodeType::ELEMENT_NODE:
  50. return !first_child_of_type<Element>();
  51. default:
  52. return false;
  53. }
  54. }
  55. void Document::fixup()
  56. {
  57. if (!first_child() || !is<DocumentType>(*first_child()))
  58. prepend_child(adopt(*new DocumentType(*this)));
  59. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  60. return;
  61. auto body = create_element(*this, "body");
  62. auto html = create_element(*this, "html");
  63. html->append_child(body);
  64. this->donate_all_children_to(body);
  65. this->append_child(html);
  66. }
  67. const HTMLHtmlElement* Document::document_element() const
  68. {
  69. return first_child_of_type<HTMLHtmlElement>();
  70. }
  71. const HTMLHeadElement* Document::head() const
  72. {
  73. auto* html = document_element();
  74. if (!html)
  75. return nullptr;
  76. return html->first_child_of_type<HTMLHeadElement>();
  77. }
  78. const HTMLBodyElement* Document::body() const
  79. {
  80. auto* html = document_element();
  81. if (!html)
  82. return nullptr;
  83. return html->first_child_of_type<HTMLBodyElement>();
  84. }
  85. String Document::title() const
  86. {
  87. auto* head_element = head();
  88. if (!head_element)
  89. return {};
  90. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  91. if (!title_element)
  92. return {};
  93. return title_element->text_content();
  94. }
  95. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  96. {
  97. m_frame = frame.make_weak_ptr();
  98. layout();
  99. }
  100. void Document::detach_from_frame(Badge<Frame>, Frame&)
  101. {
  102. m_layout_root = nullptr;
  103. m_frame = nullptr;
  104. }
  105. Color Document::background_color(const Palette& palette) const
  106. {
  107. auto default_color = palette.base();
  108. auto* body_element = body();
  109. if (!body_element)
  110. return default_color;
  111. auto* body_layout_node = body_element->layout_node();
  112. if (!body_layout_node)
  113. return default_color;
  114. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  115. if (!background_color.has_value() || !background_color.value()->is_color())
  116. return default_color;
  117. return background_color.value()->to_color(*this);
  118. }
  119. RefPtr<GraphicsBitmap> Document::background_image() const
  120. {
  121. auto* body_element = body();
  122. if (!body_element)
  123. return {};
  124. auto* body_layout_node = body_element->layout_node();
  125. if (!body_layout_node)
  126. return {};
  127. auto background_image = body_layout_node->style().property(CSS::PropertyID::BackgroundImage);
  128. if (!background_image.has_value() || !background_image.value()->is_image())
  129. return {};
  130. auto& image_value = static_cast<const ImageStyleValue&>(*background_image.value());
  131. if (!image_value.bitmap())
  132. return {};
  133. return *image_value.bitmap();
  134. }
  135. URL Document::complete_url(const String& string) const
  136. {
  137. return m_url.complete_url(string);
  138. }
  139. void Document::force_layout()
  140. {
  141. m_layout_root = nullptr;
  142. layout();
  143. }
  144. void Document::layout()
  145. {
  146. if (!m_layout_root) {
  147. LayoutTreeBuilder tree_builder;
  148. m_layout_root = tree_builder.build(*this);
  149. }
  150. m_layout_root->layout();
  151. m_layout_root->set_needs_display();
  152. }
  153. void Document::update_style()
  154. {
  155. for_each_in_subtree_of_type<Element>([&](auto& element) {
  156. if (element.needs_style_update())
  157. element.recompute_style();
  158. return IterationDecision::Continue;
  159. });
  160. update_layout();
  161. }
  162. void Document::update_layout()
  163. {
  164. if (!frame())
  165. return;
  166. layout();
  167. if (on_layout_updated)
  168. on_layout_updated();
  169. }
  170. RefPtr<LayoutNode> Document::create_layout_node(const StyleProperties*) const
  171. {
  172. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  173. }
  174. void Document::set_link_color(Color color)
  175. {
  176. m_link_color = color;
  177. }
  178. void Document::set_active_link_color(Color color)
  179. {
  180. m_active_link_color = color;
  181. }
  182. void Document::set_visited_link_color(Color color)
  183. {
  184. m_visited_link_color = color;
  185. }
  186. const LayoutDocument* Document::layout_node() const
  187. {
  188. return static_cast<const LayoutDocument*>(Node::layout_node());
  189. }
  190. LayoutDocument* Document::layout_node()
  191. {
  192. return static_cast<LayoutDocument*>(Node::layout_node());
  193. }
  194. void Document::set_inspected_node(Node* node)
  195. {
  196. if (m_inspected_node == node)
  197. return;
  198. if (m_inspected_node && m_inspected_node->layout_node())
  199. m_inspected_node->layout_node()->set_needs_display();
  200. m_inspected_node = node;
  201. if (m_inspected_node && m_inspected_node->layout_node())
  202. m_inspected_node->layout_node()->set_needs_display();
  203. }
  204. void Document::set_hovered_node(Node* node)
  205. {
  206. if (m_hovered_node == node)
  207. return;
  208. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  209. m_hovered_node = node;
  210. invalidate_style();
  211. }
  212. const Element* Document::get_element_by_id(const String& id) const
  213. {
  214. const Element* found_element = nullptr;
  215. for_each_in_subtree_of_type<Element>([&](auto& element) {
  216. if (element.attribute("id") == id) {
  217. found_element = &element;
  218. return IterationDecision::Break;
  219. }
  220. return IterationDecision::Continue;
  221. });
  222. return found_element;
  223. }
  224. Vector<const Element*> Document::get_elements_by_name(const String& name) const
  225. {
  226. Vector<const Element*> elements;
  227. for_each_in_subtree_of_type<Element>([&](auto& element) {
  228. if (element.attribute("name") == name)
  229. elements.append(&element);
  230. return IterationDecision::Continue;
  231. });
  232. return elements;
  233. }
  234. Color Document::link_color() const
  235. {
  236. if (m_link_color.has_value())
  237. return m_link_color.value();
  238. if (!frame())
  239. return Color::Blue;
  240. return frame()->html_view()->palette().link();
  241. }
  242. Color Document::active_link_color() const
  243. {
  244. if (m_active_link_color.has_value())
  245. return m_active_link_color.value();
  246. if (!frame())
  247. return Color::Red;
  248. return frame()->html_view()->palette().active_link();
  249. }
  250. Color Document::visited_link_color() const
  251. {
  252. if (m_visited_link_color.has_value())
  253. return m_visited_link_color.value();
  254. if (!frame())
  255. return Color::Magenta;
  256. return frame()->html_view()->palette().visited_link();
  257. }