Document.cpp 9.7 KB

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