Document.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Utf8View.h>
  29. #include <LibCore/Timer.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Parser.h>
  32. #include <LibJS/Runtime/Function.h>
  33. #include <LibWeb/Bindings/MainThreadVM.h>
  34. #include <LibWeb/Bindings/WindowObject.h>
  35. #include <LibWeb/CSS/StyleResolver.h>
  36. #include <LibWeb/DOM/Comment.h>
  37. #include <LibWeb/DOM/DOMException.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/ExceptionOr.h>
  45. #include <LibWeb/DOM/Range.h>
  46. #include <LibWeb/DOM/ShadowRoot.h>
  47. #include <LibWeb/DOM/Text.h>
  48. #include <LibWeb/DOM/Window.h>
  49. #include <LibWeb/Dump.h>
  50. #include <LibWeb/HTML/AttributeNames.h>
  51. #include <LibWeb/HTML/EventNames.h>
  52. #include <LibWeb/HTML/HTMLBodyElement.h>
  53. #include <LibWeb/HTML/HTMLFrameSetElement.h>
  54. #include <LibWeb/HTML/HTMLHeadElement.h>
  55. #include <LibWeb/HTML/HTMLHtmlElement.h>
  56. #include <LibWeb/HTML/HTMLScriptElement.h>
  57. #include <LibWeb/HTML/HTMLTitleElement.h>
  58. #include <LibWeb/InProcessWebView.h>
  59. #include <LibWeb/Layout/BlockFormattingContext.h>
  60. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  61. #include <LibWeb/Layout/TreeBuilder.h>
  62. #include <LibWeb/Namespace.h>
  63. #include <LibWeb/Origin.h>
  64. #include <LibWeb/Page/Frame.h>
  65. #include <LibWeb/SVG/TagNames.h>
  66. #include <LibWeb/UIEvents/MouseEvent.h>
  67. #include <ctype.h>
  68. namespace Web::DOM {
  69. Document::Document(const URL& url)
  70. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  71. , m_style_resolver(make<CSS::StyleResolver>(*this))
  72. , m_style_sheets(CSS::StyleSheetList::create(*this))
  73. , m_url(url)
  74. , m_window(Window::create_with_document(*this))
  75. , m_implementation(DOMImplementation::create(*this))
  76. {
  77. m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
  78. update_style();
  79. });
  80. m_forced_layout_timer = Core::Timer::create_single_shot(0, [this] {
  81. force_layout();
  82. });
  83. }
  84. Document::~Document()
  85. {
  86. }
  87. void Document::removed_last_ref()
  88. {
  89. VERIFY(!ref_count());
  90. VERIFY(!m_deletion_has_begun);
  91. if (m_referencing_node_count) {
  92. // The document has reached ref_count==0 but still has nodes keeping it alive.
  93. // At this point, sever all the node links we control.
  94. // If nodes remain elsewhere (e.g JS wrappers), they will keep the document alive.
  95. // NOTE: This makes sure we stay alive across for the duration of the cleanup below.
  96. increment_referencing_node_count();
  97. m_focused_element = nullptr;
  98. m_hovered_node = nullptr;
  99. m_pending_parsing_blocking_script = nullptr;
  100. m_inspected_node = nullptr;
  101. m_scripts_to_execute_when_parsing_has_finished.clear();
  102. m_scripts_to_execute_as_soon_as_possible.clear();
  103. m_associated_inert_template_document = nullptr;
  104. m_interpreter = nullptr;
  105. {
  106. // Gather up all the descendants of this document and prune them from the tree.
  107. // FIXME: This could definitely be more elegant.
  108. NonnullRefPtrVector<Node> descendants;
  109. for_each_in_inclusive_subtree([&](auto& node) {
  110. if (&node != this)
  111. descendants.append(node);
  112. return IterationDecision::Continue;
  113. });
  114. for (auto& node : descendants) {
  115. VERIFY(&node.document() == this);
  116. VERIFY(!node.is_document());
  117. if (node.parent())
  118. node.remove();
  119. }
  120. }
  121. m_in_removed_last_ref = false;
  122. decrement_referencing_node_count();
  123. return;
  124. }
  125. m_in_removed_last_ref = false;
  126. m_deletion_has_begun = true;
  127. delete this;
  128. }
  129. Origin Document::origin() const
  130. {
  131. if (!m_url.is_valid())
  132. return {};
  133. return { m_url.protocol(), m_url.host(), m_url.port() };
  134. }
  135. void Document::set_origin(const Origin& origin)
  136. {
  137. m_url.set_protocol(origin.protocol());
  138. m_url.set_host(origin.host());
  139. m_url.set_port(origin.port());
  140. }
  141. void Document::schedule_style_update()
  142. {
  143. if (m_style_update_timer->is_active())
  144. return;
  145. m_style_update_timer->start();
  146. }
  147. void Document::schedule_forced_layout()
  148. {
  149. if (m_forced_layout_timer->is_active())
  150. return;
  151. m_forced_layout_timer->start();
  152. }
  153. bool Document::is_child_allowed(const Node& node) const
  154. {
  155. switch (node.type()) {
  156. case NodeType::DOCUMENT_NODE:
  157. case NodeType::TEXT_NODE:
  158. return false;
  159. case NodeType::COMMENT_NODE:
  160. return true;
  161. case NodeType::DOCUMENT_TYPE_NODE:
  162. return !first_child_of_type<DocumentType>();
  163. case NodeType::ELEMENT_NODE:
  164. return !first_child_of_type<Element>();
  165. default:
  166. return false;
  167. }
  168. }
  169. Element* Document::document_element()
  170. {
  171. return first_child_of_type<Element>();
  172. }
  173. const Element* Document::document_element() const
  174. {
  175. return first_child_of_type<Element>();
  176. }
  177. const HTML::HTMLHtmlElement* Document::html_element() const
  178. {
  179. auto* html = document_element();
  180. if (is<HTML::HTMLHtmlElement>(html))
  181. return downcast<HTML::HTMLHtmlElement>(html);
  182. return nullptr;
  183. }
  184. const HTML::HTMLHeadElement* Document::head() const
  185. {
  186. auto* html = html_element();
  187. if (!html)
  188. return nullptr;
  189. return html->first_child_of_type<HTML::HTMLHeadElement>();
  190. }
  191. const HTML::HTMLElement* Document::body() const
  192. {
  193. auto* html = html_element();
  194. if (!html)
  195. return nullptr;
  196. auto* first_body = html->first_child_of_type<HTML::HTMLBodyElement>();
  197. if (first_body)
  198. return first_body;
  199. auto* first_frameset = html->first_child_of_type<HTML::HTMLFrameSetElement>();
  200. if (first_frameset)
  201. return first_frameset;
  202. return nullptr;
  203. }
  204. // https://html.spec.whatwg.org/multipage/dom.html#dom-document-body
  205. ExceptionOr<void> Document::set_body(HTML::HTMLElement& new_body)
  206. {
  207. if (!is<HTML::HTMLBodyElement>(new_body) && !is<HTML::HTMLFrameSetElement>(new_body))
  208. return DOM::HierarchyRequestError::create("Invalid document body element, must be 'body' or 'frameset'");
  209. auto* existing_body = body();
  210. if (existing_body) {
  211. TODO();
  212. return {};
  213. }
  214. auto* document_element = this->document_element();
  215. if (!document_element)
  216. return DOM::HierarchyRequestError::create("Missing document element");
  217. document_element->append_child(new_body);
  218. return {};
  219. }
  220. String Document::title() const
  221. {
  222. auto* head_element = head();
  223. if (!head_element)
  224. return {};
  225. auto* title_element = head_element->first_child_of_type<HTML::HTMLTitleElement>();
  226. if (!title_element)
  227. return {};
  228. auto raw_title = title_element->text_content();
  229. StringBuilder builder;
  230. bool last_was_space = false;
  231. for (auto code_point : Utf8View(raw_title)) {
  232. if (isspace(code_point)) {
  233. last_was_space = true;
  234. } else {
  235. if (last_was_space && !builder.is_empty())
  236. builder.append(' ');
  237. builder.append_code_point(code_point);
  238. last_was_space = false;
  239. }
  240. }
  241. return builder.to_string();
  242. }
  243. void Document::set_title(const String& title)
  244. {
  245. auto* head_element = const_cast<HTML::HTMLHeadElement*>(head());
  246. if (!head_element)
  247. return;
  248. RefPtr<HTML::HTMLTitleElement> title_element = head_element->first_child_of_type<HTML::HTMLTitleElement>();
  249. if (!title_element) {
  250. title_element = static_ptr_cast<HTML::HTMLTitleElement>(create_element(HTML::TagNames::title));
  251. head_element->append_child(*title_element);
  252. }
  253. title_element->remove_all_children(true);
  254. title_element->append_child(adopt(*new Text(*this, title)));
  255. if (auto* page = this->page()) {
  256. if (frame() == &page->main_frame())
  257. page->client().page_did_change_title(title);
  258. }
  259. }
  260. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  261. {
  262. m_frame = frame;
  263. update_layout();
  264. }
  265. void Document::detach_from_frame(Badge<Frame>, Frame& frame)
  266. {
  267. VERIFY(&frame == m_frame);
  268. tear_down_layout_tree();
  269. m_frame = nullptr;
  270. }
  271. void Document::tear_down_layout_tree()
  272. {
  273. if (!m_layout_root)
  274. return;
  275. // Gather up all the layout nodes in a vector and detach them from parents
  276. // while the vector keeps them alive.
  277. NonnullRefPtrVector<Layout::Node> layout_nodes;
  278. m_layout_root->for_each_in_inclusive_subtree([&](auto& layout_node) {
  279. layout_nodes.append(layout_node);
  280. return IterationDecision::Continue;
  281. });
  282. for (auto& layout_node : layout_nodes) {
  283. if (layout_node.parent())
  284. layout_node.parent()->remove_child(layout_node);
  285. }
  286. m_layout_root = nullptr;
  287. }
  288. Color Document::background_color(const Palette& palette) const
  289. {
  290. auto default_color = palette.base();
  291. auto* body_element = body();
  292. if (!body_element)
  293. return default_color;
  294. auto* body_layout_node = body_element->layout_node();
  295. if (!body_layout_node)
  296. return default_color;
  297. auto color = body_layout_node->computed_values().background_color();
  298. if (!color.alpha())
  299. return default_color;
  300. return color;
  301. }
  302. RefPtr<Gfx::Bitmap> Document::background_image() const
  303. {
  304. auto* body_element = body();
  305. if (!body_element)
  306. return {};
  307. auto* body_layout_node = body_element->layout_node();
  308. if (!body_layout_node)
  309. return {};
  310. auto background_image = body_layout_node->background_image();
  311. if (!background_image)
  312. return {};
  313. return background_image->bitmap();
  314. }
  315. CSS::Repeat Document::background_repeat_x() const
  316. {
  317. auto* body_element = body();
  318. if (!body_element)
  319. return CSS::Repeat::Repeat;
  320. auto* body_layout_node = body_element->layout_node();
  321. if (!body_layout_node)
  322. return CSS::Repeat::Repeat;
  323. return body_layout_node->computed_values().background_repeat_x();
  324. }
  325. CSS::Repeat Document::background_repeat_y() const
  326. {
  327. auto* body_element = body();
  328. if (!body_element)
  329. return CSS::Repeat::Repeat;
  330. auto* body_layout_node = body_element->layout_node();
  331. if (!body_layout_node)
  332. return CSS::Repeat::Repeat;
  333. return body_layout_node->computed_values().background_repeat_y();
  334. }
  335. URL Document::complete_url(const String& string) const
  336. {
  337. return m_url.complete_url(string);
  338. }
  339. void Document::invalidate_layout()
  340. {
  341. tear_down_layout_tree();
  342. }
  343. void Document::force_layout()
  344. {
  345. invalidate_layout();
  346. update_layout();
  347. }
  348. void Document::update_layout()
  349. {
  350. if (!frame())
  351. return;
  352. if (!m_layout_root) {
  353. Layout::TreeBuilder tree_builder;
  354. m_layout_root = static_ptr_cast<Layout::InitialContainingBlockBox>(tree_builder.build(*this));
  355. }
  356. Layout::BlockFormattingContext root_formatting_context(*m_layout_root, nullptr);
  357. root_formatting_context.run(*m_layout_root, Layout::LayoutMode::Default);
  358. m_layout_root->set_needs_display();
  359. if (frame()->is_main_frame()) {
  360. if (auto* page = this->page())
  361. page->client().page_did_layout();
  362. }
  363. }
  364. static void update_style_recursively(DOM::Node& node)
  365. {
  366. node.for_each_child([&](auto& child) {
  367. if (child.needs_style_update()) {
  368. if (is<Element>(child))
  369. downcast<Element>(child).recompute_style();
  370. child.set_needs_style_update(false);
  371. }
  372. if (child.child_needs_style_update()) {
  373. update_style_recursively(child);
  374. child.set_child_needs_style_update(false);
  375. }
  376. return IterationDecision::Continue;
  377. });
  378. }
  379. void Document::update_style()
  380. {
  381. update_style_recursively(*this);
  382. update_layout();
  383. }
  384. RefPtr<Layout::Node> Document::create_layout_node()
  385. {
  386. return adopt(*new Layout::InitialContainingBlockBox(*this, CSS::StyleProperties::create()));
  387. }
  388. void Document::set_link_color(Color color)
  389. {
  390. m_link_color = color;
  391. }
  392. void Document::set_active_link_color(Color color)
  393. {
  394. m_active_link_color = color;
  395. }
  396. void Document::set_visited_link_color(Color color)
  397. {
  398. m_visited_link_color = color;
  399. }
  400. const Layout::InitialContainingBlockBox* Document::layout_node() const
  401. {
  402. return static_cast<const Layout::InitialContainingBlockBox*>(Node::layout_node());
  403. }
  404. Layout::InitialContainingBlockBox* Document::layout_node()
  405. {
  406. return static_cast<Layout::InitialContainingBlockBox*>(Node::layout_node());
  407. }
  408. void Document::set_inspected_node(Node* node)
  409. {
  410. if (m_inspected_node == node)
  411. return;
  412. if (m_inspected_node && m_inspected_node->layout_node())
  413. m_inspected_node->layout_node()->set_needs_display();
  414. m_inspected_node = node;
  415. if (m_inspected_node && m_inspected_node->layout_node())
  416. m_inspected_node->layout_node()->set_needs_display();
  417. }
  418. void Document::set_hovered_node(Node* node)
  419. {
  420. if (m_hovered_node == node)
  421. return;
  422. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  423. m_hovered_node = node;
  424. invalidate_style();
  425. }
  426. NonnullRefPtrVector<Element> Document::get_elements_by_name(const String& name) const
  427. {
  428. NonnullRefPtrVector<Element> elements;
  429. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  430. if (element.attribute(HTML::AttributeNames::name) == name)
  431. elements.append(element);
  432. return IterationDecision::Continue;
  433. });
  434. return elements;
  435. }
  436. NonnullRefPtrVector<Element> Document::get_elements_by_tag_name(const FlyString& tag_name) const
  437. {
  438. // FIXME: Support "*" for tag_name
  439. // https://dom.spec.whatwg.org/#concept-getelementsbytagname
  440. NonnullRefPtrVector<Element> elements;
  441. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  442. if (element.namespace_() == Namespace::HTML
  443. ? element.local_name().to_lowercase() == tag_name.to_lowercase()
  444. : element.local_name() == tag_name) {
  445. elements.append(element);
  446. }
  447. return IterationDecision::Continue;
  448. });
  449. return elements;
  450. }
  451. NonnullRefPtrVector<Element> Document::get_elements_by_class_name(const FlyString& class_name) const
  452. {
  453. NonnullRefPtrVector<Element> elements;
  454. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  455. if (element.has_class(class_name, in_quirks_mode() ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
  456. elements.append(element);
  457. return IterationDecision::Continue;
  458. });
  459. return elements;
  460. }
  461. Color Document::link_color() const
  462. {
  463. if (m_link_color.has_value())
  464. return m_link_color.value();
  465. if (!page())
  466. return Color::Blue;
  467. return page()->palette().link();
  468. }
  469. Color Document::active_link_color() const
  470. {
  471. if (m_active_link_color.has_value())
  472. return m_active_link_color.value();
  473. if (!page())
  474. return Color::Red;
  475. return page()->palette().active_link();
  476. }
  477. Color Document::visited_link_color() const
  478. {
  479. if (m_visited_link_color.has_value())
  480. return m_visited_link_color.value();
  481. if (!page())
  482. return Color::Magenta;
  483. return page()->palette().visited_link();
  484. }
  485. JS::Interpreter& Document::interpreter()
  486. {
  487. if (!m_interpreter) {
  488. auto& vm = Bindings::main_thread_vm();
  489. // TODO: Hook up vm.on_promise_unhandled_rejection and vm.on_promise_rejection_handled
  490. // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#promise_rejection_events
  491. m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(vm, *m_window);
  492. }
  493. return *m_interpreter;
  494. }
  495. JS::Value Document::run_javascript(const StringView& source, const StringView& filename)
  496. {
  497. auto parser = JS::Parser(JS::Lexer(source, filename));
  498. auto program = parser.parse_program();
  499. if (parser.has_errors()) {
  500. parser.print_errors();
  501. return JS::js_undefined();
  502. }
  503. auto& interpreter = document().interpreter();
  504. auto& vm = interpreter.vm();
  505. interpreter.run(interpreter.global_object(), *program);
  506. if (vm.exception())
  507. vm.clear_exception();
  508. return vm.last_value();
  509. }
  510. // https://dom.spec.whatwg.org/#dom-document-createelement
  511. // FIXME: This only implements step 6 of the algorithm and does not take in options.
  512. NonnullRefPtr<Element> Document::create_element(const String& tag_name)
  513. {
  514. // FIXME: Let namespace be the HTML namespace, if this is an HTML document or this’s content type is "application/xhtml+xml", and null otherwise.
  515. return DOM::create_element(*this, tag_name, Namespace::HTML);
  516. }
  517. // https://dom.spec.whatwg.org/#internal-createelementns-steps
  518. // FIXME: This only implements step 4 of the algorithm and does not take in options.
  519. NonnullRefPtr<Element> Document::create_element_ns(const String& namespace_, const String& qualified_name)
  520. {
  521. return DOM::create_element(*this, qualified_name, namespace_);
  522. }
  523. NonnullRefPtr<DocumentFragment> Document::create_document_fragment()
  524. {
  525. return adopt(*new DocumentFragment(*this));
  526. }
  527. NonnullRefPtr<Text> Document::create_text_node(const String& data)
  528. {
  529. return adopt(*new Text(*this, data));
  530. }
  531. NonnullRefPtr<Comment> Document::create_comment(const String& data)
  532. {
  533. return adopt(*new Comment(*this, data));
  534. }
  535. NonnullRefPtr<Range> Document::create_range()
  536. {
  537. return Range::create(*this);
  538. }
  539. // https://dom.spec.whatwg.org/#dom-document-createevent
  540. NonnullRefPtr<Event> Document::create_event(const String& interface)
  541. {
  542. auto interface_lowercase = interface.to_lowercase();
  543. RefPtr<Event> event;
  544. if (interface_lowercase == "beforeunloadevent") {
  545. event = Event::create(""); // FIXME: Create BeforeUnloadEvent
  546. } else if (interface_lowercase == "compositionevent") {
  547. event = Event::create(""); // FIXME: Create CompositionEvent
  548. } else if (interface_lowercase == "customevent") {
  549. event = Event::create(""); // FIXME: Create CustomEvent
  550. } else if (interface_lowercase == "devicemotionevent") {
  551. event = Event::create(""); // FIXME: Create DeviceMotionEvent
  552. } else if (interface_lowercase == "deviceorientationevent") {
  553. event = Event::create(""); // FIXME: Create DeviceOrientationEvent
  554. } else if (interface_lowercase == "dragevent") {
  555. event = Event::create(""); // FIXME: Create DragEvent
  556. } else if (interface_lowercase.is_one_of("event", "events")) {
  557. event = Event::create("");
  558. } else if (interface_lowercase == "focusevent") {
  559. event = Event::create(""); // FIXME: Create FocusEvent
  560. } else if (interface_lowercase == "hashchangeevent") {
  561. event = Event::create(""); // FIXME: Create HashChangeEvent
  562. } else if (interface_lowercase == "htmlevents") {
  563. event = Event::create("");
  564. } else if (interface_lowercase == "keyboardevent") {
  565. event = Event::create(""); // FIXME: Create KeyboardEvent
  566. } else if (interface_lowercase == "messageevent") {
  567. event = Event::create(""); // FIXME: Create MessageEvent
  568. } else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) {
  569. event = UIEvents::MouseEvent::create("", 0, 0);
  570. } else if (interface_lowercase == "storageevent") {
  571. event = Event::create(""); // FIXME: Create StorageEvent
  572. } else if (interface_lowercase == "svgevents") {
  573. event = Event::create("");
  574. } else if (interface_lowercase == "textevent") {
  575. event = Event::create(""); // FIXME: Create CompositionEvent
  576. } else if (interface_lowercase == "touchevent") {
  577. event = Event::create(""); // FIXME: Create TouchEvent
  578. } else if (interface_lowercase.is_one_of("uievent", "uievents")) {
  579. event = UIEvents::UIEvent::create("");
  580. } else {
  581. // FIXME:
  582. // 3. If constructor is null, then throw a "NotSupportedError" DOMException.
  583. // 4. If the interface indicated by constructor is not exposed on the relevant global object of this, then throw a "NotSupportedError" DOMException.
  584. TODO();
  585. }
  586. // Setting type to empty string is handled by each constructor.
  587. // FIXME:
  588. // 7. Initialize event’s timeStamp attribute to a DOMHighResTimeStamp representing the high resolution time from the time origin to now.
  589. event->set_is_trusted(false);
  590. event->set_initialized(false);
  591. return event.release_nonnull();
  592. }
  593. void Document::set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement* script)
  594. {
  595. m_pending_parsing_blocking_script = script;
  596. }
  597. NonnullRefPtr<HTML::HTMLScriptElement> Document::take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>)
  598. {
  599. return m_pending_parsing_blocking_script.release_nonnull();
  600. }
  601. void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
  602. {
  603. m_scripts_to_execute_when_parsing_has_finished.append(script);
  604. }
  605. NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>)
  606. {
  607. return move(m_scripts_to_execute_when_parsing_has_finished);
  608. }
  609. void Document::add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
  610. {
  611. m_scripts_to_execute_as_soon_as_possible.append(script);
  612. }
  613. NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>)
  614. {
  615. return move(m_scripts_to_execute_as_soon_as_possible);
  616. }
  617. // https://dom.spec.whatwg.org/#concept-node-adopt
  618. void Document::adopt_node(Node& node)
  619. {
  620. auto& old_document = node.document();
  621. if (node.parent())
  622. node.remove();
  623. if (&old_document != this) {
  624. // FIXME: This should be shadow-including.
  625. node.for_each_in_inclusive_subtree([&](auto& inclusive_descendant) {
  626. inclusive_descendant.set_document({}, *this);
  627. // FIXME: If inclusiveDescendant is an element, then set the node document of each attribute in inclusiveDescendant’s attribute list to document.
  628. return IterationDecision::Continue;
  629. });
  630. // FIXME: For each inclusiveDescendant in node’s shadow-including inclusive descendants that is custom,
  631. // enqueue a custom element callback reaction with inclusiveDescendant, callback name "adoptedCallback",
  632. // and an argument list containing oldDocument and document.
  633. // FIXME: This should be shadow-including.
  634. node.for_each_in_inclusive_subtree([&](auto& inclusive_descendant) {
  635. inclusive_descendant.adopted_from(old_document);
  636. return IterationDecision::Continue;
  637. });
  638. }
  639. }
  640. // https://dom.spec.whatwg.org/#dom-document-adoptnode
  641. NonnullRefPtr<Node> Document::adopt_node_binding(NonnullRefPtr<Node> node)
  642. {
  643. if (is<Document>(*node)) {
  644. dbgln("Document::adopt_node_binding: Cannot adopt a document into a document (FIXME: throw as NotSupportedError exception, see issue #6075");
  645. return node;
  646. }
  647. if (is<ShadowRoot>(*node)) {
  648. dbgln("Document::adopt_node_binding: Cannot adopt a shadow root into a document (FIXME: throw as HierarchyRequestError exception, see issue #6075");
  649. return node;
  650. }
  651. if (is<DocumentFragment>(*node) && downcast<DocumentFragment>(*node).host())
  652. return node;
  653. adopt_node(*node);
  654. return node;
  655. }
  656. const DocumentType* Document::doctype() const
  657. {
  658. return first_child_of_type<DocumentType>();
  659. }
  660. const String& Document::compat_mode() const
  661. {
  662. static String back_compat = "BackCompat";
  663. static String css1_compat = "CSS1Compat";
  664. if (m_quirks_mode == QuirksMode::Yes)
  665. return back_compat;
  666. return css1_compat;
  667. }
  668. bool Document::is_editable() const
  669. {
  670. return m_editable;
  671. }
  672. void Document::set_focused_element(Element* element)
  673. {
  674. if (m_focused_element == element)
  675. return;
  676. m_focused_element = element;
  677. if (m_layout_root)
  678. m_layout_root->set_needs_display();
  679. }
  680. void Document::set_ready_state(const String& ready_state)
  681. {
  682. m_ready_state = ready_state;
  683. dispatch_event(Event::create(HTML::EventNames::readystatechange));
  684. }
  685. Page* Document::page()
  686. {
  687. return m_frame ? m_frame->page() : nullptr;
  688. }
  689. const Page* Document::page() const
  690. {
  691. return m_frame ? m_frame->page() : nullptr;
  692. }
  693. EventTarget* Document::get_parent(const Event& event)
  694. {
  695. if (event.type() == HTML::EventNames::load)
  696. return nullptr;
  697. return &window();
  698. }
  699. void Document::completely_finish_loading()
  700. {
  701. // FIXME: This needs to handle iframes.
  702. dispatch_event(DOM::Event::create(HTML::EventNames::load));
  703. }
  704. String Document::cookie()
  705. {
  706. if (auto* page = this->page())
  707. return page->client().page_did_request_cookie(m_url);
  708. return {};
  709. }
  710. void Document::set_cookie(String cookie)
  711. {
  712. if (auto* page = this->page())
  713. page->client().page_did_set_cookie(m_url, cookie);
  714. }
  715. }