Document.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/StringBuilder.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibGUI/Application.h>
  29. #include <LibGUI/DisplayLink.h>
  30. #include <LibGUI/MessageBox.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Parser.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/AttributeNames.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/Frame.h>
  53. #include <LibWeb/Layout/LayoutDocument.h>
  54. #include <LibWeb/Layout/LayoutTreeBuilder.h>
  55. #include <LibWeb/Origin.h>
  56. #include <LibWeb/PageView.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_style_sheets(CSS::StyleSheetList::create(*this))
  64. , m_url(url)
  65. , m_window(Window::create_with_document(*this))
  66. {
  67. HTML::AttributeNames::initialize();
  68. HTML::TagNames::initialize();
  69. m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
  70. update_style();
  71. });
  72. }
  73. Document::~Document()
  74. {
  75. }
  76. Origin Document::origin() const
  77. {
  78. if (!m_url.is_valid())
  79. return {};
  80. return { m_url.protocol(), m_url.host(), m_url.port() };
  81. }
  82. void Document::schedule_style_update()
  83. {
  84. if (m_style_update_timer->is_active())
  85. return;
  86. m_style_update_timer->start();
  87. }
  88. bool Document::is_child_allowed(const Node& node) const
  89. {
  90. switch (node.type()) {
  91. case NodeType::DOCUMENT_NODE:
  92. case NodeType::TEXT_NODE:
  93. return false;
  94. case NodeType::COMMENT_NODE:
  95. return true;
  96. case NodeType::DOCUMENT_TYPE_NODE:
  97. return !first_child_of_type<DocumentType>();
  98. case NodeType::ELEMENT_NODE:
  99. return !first_child_of_type<Element>();
  100. default:
  101. return false;
  102. }
  103. }
  104. void Document::fixup()
  105. {
  106. if (!first_child() || !is<DocumentType>(*first_child()))
  107. prepend_child(adopt(*new DocumentType(*this)));
  108. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  109. return;
  110. auto body = create_element("body");
  111. auto html = create_element("html");
  112. html->append_child(body);
  113. this->donate_all_children_to(body);
  114. this->append_child(html);
  115. }
  116. const HTMLHtmlElement* Document::document_element() const
  117. {
  118. return first_child_of_type<HTMLHtmlElement>();
  119. }
  120. const HTMLHeadElement* Document::head() const
  121. {
  122. auto* html = document_element();
  123. if (!html)
  124. return nullptr;
  125. return html->first_child_of_type<HTMLHeadElement>();
  126. }
  127. const HTMLElement* Document::body() const
  128. {
  129. auto* html = document_element();
  130. if (!html)
  131. return nullptr;
  132. return html->first_child_of_type<HTMLBodyElement>();
  133. }
  134. String Document::title() const
  135. {
  136. auto* head_element = head();
  137. if (!head_element)
  138. return {};
  139. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  140. if (!title_element)
  141. return {};
  142. return title_element->text_content();
  143. }
  144. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  145. {
  146. m_frame = frame.make_weak_ptr();
  147. for_each_in_subtree([&](auto& node) {
  148. node.document_did_attach_to_frame(frame);
  149. return IterationDecision::Continue;
  150. });
  151. layout();
  152. }
  153. void Document::detach_from_frame(Badge<Frame>, Frame& frame)
  154. {
  155. for_each_in_subtree([&](auto& node) {
  156. node.document_will_detach_from_frame(frame);
  157. return IterationDecision::Continue;
  158. });
  159. m_layout_root = nullptr;
  160. m_frame = nullptr;
  161. }
  162. Color Document::background_color(const Palette& palette) const
  163. {
  164. auto default_color = palette.base();
  165. auto* body_element = body();
  166. if (!body_element)
  167. return default_color;
  168. auto* body_layout_node = body_element->layout_node();
  169. if (!body_layout_node)
  170. return default_color;
  171. auto background_color = body_layout_node->specified_style().property(CSS::PropertyID::BackgroundColor);
  172. if (!background_color.has_value() || !background_color.value()->is_color())
  173. return default_color;
  174. return background_color.value()->to_color(*this);
  175. }
  176. RefPtr<Gfx::Bitmap> Document::background_image() const
  177. {
  178. auto* body_element = body();
  179. if (!body_element)
  180. return {};
  181. auto* body_layout_node = body_element->layout_node();
  182. if (!body_layout_node)
  183. return {};
  184. auto background_image = body_layout_node->specified_style().property(CSS::PropertyID::BackgroundImage);
  185. if (!background_image.has_value() || !background_image.value()->is_image())
  186. return {};
  187. auto& image_value = static_cast<const ImageStyleValue&>(*background_image.value());
  188. if (!image_value.bitmap())
  189. return {};
  190. return *image_value.bitmap();
  191. }
  192. URL Document::complete_url(const String& string) const
  193. {
  194. return m_url.complete_url(string);
  195. }
  196. void Document::invalidate_layout()
  197. {
  198. m_layout_root = nullptr;
  199. }
  200. void Document::force_layout()
  201. {
  202. invalidate_layout();
  203. layout();
  204. }
  205. void Document::layout()
  206. {
  207. if (!frame())
  208. return;
  209. if (!m_layout_root) {
  210. LayoutTreeBuilder tree_builder;
  211. m_layout_root = static_ptr_cast<LayoutDocument>(tree_builder.build(*this));
  212. }
  213. m_layout_root->layout();
  214. m_layout_root->set_needs_display();
  215. frame()->page().client().page_did_layout();
  216. }
  217. void Document::update_style()
  218. {
  219. for_each_in_subtree_of_type<Element>([&](auto& element) {
  220. if (element.needs_style_update())
  221. element.recompute_style();
  222. return IterationDecision::Continue;
  223. });
  224. update_layout();
  225. }
  226. void Document::update_layout()
  227. {
  228. if (!frame())
  229. return;
  230. layout();
  231. }
  232. RefPtr<LayoutNode> Document::create_layout_node(const StyleProperties*)
  233. {
  234. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  235. }
  236. void Document::set_link_color(Color color)
  237. {
  238. m_link_color = color;
  239. }
  240. void Document::set_active_link_color(Color color)
  241. {
  242. m_active_link_color = color;
  243. }
  244. void Document::set_visited_link_color(Color color)
  245. {
  246. m_visited_link_color = color;
  247. }
  248. const LayoutDocument* Document::layout_node() const
  249. {
  250. return static_cast<const LayoutDocument*>(Node::layout_node());
  251. }
  252. LayoutDocument* Document::layout_node()
  253. {
  254. return static_cast<LayoutDocument*>(Node::layout_node());
  255. }
  256. void Document::set_inspected_node(Node* node)
  257. {
  258. if (m_inspected_node == node)
  259. return;
  260. if (m_inspected_node && m_inspected_node->layout_node())
  261. m_inspected_node->layout_node()->set_needs_display();
  262. m_inspected_node = node;
  263. if (m_inspected_node && m_inspected_node->layout_node())
  264. m_inspected_node->layout_node()->set_needs_display();
  265. }
  266. void Document::set_hovered_node(Node* node)
  267. {
  268. if (m_hovered_node == node)
  269. return;
  270. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  271. m_hovered_node = node;
  272. invalidate_style();
  273. }
  274. Vector<const Element*> Document::get_elements_by_name(const String& name) const
  275. {
  276. Vector<const Element*> elements;
  277. for_each_in_subtree_of_type<Element>([&](auto& element) {
  278. if (element.attribute(HTML::AttributeNames::name) == name)
  279. elements.append(&element);
  280. return IterationDecision::Continue;
  281. });
  282. return elements;
  283. }
  284. NonnullRefPtrVector<Element> Document::get_elements_by_tag_name(const String& tag_name) const
  285. {
  286. NonnullRefPtrVector<Element> elements;
  287. for_each_in_subtree_of_type<Element>([&](auto& element) {
  288. if (element.tag_name() == tag_name)
  289. elements.append(element);
  290. return IterationDecision::Continue;
  291. });
  292. return elements;
  293. }
  294. RefPtr<Element> Document::query_selector(const StringView& selector_text)
  295. {
  296. auto selector = parse_selector(selector_text);
  297. if (!selector.has_value())
  298. return {};
  299. dump_selector(selector.value());
  300. RefPtr<Element> result;
  301. for_each_in_subtree_of_type<Element>([&](auto& element) {
  302. if (SelectorEngine::matches(selector.value(), element)) {
  303. result = element;
  304. return IterationDecision::Break;
  305. }
  306. return IterationDecision::Continue;
  307. });
  308. return result;
  309. }
  310. NonnullRefPtrVector<Element> Document::query_selector_all(const StringView& selector_text)
  311. {
  312. auto selector = parse_selector(selector_text);
  313. if (!selector.has_value())
  314. return {};
  315. dump_selector(selector.value());
  316. NonnullRefPtrVector<Element> elements;
  317. for_each_in_subtree_of_type<Element>([&](auto& element) {
  318. if (SelectorEngine::matches(selector.value(), element)) {
  319. elements.append(element);
  320. }
  321. return IterationDecision::Continue;
  322. });
  323. return elements;
  324. }
  325. Color Document::link_color() const
  326. {
  327. if (m_link_color.has_value())
  328. return m_link_color.value();
  329. if (!frame())
  330. return Color::Blue;
  331. return frame()->page().palette().link();
  332. }
  333. Color Document::active_link_color() const
  334. {
  335. if (m_active_link_color.has_value())
  336. return m_active_link_color.value();
  337. if (!frame())
  338. return Color::Red;
  339. return frame()->page().palette().active_link();
  340. }
  341. Color Document::visited_link_color() const
  342. {
  343. if (m_visited_link_color.has_value())
  344. return m_visited_link_color.value();
  345. if (!frame())
  346. return Color::Magenta;
  347. return frame()->page().palette().visited_link();
  348. }
  349. JS::Interpreter& Document::interpreter()
  350. {
  351. if (!m_interpreter)
  352. m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(*m_window);
  353. return *m_interpreter;
  354. }
  355. JS::Value Document::run_javascript(const StringView& source)
  356. {
  357. auto parser = JS::Parser(JS::Lexer(source));
  358. auto program = parser.parse_program();
  359. if (parser.has_errors()) {
  360. parser.print_errors();
  361. return JS::js_undefined();
  362. }
  363. return document().interpreter().run(document().interpreter().global_object(), *program);
  364. }
  365. NonnullRefPtr<Element> Document::create_element(const String& tag_name)
  366. {
  367. return Web::create_element(*this, tag_name);
  368. }
  369. NonnullRefPtr<Text> Document::create_text_node(const String& data)
  370. {
  371. return adopt(*new Text(*this, data));
  372. }
  373. void Document::set_pending_parsing_blocking_script(Badge<HTMLScriptElement>, HTMLScriptElement* script)
  374. {
  375. m_pending_parsing_blocking_script = script;
  376. }
  377. NonnullRefPtr<HTMLScriptElement> Document::take_pending_parsing_blocking_script(Badge<HTMLDocumentParser>)
  378. {
  379. return m_pending_parsing_blocking_script.release_nonnull();
  380. }
  381. void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTMLScriptElement>, HTMLScriptElement& script)
  382. {
  383. m_scripts_to_execute_when_parsing_has_finished.append(script);
  384. }
  385. NonnullRefPtrVector<HTMLScriptElement> Document::take_scripts_to_execute_when_parsing_has_finished(Badge<HTMLDocumentParser>)
  386. {
  387. return move(m_scripts_to_execute_when_parsing_has_finished);
  388. }
  389. void Document::add_script_to_execute_as_soon_as_possible(Badge<HTMLScriptElement>, HTMLScriptElement& script)
  390. {
  391. m_scripts_to_execute_as_soon_as_possible.append(script);
  392. }
  393. NonnullRefPtrVector<HTMLScriptElement> Document::take_scripts_to_execute_as_soon_as_possible(Badge<HTMLDocumentParser>)
  394. {
  395. return move(m_scripts_to_execute_as_soon_as_possible);
  396. }
  397. void Document::adopt_node(Node& subtree_root)
  398. {
  399. subtree_root.for_each_in_subtree([&](auto& node) {
  400. node.set_document({}, *this);
  401. return IterationDecision::Continue;
  402. });
  403. }
  404. }