Document.cpp 14 KB

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