Document.cpp 15 KB

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