Document.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/CSS/SelectorEngine.h>
  37. #include <LibWeb/CSS/StyleResolver.h>
  38. #include <LibWeb/DOM/Document.h>
  39. #include <LibWeb/DOM/DocumentType.h>
  40. #include <LibWeb/DOM/Element.h>
  41. #include <LibWeb/DOM/ElementFactory.h>
  42. #include <LibWeb/DOM/HTMLBodyElement.h>
  43. #include <LibWeb/DOM/HTMLHeadElement.h>
  44. #include <LibWeb/DOM/HTMLHtmlElement.h>
  45. #include <LibWeb/DOM/HTMLTitleElement.h>
  46. #include <LibWeb/Dump.h>
  47. #include <LibWeb/Frame.h>
  48. #include <LibWeb/HtmlView.h>
  49. #include <LibWeb/Layout/LayoutDocument.h>
  50. #include <LibWeb/Layout/LayoutTreeBuilder.h>
  51. #include <LibWeb/Parser/CSSParser.h>
  52. #include <stdio.h>
  53. namespace Web {
  54. Document::Document()
  55. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  56. , m_style_resolver(make<StyleResolver>(*this))
  57. {
  58. m_style_update_timer = Core::Timer::construct();
  59. m_style_update_timer->set_single_shot(true);
  60. m_style_update_timer->set_interval(0);
  61. m_style_update_timer->on_timeout = [this] {
  62. update_style();
  63. };
  64. }
  65. Document::~Document()
  66. {
  67. }
  68. void Document::schedule_style_update()
  69. {
  70. if (m_style_update_timer->is_active())
  71. return;
  72. m_style_update_timer->start();
  73. }
  74. bool Document::is_child_allowed(const Node& node) const
  75. {
  76. switch (node.type()) {
  77. case NodeType::DOCUMENT_NODE:
  78. case NodeType::TEXT_NODE:
  79. return false;
  80. case NodeType::COMMENT_NODE:
  81. return true;
  82. case NodeType::DOCUMENT_TYPE_NODE:
  83. return !first_child_of_type<DocumentType>();
  84. case NodeType::ELEMENT_NODE:
  85. return !first_child_of_type<Element>();
  86. default:
  87. return false;
  88. }
  89. }
  90. void Document::fixup()
  91. {
  92. if (!first_child() || !is<DocumentType>(*first_child()))
  93. prepend_child(adopt(*new DocumentType(*this)));
  94. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  95. return;
  96. auto body = create_element(*this, "body");
  97. auto html = create_element(*this, "html");
  98. html->append_child(body);
  99. this->donate_all_children_to(body);
  100. this->append_child(html);
  101. }
  102. const HTMLHtmlElement* Document::document_element() const
  103. {
  104. return first_child_of_type<HTMLHtmlElement>();
  105. }
  106. const HTMLHeadElement* Document::head() const
  107. {
  108. auto* html = document_element();
  109. if (!html)
  110. return nullptr;
  111. return html->first_child_of_type<HTMLHeadElement>();
  112. }
  113. const HTMLBodyElement* Document::body() const
  114. {
  115. auto* html = document_element();
  116. if (!html)
  117. return nullptr;
  118. return html->first_child_of_type<HTMLBodyElement>();
  119. }
  120. String Document::title() const
  121. {
  122. auto* head_element = head();
  123. if (!head_element)
  124. return {};
  125. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  126. if (!title_element)
  127. return {};
  128. return title_element->text_content();
  129. }
  130. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  131. {
  132. m_frame = frame.make_weak_ptr();
  133. layout();
  134. }
  135. void Document::detach_from_frame(Badge<Frame>, Frame&)
  136. {
  137. m_layout_root = nullptr;
  138. m_frame = nullptr;
  139. }
  140. Color Document::background_color(const Palette& palette) const
  141. {
  142. auto default_color = palette.base();
  143. auto* body_element = body();
  144. if (!body_element)
  145. return default_color;
  146. auto* body_layout_node = body_element->layout_node();
  147. if (!body_layout_node)
  148. return default_color;
  149. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  150. if (!background_color.has_value() || !background_color.value()->is_color())
  151. return default_color;
  152. return background_color.value()->to_color(*this);
  153. }
  154. RefPtr<Gfx::Bitmap> Document::background_image() const
  155. {
  156. auto* body_element = body();
  157. if (!body_element)
  158. return {};
  159. auto* body_layout_node = body_element->layout_node();
  160. if (!body_layout_node)
  161. return {};
  162. auto background_image = body_layout_node->style().property(CSS::PropertyID::BackgroundImage);
  163. if (!background_image.has_value() || !background_image.value()->is_image())
  164. return {};
  165. auto& image_value = static_cast<const ImageStyleValue&>(*background_image.value());
  166. if (!image_value.bitmap())
  167. return {};
  168. return *image_value.bitmap();
  169. }
  170. URL Document::complete_url(const String& string) const
  171. {
  172. return m_url.complete_url(string);
  173. }
  174. void Document::invalidate_layout()
  175. {
  176. m_layout_root = nullptr;
  177. }
  178. void Document::force_layout()
  179. {
  180. invalidate_layout();
  181. layout();
  182. }
  183. void Document::layout()
  184. {
  185. if (!frame())
  186. return;
  187. if (!m_layout_root) {
  188. LayoutTreeBuilder tree_builder;
  189. m_layout_root = tree_builder.build(*this);
  190. }
  191. m_layout_root->layout();
  192. m_layout_root->set_needs_display();
  193. }
  194. void Document::update_style()
  195. {
  196. for_each_in_subtree_of_type<Element>([&](auto& element) {
  197. if (element.needs_style_update())
  198. element.recompute_style();
  199. return IterationDecision::Continue;
  200. });
  201. update_layout();
  202. }
  203. void Document::update_layout()
  204. {
  205. if (!frame())
  206. return;
  207. layout();
  208. if (on_layout_updated)
  209. on_layout_updated();
  210. }
  211. RefPtr<LayoutNode> Document::create_layout_node(const StyleProperties*) const
  212. {
  213. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  214. }
  215. void Document::set_link_color(Color color)
  216. {
  217. m_link_color = color;
  218. }
  219. void Document::set_active_link_color(Color color)
  220. {
  221. m_active_link_color = color;
  222. }
  223. void Document::set_visited_link_color(Color color)
  224. {
  225. m_visited_link_color = color;
  226. }
  227. const LayoutDocument* Document::layout_node() const
  228. {
  229. return static_cast<const LayoutDocument*>(Node::layout_node());
  230. }
  231. LayoutDocument* Document::layout_node()
  232. {
  233. return static_cast<LayoutDocument*>(Node::layout_node());
  234. }
  235. void Document::set_inspected_node(Node* node)
  236. {
  237. if (m_inspected_node == node)
  238. return;
  239. if (m_inspected_node && m_inspected_node->layout_node())
  240. m_inspected_node->layout_node()->set_needs_display();
  241. m_inspected_node = node;
  242. if (m_inspected_node && m_inspected_node->layout_node())
  243. m_inspected_node->layout_node()->set_needs_display();
  244. }
  245. void Document::set_hovered_node(Node* node)
  246. {
  247. if (m_hovered_node == node)
  248. return;
  249. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  250. m_hovered_node = node;
  251. invalidate_style();
  252. }
  253. Vector<const Element*> Document::get_elements_by_name(const String& name) const
  254. {
  255. Vector<const Element*> elements;
  256. for_each_in_subtree_of_type<Element>([&](auto& element) {
  257. if (element.attribute("name") == name)
  258. elements.append(&element);
  259. return IterationDecision::Continue;
  260. });
  261. return elements;
  262. }
  263. NonnullRefPtrVector<Element> Document::query_selector_all(const StringView& selector_text)
  264. {
  265. auto selector = parse_selector(selector_text);
  266. if (!selector.has_value())
  267. return {};
  268. NonnullRefPtrVector<Element> elements;
  269. for_each_in_subtree_of_type<Element>([&](auto& element) {
  270. if (SelectorEngine::matches(selector.value(), element)) {
  271. elements.append(element);
  272. }
  273. return IterationDecision::Continue;
  274. });
  275. return elements;
  276. }
  277. Color Document::link_color() const
  278. {
  279. if (m_link_color.has_value())
  280. return m_link_color.value();
  281. if (!frame())
  282. return Color::Blue;
  283. return frame()->html_view()->palette().link();
  284. }
  285. Color Document::active_link_color() const
  286. {
  287. if (m_active_link_color.has_value())
  288. return m_active_link_color.value();
  289. if (!frame())
  290. return Color::Red;
  291. return frame()->html_view()->palette().active_link();
  292. }
  293. Color Document::visited_link_color() const
  294. {
  295. if (m_visited_link_color.has_value())
  296. return m_visited_link_color.value();
  297. if (!frame())
  298. return Color::Magenta;
  299. return frame()->html_view()->palette().visited_link();
  300. }
  301. JS::Interpreter& Document::interpreter()
  302. {
  303. if (!m_interpreter) {
  304. m_interpreter = make<JS::Interpreter>();
  305. m_interpreter->global_object().put_native_function("alert", [](JS::Interpreter& interpreter) -> JS::Value {
  306. auto& arguments = interpreter.call_frame().arguments;
  307. if (arguments.size() < 1)
  308. return JS::js_undefined();
  309. GUI::MessageBox::show(arguments[0].to_string(), "Alert", GUI::MessageBox::Type::Information);
  310. return JS::js_undefined();
  311. });
  312. m_interpreter->global_object().put_native_function("setInterval", [this](JS::Interpreter& interpreter) -> JS::Value {
  313. auto& arguments = interpreter.call_frame().arguments;
  314. if (arguments.size() < 2)
  315. return JS::js_undefined();
  316. ASSERT(arguments[0].is_object());
  317. ASSERT(arguments[0].as_object()->is_function());
  318. auto callback = make_handle(const_cast<JS::Object*>(arguments[0].as_object()));
  319. // FIXME: This timer should not be leaked! It should also be removable with clearInterval()!
  320. (void)Core::Timer::construct(
  321. arguments[1].to_i32(), [this, callback] {
  322. auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()));
  323. m_interpreter->call(function);
  324. })
  325. .leak_ref();
  326. return JS::js_undefined();
  327. });
  328. m_interpreter->global_object().put_native_function("requestAnimationFrame", [this](JS::Interpreter& interpreter) -> JS::Value {
  329. auto& arguments = interpreter.call_frame().arguments;
  330. if (arguments.size() < 1)
  331. return JS::js_undefined();
  332. ASSERT(arguments[0].is_object());
  333. ASSERT(arguments[0].as_object()->is_function());
  334. auto callback = make_handle(const_cast<JS::Object*>(arguments[0].as_object()));
  335. // FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
  336. i32 link_id = GUI::DisplayLink::register_callback([this, callback](i32 link_id) {
  337. auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()));
  338. m_interpreter->call(function);
  339. GUI::DisplayLink::unregister_callback(link_id);
  340. });
  341. return JS::Value(link_id);
  342. });
  343. m_interpreter->global_object().put_native_function("cancelAnimationFrame", [](JS::Interpreter& interpreter) -> JS::Value {
  344. auto& arguments = interpreter.call_frame().arguments;
  345. if (arguments.size() < 1)
  346. return JS::js_undefined();
  347. // FIXME: We should not be passing untrusted numbers to DisplayLink::unregistered_callback()!
  348. GUI::DisplayLink::unregister_callback(arguments[0].to_i32());
  349. return JS::js_undefined();
  350. });
  351. m_interpreter->global_object().put_native_property(
  352. "document",
  353. [this](JS::Interpreter&) {
  354. return wrap(m_interpreter->heap(), *this);
  355. },
  356. nullptr);
  357. }
  358. return *m_interpreter;
  359. }
  360. }