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