HTMLIFrameElement.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/HTMLIFrameElement.h>
  11. #include <LibWeb/HTML/Navigable.h>
  12. #include <LibWeb/HTML/Origin.h>
  13. #include <LibWeb/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/Layout/FrameBox.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(HTMLIFrameElement);
  17. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : NavigableContainer(document, move(qualified_name))
  19. {
  20. }
  21. HTMLIFrameElement::~HTMLIFrameElement() = default;
  22. void HTMLIFrameElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLIFrameElement);
  26. }
  27. JS::GCPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  28. {
  29. return heap().allocate_without_realm<Layout::FrameBox>(document(), *this, move(style));
  30. }
  31. void HTMLIFrameElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  32. {
  33. HTMLElement::attribute_changed(name, value);
  34. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:process-the-iframe-attributes-2
  35. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:process-the-iframe-attributes-3
  36. // Whenever an iframe element with a non-null content navigable has its srcdoc attribute set, changed, or removed,
  37. // the user agent must process the iframe attributes.
  38. // Similarly, whenever an iframe element with a non-null content navigable but with no srcdoc attribute specified
  39. // has its src attribute set, changed, or removed, the user agent must process the iframe attributes.
  40. if (m_content_navigable) {
  41. if (name == AttributeNames::srcdoc || (name == AttributeNames::src && !has_attribute(AttributeNames::srcdoc)))
  42. process_the_iframe_attributes();
  43. }
  44. }
  45. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
  46. void HTMLIFrameElement::inserted()
  47. {
  48. HTMLElement::inserted();
  49. // When an iframe element element is inserted into a document whose browsing context is non-null, the user agent must run these steps:
  50. if (in_a_document_tree() && document().browsing_context()) {
  51. // 1. Create a new child navigable for element.
  52. MUST(create_new_child_navigable([this] {
  53. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  54. process_the_iframe_attributes(true);
  55. set_content_navigable_initialized();
  56. }));
  57. // FIXME: 2. If element has a sandbox attribute, then parse the sandboxing directive given the attribute's value and element's iframe sandboxing flag set.
  58. }
  59. }
  60. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
  61. void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
  62. {
  63. if (!content_navigable())
  64. return;
  65. // Make sure applying of history step caused by potential sync navigation to "about:blank"
  66. // is finished. Otherwise, it might interrupt navigation caused by changing src or srcdoc.
  67. if (!initial_insertion && !content_navigable_initialized()) {
  68. main_thread_event_loop().spin_processing_tasks_with_source_until(Task::Source::NavigationAndTraversal, [this] { return content_navigable_initialized(); });
  69. }
  70. // 1. If element's srcdoc attribute is specified, then:
  71. if (has_attribute(HTML::AttributeNames::srcdoc)) {
  72. // 1. Set element's current navigation was lazy loaded boolean to false.
  73. set_current_navigation_was_lazy_loaded(false);
  74. // 2. If the will lazy load element steps given element return true, then:
  75. if (will_lazy_load_element()) {
  76. // 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate to the srcdoc resource.
  77. set_lazy_load_resumption_steps([this]() {
  78. // 3. Navigate to the srcdoc resource: navigate an iframe or frame given element, about:srcdoc, the empty string, and the value of element's srcdoc attribute.
  79. navigate_an_iframe_or_frame(URL::URL("about:srcdoc"sv), ReferrerPolicy::ReferrerPolicy::EmptyString, get_attribute(HTML::AttributeNames::srcdoc));
  80. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  81. });
  82. // 2. Set element's current navigation was lazy loaded boolean to true.
  83. set_current_navigation_was_lazy_loaded(true);
  84. // 3. Start intersection-observing a lazy loading element for element.
  85. document().start_intersection_observing_a_lazy_loading_element(*this);
  86. // 4. Return.
  87. return;
  88. }
  89. // 3. Navigate to the srcdoc resource: navigate an iframe or frame given element, about:srcdoc, the empty string, and the value of element's srcdoc attribute.
  90. navigate_an_iframe_or_frame(URL::URL("about:srcdoc"sv), ReferrerPolicy::ReferrerPolicy::EmptyString, get_attribute(HTML::AttributeNames::srcdoc));
  91. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  92. return;
  93. }
  94. // 1. Let url be the result of running the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
  95. auto url = shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  96. // 2. If url is null, then return.
  97. if (!url.has_value()) {
  98. return;
  99. }
  100. // 3. If url matches about:blank and initialInsertion is true, then:
  101. if (url_matches_about_blank(*url) && initial_insertion) {
  102. // 1. Run the iframe load event steps given element.
  103. run_iframe_load_event_steps(*this);
  104. // 2. Return.
  105. return;
  106. }
  107. // FIXME: 4. Let referrerPolicy be the current state of element's referrerpolicy content attribute.
  108. auto referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString;
  109. // 5. Set element's current navigation was lazy loaded boolean to false.
  110. set_current_navigation_was_lazy_loaded(false);
  111. // 6. If the will lazy load element steps given element return true, then:
  112. if (will_lazy_load_element()) {
  113. // 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate.
  114. set_lazy_load_resumption_steps([this, url, referrer_policy]() {
  115. // 7. Navigate: navigate an iframe or frame given element, url, and referrerPolicy.
  116. navigate_an_iframe_or_frame(*url, referrer_policy);
  117. });
  118. // 2. Set element's current navigation was lazy loaded boolean to true.
  119. set_current_navigation_was_lazy_loaded(true);
  120. // 3. Start intersection-observing a lazy loading element for element.
  121. document().start_intersection_observing_a_lazy_loading_element(*this);
  122. // 4. Return.
  123. return;
  124. }
  125. // 7. Navigate: navigate an iframe or frame given element, url, and referrerPolicy.
  126. navigate_an_iframe_or_frame(*url, referrer_policy);
  127. }
  128. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  129. void HTMLIFrameElement::removed_from(DOM::Node* node)
  130. {
  131. HTMLElement::removed_from(node);
  132. // When an iframe element is removed from a document, the user agent must destroy the nested navigable of the element.
  133. destroy_the_child_navigable();
  134. }
  135. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  136. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  137. {
  138. // FIXME: 1. Assert: element's content navigable is not null.
  139. if (!element.content_navigable()) {
  140. // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
  141. dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
  142. return;
  143. }
  144. // 2. Let childDocument be element's content navigable's active document.
  145. [[maybe_unused]] auto child_document = element.content_navigable()->active_document();
  146. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  147. // FIXME: 4. Set childDocument's iframe load in progress flag.
  148. // 5. Fire an event named load at element.
  149. element.dispatch_event(DOM::Event::create(element.realm(), HTML::EventNames::load));
  150. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  151. }
  152. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  153. i32 HTMLIFrameElement::default_tab_index_value() const
  154. {
  155. // See the base function for the spec comments.
  156. return 0;
  157. }
  158. void HTMLIFrameElement::visit_edges(Cell::Visitor& visitor)
  159. {
  160. Base::visit_edges(visitor);
  161. visit_lazy_loading_element(visitor);
  162. }
  163. void HTMLIFrameElement::set_current_navigation_was_lazy_loaded(bool value)
  164. {
  165. m_current_navigation_was_lazy_loaded = value;
  166. // An iframe element whose current navigation was lazy loaded boolean is false potentially delays the load event.
  167. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:potentially-delays-the-load-event
  168. set_potentially_delays_the_load_event(!value);
  169. }
  170. }