Document.cpp 9.6 KB

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