Document.cpp 14 KB

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