HTMLIFrameElement.cpp 9.7 KB

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