Document.cpp 8.8 KB

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