Document.cpp 10 KB

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