Document.cpp 11 KB

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