Document.cpp 14 KB

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