HTMLIFrameElement.cpp 9.0 KB

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