Document.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (c) 2018-2021, 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 <AK/Utf8View.h>
  28. #include <LibCore/Timer.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Parser.h>
  31. #include <LibJS/Runtime/Function.h>
  32. #include <LibWeb/Bindings/MainThreadVM.h>
  33. #include <LibWeb/Bindings/WindowObject.h>
  34. #include <LibWeb/CSS/StyleResolver.h>
  35. #include <LibWeb/DOM/Comment.h>
  36. #include <LibWeb/DOM/DOMException.h>
  37. #include <LibWeb/DOM/Document.h>
  38. #include <LibWeb/DOM/DocumentFragment.h>
  39. #include <LibWeb/DOM/DocumentType.h>
  40. #include <LibWeb/DOM/Element.h>
  41. #include <LibWeb/DOM/ElementFactory.h>
  42. #include <LibWeb/DOM/Event.h>
  43. #include <LibWeb/DOM/ExceptionOr.h>
  44. #include <LibWeb/DOM/Range.h>
  45. #include <LibWeb/DOM/Text.h>
  46. #include <LibWeb/DOM/Window.h>
  47. #include <LibWeb/Dump.h>
  48. #include <LibWeb/HTML/AttributeNames.h>
  49. #include <LibWeb/HTML/EventNames.h>
  50. #include <LibWeb/HTML/HTMLBodyElement.h>
  51. #include <LibWeb/HTML/HTMLFrameSetElement.h>
  52. #include <LibWeb/HTML/HTMLHeadElement.h>
  53. #include <LibWeb/HTML/HTMLHtmlElement.h>
  54. #include <LibWeb/HTML/HTMLScriptElement.h>
  55. #include <LibWeb/HTML/HTMLTitleElement.h>
  56. #include <LibWeb/InProcessWebView.h>
  57. #include <LibWeb/Layout/BlockFormattingContext.h>
  58. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  59. #include <LibWeb/Layout/TreeBuilder.h>
  60. #include <LibWeb/Namespace.h>
  61. #include <LibWeb/Origin.h>
  62. #include <LibWeb/Page/Frame.h>
  63. #include <LibWeb/SVG/TagNames.h>
  64. #include <ctype.h>
  65. namespace Web::DOM {
  66. Document::Document(const URL& url)
  67. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  68. , m_style_resolver(make<CSS::StyleResolver>(*this))
  69. , m_style_sheets(CSS::StyleSheetList::create(*this))
  70. , m_url(url)
  71. , m_window(Window::create_with_document(*this))
  72. , m_implementation(DOMImplementation::create(*this))
  73. {
  74. m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
  75. update_style();
  76. });
  77. m_forced_layout_timer = Core::Timer::create_single_shot(0, [this] {
  78. force_layout();
  79. });
  80. }
  81. Document::~Document()
  82. {
  83. }
  84. void Document::removed_last_ref()
  85. {
  86. VERIFY(!ref_count());
  87. VERIFY(!m_deletion_has_begun);
  88. if (m_referencing_node_count) {
  89. // The document has reached ref_count==0 but still has nodes keeping it alive.
  90. // At this point, sever all the node links we control.
  91. // If nodes remain elsewhere (e.g JS wrappers), they will keep the document alive.
  92. // NOTE: This makes sure we stay alive across for the duration of the cleanup below.
  93. increment_referencing_node_count();
  94. m_focused_element = nullptr;
  95. m_hovered_node = nullptr;
  96. m_pending_parsing_blocking_script = nullptr;
  97. m_inspected_node = nullptr;
  98. m_scripts_to_execute_when_parsing_has_finished.clear();
  99. m_scripts_to_execute_as_soon_as_possible.clear();
  100. m_associated_inert_template_document = nullptr;
  101. m_interpreter = nullptr;
  102. {
  103. // Gather up all the descendants of this document and prune them from the tree.
  104. // FIXME: This could definitely be more elegant.
  105. NonnullRefPtrVector<Node> descendants;
  106. for_each_in_subtree([&](auto& node) {
  107. if (&node != this)
  108. descendants.append(node);
  109. return IterationDecision::Continue;
  110. });
  111. for (auto& node : descendants) {
  112. VERIFY(&node.document() == this);
  113. VERIFY(!node.is_document());
  114. if (node.parent())
  115. node.parent()->remove_child(node);
  116. }
  117. }
  118. m_in_removed_last_ref = false;
  119. decrement_referencing_node_count();
  120. return;
  121. }
  122. m_in_removed_last_ref = false;
  123. m_deletion_has_begun = true;
  124. delete this;
  125. }
  126. Origin Document::origin() const
  127. {
  128. if (!m_url.is_valid())
  129. return {};
  130. return { m_url.protocol(), m_url.host(), m_url.port() };
  131. }
  132. void Document::set_origin(const Origin& origin)
  133. {
  134. m_url.set_protocol(origin.protocol());
  135. m_url.set_host(origin.host());
  136. m_url.set_port(origin.port());
  137. }
  138. void Document::schedule_style_update()
  139. {
  140. if (m_style_update_timer->is_active())
  141. return;
  142. m_style_update_timer->start();
  143. }
  144. void Document::schedule_forced_layout()
  145. {
  146. if (m_forced_layout_timer->is_active())
  147. return;
  148. m_forced_layout_timer->start();
  149. }
  150. bool Document::is_child_allowed(const Node& node) const
  151. {
  152. switch (node.type()) {
  153. case NodeType::DOCUMENT_NODE:
  154. case NodeType::TEXT_NODE:
  155. return false;
  156. case NodeType::COMMENT_NODE:
  157. return true;
  158. case NodeType::DOCUMENT_TYPE_NODE:
  159. return !first_child_of_type<DocumentType>();
  160. case NodeType::ELEMENT_NODE:
  161. return !first_child_of_type<Element>();
  162. default:
  163. return false;
  164. }
  165. }
  166. Element* Document::document_element()
  167. {
  168. return first_child_of_type<Element>();
  169. }
  170. const Element* Document::document_element() const
  171. {
  172. return first_child_of_type<Element>();
  173. }
  174. const HTML::HTMLHtmlElement* Document::html_element() const
  175. {
  176. auto* html = document_element();
  177. if (is<HTML::HTMLHtmlElement>(html))
  178. return downcast<HTML::HTMLHtmlElement>(html);
  179. return nullptr;
  180. }
  181. const HTML::HTMLHeadElement* Document::head() const
  182. {
  183. auto* html = html_element();
  184. if (!html)
  185. return nullptr;
  186. return html->first_child_of_type<HTML::HTMLHeadElement>();
  187. }
  188. const HTML::HTMLElement* Document::body() const
  189. {
  190. auto* html = html_element();
  191. if (!html)
  192. return nullptr;
  193. auto* first_body = html->first_child_of_type<HTML::HTMLBodyElement>();
  194. if (first_body)
  195. return first_body;
  196. auto* first_frameset = html->first_child_of_type<HTML::HTMLFrameSetElement>();
  197. if (first_frameset)
  198. return first_frameset;
  199. return nullptr;
  200. }
  201. // https://html.spec.whatwg.org/multipage/dom.html#dom-document-body
  202. ExceptionOr<void> Document::set_body(HTML::HTMLElement& new_body)
  203. {
  204. if (!is<HTML::HTMLBodyElement>(new_body) && !is<HTML::HTMLFrameSetElement>(new_body))
  205. return DOM::HierarchyRequestError::create("Invalid document body element, must be 'body' or 'frameset'");
  206. auto* existing_body = body();
  207. if (existing_body) {
  208. TODO();
  209. return {};
  210. }
  211. auto* document_element = this->document_element();
  212. if (!document_element)
  213. return DOM::HierarchyRequestError::create("Missing document element");
  214. document_element->append_child(new_body);
  215. return {};
  216. }
  217. String Document::title() const
  218. {
  219. auto* head_element = head();
  220. if (!head_element)
  221. return {};
  222. auto* title_element = head_element->first_child_of_type<HTML::HTMLTitleElement>();
  223. if (!title_element)
  224. return {};
  225. auto raw_title = title_element->text_content();
  226. StringBuilder builder;
  227. bool last_was_space = false;
  228. for (auto code_point : Utf8View(raw_title)) {
  229. if (isspace(code_point)) {
  230. last_was_space = true;
  231. } else {
  232. if (last_was_space && !builder.is_empty())
  233. builder.append(' ');
  234. builder.append_code_point(code_point);
  235. last_was_space = false;
  236. }
  237. }
  238. return builder.to_string();
  239. }
  240. void Document::set_title(const String& title)
  241. {
  242. auto* head_element = const_cast<HTML::HTMLHeadElement*>(head());
  243. if (!head_element)
  244. return;
  245. RefPtr<HTML::HTMLTitleElement> title_element = head_element->first_child_of_type<HTML::HTMLTitleElement>();
  246. if (!title_element) {
  247. title_element = static_ptr_cast<HTML::HTMLTitleElement>(create_element(HTML::TagNames::title));
  248. head_element->append_child(*title_element);
  249. }
  250. while (RefPtr<Node> child = title_element->first_child())
  251. title_element->remove_child(child.release_nonnull());
  252. title_element->append_child(adopt(*new Text(*this, title)));
  253. if (auto* page = this->page()) {
  254. if (frame() == &page->main_frame())
  255. page->client().page_did_change_title(title);
  256. }
  257. }
  258. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  259. {
  260. m_frame = frame;
  261. update_layout();
  262. }
  263. void Document::detach_from_frame(Badge<Frame>, Frame& frame)
  264. {
  265. VERIFY(&frame == m_frame);
  266. tear_down_layout_tree();
  267. m_frame = nullptr;
  268. }
  269. void Document::tear_down_layout_tree()
  270. {
  271. if (!m_layout_root)
  272. return;
  273. // Gather up all the layout nodes in a vector and detach them from parents
  274. // while the vector keeps them alive.
  275. NonnullRefPtrVector<Layout::Node> layout_nodes;
  276. m_layout_root->for_each_in_subtree([&](auto& layout_node) {
  277. layout_nodes.append(layout_node);
  278. return IterationDecision::Continue;
  279. });
  280. for (auto& layout_node : layout_nodes) {
  281. if (layout_node.parent())
  282. layout_node.parent()->remove_child(layout_node);
  283. }
  284. m_layout_root = nullptr;
  285. }
  286. Color Document::background_color(const Palette& palette) const
  287. {
  288. auto default_color = palette.base();
  289. auto* body_element = body();
  290. if (!body_element)
  291. return default_color;
  292. auto* body_layout_node = body_element->layout_node();
  293. if (!body_layout_node)
  294. return default_color;
  295. auto color = body_layout_node->computed_values().background_color();
  296. if (!color.alpha())
  297. return default_color;
  298. return color;
  299. }
  300. RefPtr<Gfx::Bitmap> Document::background_image() const
  301. {
  302. auto* body_element = body();
  303. if (!body_element)
  304. return {};
  305. auto* body_layout_node = body_element->layout_node();
  306. if (!body_layout_node)
  307. return {};
  308. auto background_image = body_layout_node->background_image();
  309. if (!background_image)
  310. return {};
  311. return background_image->bitmap();
  312. }
  313. CSS::Repeat Document::background_repeat_x() const
  314. {
  315. auto* body_element = body();
  316. if (!body_element)
  317. return CSS::Repeat::Repeat;
  318. auto* body_layout_node = body_element->layout_node();
  319. if (!body_layout_node)
  320. return CSS::Repeat::Repeat;
  321. return body_layout_node->computed_values().background_repeat_x();
  322. }
  323. CSS::Repeat Document::background_repeat_y() const
  324. {
  325. auto* body_element = body();
  326. if (!body_element)
  327. return CSS::Repeat::Repeat;
  328. auto* body_layout_node = body_element->layout_node();
  329. if (!body_layout_node)
  330. return CSS::Repeat::Repeat;
  331. return body_layout_node->computed_values().background_repeat_y();
  332. }
  333. URL Document::complete_url(const String& string) const
  334. {
  335. return m_url.complete_url(string);
  336. }
  337. void Document::invalidate_layout()
  338. {
  339. tear_down_layout_tree();
  340. }
  341. void Document::force_layout()
  342. {
  343. invalidate_layout();
  344. update_layout();
  345. }
  346. void Document::update_layout()
  347. {
  348. if (!frame())
  349. return;
  350. if (!m_layout_root) {
  351. Layout::TreeBuilder tree_builder;
  352. m_layout_root = static_ptr_cast<Layout::InitialContainingBlockBox>(tree_builder.build(*this));
  353. }
  354. Layout::BlockFormattingContext root_formatting_context(*m_layout_root, nullptr);
  355. root_formatting_context.run(*m_layout_root, Layout::LayoutMode::Default);
  356. m_layout_root->set_needs_display();
  357. if (frame()->is_main_frame()) {
  358. if (auto* page = this->page())
  359. page->client().page_did_layout();
  360. }
  361. }
  362. static void update_style_recursively(DOM::Node& node)
  363. {
  364. node.for_each_child([&](auto& child) {
  365. if (child.needs_style_update()) {
  366. if (is<Element>(child))
  367. downcast<Element>(child).recompute_style();
  368. child.set_needs_style_update(false);
  369. }
  370. if (child.child_needs_style_update()) {
  371. update_style_recursively(child);
  372. child.set_child_needs_style_update(false);
  373. }
  374. return IterationDecision::Continue;
  375. });
  376. }
  377. void Document::update_style()
  378. {
  379. update_style_recursively(*this);
  380. update_layout();
  381. }
  382. RefPtr<Layout::Node> Document::create_layout_node()
  383. {
  384. return adopt(*new Layout::InitialContainingBlockBox(*this, CSS::StyleProperties::create()));
  385. }
  386. void Document::set_link_color(Color color)
  387. {
  388. m_link_color = color;
  389. }
  390. void Document::set_active_link_color(Color color)
  391. {
  392. m_active_link_color = color;
  393. }
  394. void Document::set_visited_link_color(Color color)
  395. {
  396. m_visited_link_color = color;
  397. }
  398. const Layout::InitialContainingBlockBox* Document::layout_node() const
  399. {
  400. return static_cast<const Layout::InitialContainingBlockBox*>(Node::layout_node());
  401. }
  402. Layout::InitialContainingBlockBox* Document::layout_node()
  403. {
  404. return static_cast<Layout::InitialContainingBlockBox*>(Node::layout_node());
  405. }
  406. void Document::set_inspected_node(Node* node)
  407. {
  408. if (m_inspected_node == node)
  409. return;
  410. if (m_inspected_node && m_inspected_node->layout_node())
  411. m_inspected_node->layout_node()->set_needs_display();
  412. m_inspected_node = node;
  413. if (m_inspected_node && m_inspected_node->layout_node())
  414. m_inspected_node->layout_node()->set_needs_display();
  415. }
  416. void Document::set_hovered_node(Node* node)
  417. {
  418. if (m_hovered_node == node)
  419. return;
  420. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  421. m_hovered_node = node;
  422. invalidate_style();
  423. }
  424. NonnullRefPtrVector<Element> Document::get_elements_by_name(const String& name) const
  425. {
  426. NonnullRefPtrVector<Element> elements;
  427. for_each_in_subtree_of_type<Element>([&](auto& element) {
  428. if (element.attribute(HTML::AttributeNames::name) == name)
  429. elements.append(element);
  430. return IterationDecision::Continue;
  431. });
  432. return elements;
  433. }
  434. NonnullRefPtrVector<Element> Document::get_elements_by_tag_name(const FlyString& tag_name) const
  435. {
  436. // FIXME: Support "*" for tag_name
  437. // https://dom.spec.whatwg.org/#concept-getelementsbytagname
  438. NonnullRefPtrVector<Element> elements;
  439. for_each_in_subtree_of_type<Element>([&](auto& element) {
  440. if (element.namespace_() == Namespace::HTML
  441. ? element.local_name().to_lowercase() == tag_name.to_lowercase()
  442. : element.local_name() == tag_name) {
  443. elements.append(element);
  444. }
  445. return IterationDecision::Continue;
  446. });
  447. return elements;
  448. }
  449. NonnullRefPtrVector<Element> Document::get_elements_by_class_name(const FlyString& class_name) const
  450. {
  451. NonnullRefPtrVector<Element> elements;
  452. for_each_in_subtree_of_type<Element>([&](auto& element) {
  453. if (element.has_class(class_name, in_quirks_mode() ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
  454. elements.append(element);
  455. return IterationDecision::Continue;
  456. });
  457. return elements;
  458. }
  459. Color Document::link_color() const
  460. {
  461. if (m_link_color.has_value())
  462. return m_link_color.value();
  463. if (!page())
  464. return Color::Blue;
  465. return page()->palette().link();
  466. }
  467. Color Document::active_link_color() const
  468. {
  469. if (m_active_link_color.has_value())
  470. return m_active_link_color.value();
  471. if (!page())
  472. return Color::Red;
  473. return page()->palette().active_link();
  474. }
  475. Color Document::visited_link_color() const
  476. {
  477. if (m_visited_link_color.has_value())
  478. return m_visited_link_color.value();
  479. if (!page())
  480. return Color::Magenta;
  481. return page()->palette().visited_link();
  482. }
  483. JS::Interpreter& Document::interpreter()
  484. {
  485. if (!m_interpreter) {
  486. auto& vm = Bindings::main_thread_vm();
  487. // TODO: Hook up vm.on_promise_unhandled_rejection and vm.on_promise_rejection_handled
  488. // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#promise_rejection_events
  489. m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(vm, *m_window);
  490. }
  491. return *m_interpreter;
  492. }
  493. JS::Value Document::run_javascript(const StringView& source, const StringView& filename)
  494. {
  495. auto parser = JS::Parser(JS::Lexer(source, filename));
  496. auto program = parser.parse_program();
  497. if (parser.has_errors()) {
  498. parser.print_errors();
  499. return JS::js_undefined();
  500. }
  501. auto& interpreter = document().interpreter();
  502. auto& vm = interpreter.vm();
  503. interpreter.run(interpreter.global_object(), *program);
  504. if (vm.exception())
  505. vm.clear_exception();
  506. return vm.last_value();
  507. }
  508. // https://dom.spec.whatwg.org/#dom-document-createelement
  509. // FIXME: This only implements step 6 of the algorithm and does not take in options.
  510. NonnullRefPtr<Element> Document::create_element(const String& tag_name)
  511. {
  512. // 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.
  513. return DOM::create_element(*this, tag_name, Namespace::HTML);
  514. }
  515. // https://dom.spec.whatwg.org/#internal-createelementns-steps
  516. // FIXME: This only implements step 4 of the algorithm and does not take in options.
  517. NonnullRefPtr<Element> Document::create_element_ns(const String& namespace_, const String& qualified_name)
  518. {
  519. return DOM::create_element(*this, qualified_name, namespace_);
  520. }
  521. NonnullRefPtr<DocumentFragment> Document::create_document_fragment()
  522. {
  523. return adopt(*new DocumentFragment(*this));
  524. }
  525. NonnullRefPtr<Text> Document::create_text_node(const String& data)
  526. {
  527. return adopt(*new Text(*this, data));
  528. }
  529. NonnullRefPtr<Comment> Document::create_comment(const String& data)
  530. {
  531. return adopt(*new Comment(*this, data));
  532. }
  533. NonnullRefPtr<Range> Document::create_range()
  534. {
  535. return Range::create(*this);
  536. }
  537. void Document::set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement* script)
  538. {
  539. m_pending_parsing_blocking_script = script;
  540. }
  541. NonnullRefPtr<HTML::HTMLScriptElement> Document::take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>)
  542. {
  543. return m_pending_parsing_blocking_script.release_nonnull();
  544. }
  545. void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
  546. {
  547. m_scripts_to_execute_when_parsing_has_finished.append(script);
  548. }
  549. NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>)
  550. {
  551. return move(m_scripts_to_execute_when_parsing_has_finished);
  552. }
  553. void Document::add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
  554. {
  555. m_scripts_to_execute_as_soon_as_possible.append(script);
  556. }
  557. NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>)
  558. {
  559. return move(m_scripts_to_execute_as_soon_as_possible);
  560. }
  561. void Document::adopt_node(Node& subtree_root)
  562. {
  563. subtree_root.for_each_in_subtree([&](auto& node) {
  564. node.set_document({}, *this);
  565. return IterationDecision::Continue;
  566. });
  567. }
  568. const DocumentType* Document::doctype() const
  569. {
  570. return first_child_of_type<DocumentType>();
  571. }
  572. const String& Document::compat_mode() const
  573. {
  574. static String back_compat = "BackCompat";
  575. static String css1_compat = "CSS1Compat";
  576. if (m_quirks_mode == QuirksMode::Yes)
  577. return back_compat;
  578. return css1_compat;
  579. }
  580. bool Document::is_editable() const
  581. {
  582. return m_editable;
  583. }
  584. void Document::set_focused_element(Element* element)
  585. {
  586. if (m_focused_element == element)
  587. return;
  588. m_focused_element = element;
  589. if (m_layout_root)
  590. m_layout_root->set_needs_display();
  591. }
  592. void Document::set_ready_state(const String& ready_state)
  593. {
  594. m_ready_state = ready_state;
  595. dispatch_event(Event::create(HTML::EventNames::readystatechange));
  596. }
  597. Page* Document::page()
  598. {
  599. return m_frame ? m_frame->page() : nullptr;
  600. }
  601. const Page* Document::page() const
  602. {
  603. return m_frame ? m_frame->page() : nullptr;
  604. }
  605. EventTarget* Document::get_parent(const Event& event)
  606. {
  607. if (event.type() == HTML::EventNames::load)
  608. return nullptr;
  609. return &window();
  610. }
  611. void Document::completely_finish_loading()
  612. {
  613. // FIXME: This needs to handle iframes.
  614. dispatch_event(DOM::Event::create(HTML::EventNames::load));
  615. }
  616. String Document::cookie() const
  617. {
  618. // FIXME: Support cookies!
  619. return {};
  620. }
  621. void Document::set_cookie(String)
  622. {
  623. // FIXME: Support cookies!
  624. }
  625. }