HTMLIFrameElement.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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());
  53. // 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.
  54. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  55. process_the_iframe_attributes(true);
  56. }
  57. }
  58. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
  59. void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
  60. {
  61. if (!content_navigable())
  62. return;
  63. // 1. If element's srcdoc attribute is specified, then:
  64. if (has_attribute(HTML::AttributeNames::srcdoc)) {
  65. // 1. Set element's current navigation was lazy loaded boolean to false.
  66. set_current_navigation_was_lazy_loaded(false);
  67. // 2. If the will lazy load element steps given element return true, then:
  68. if (will_lazy_load_element()) {
  69. // 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate to the srcdoc resource.
  70. set_lazy_load_resumption_steps([this]() {
  71. // 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.
  72. navigate_an_iframe_or_frame(URL("about:srcdoc"sv), ReferrerPolicy::ReferrerPolicy::EmptyString, get_attribute(HTML::AttributeNames::srcdoc));
  73. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  74. });
  75. // 2. Set element's current navigation was lazy loaded boolean to true.
  76. set_current_navigation_was_lazy_loaded(true);
  77. // 3. Start intersection-observing a lazy loading element for element.
  78. document().start_intersection_observing_a_lazy_loading_element(*this);
  79. // 4. Return.
  80. return;
  81. }
  82. // 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.
  83. navigate_an_iframe_or_frame(URL("about:srcdoc"sv), ReferrerPolicy::ReferrerPolicy::EmptyString, get_attribute(HTML::AttributeNames::srcdoc));
  84. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  85. return;
  86. }
  87. // 1. Let url be the result of running the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
  88. auto url = shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  89. // 2. If url is null, then return.
  90. if (!url.has_value()) {
  91. return;
  92. }
  93. // 3. If url matches about:blank and initialInsertion is true, then:
  94. if (url_matches_about_blank(*url) && initial_insertion) {
  95. // 1. Run the iframe load event steps given element.
  96. run_iframe_load_event_steps(*this);
  97. // 2. Return.
  98. return;
  99. }
  100. // FIXME: 4. Let referrerPolicy be the current state of element's referrerpolicy content attribute.
  101. auto referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString;
  102. // 5. Set element's current navigation was lazy loaded boolean to false.
  103. set_current_navigation_was_lazy_loaded(false);
  104. // 6. If the will lazy load element steps given element return true, then:
  105. if (will_lazy_load_element()) {
  106. // 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate.
  107. set_lazy_load_resumption_steps([this, url, referrer_policy]() {
  108. // 7. Navigate: navigate an iframe or frame given element, url, and referrerPolicy.
  109. navigate_an_iframe_or_frame(*url, referrer_policy);
  110. });
  111. // 2. Set element's current navigation was lazy loaded boolean to true.
  112. set_current_navigation_was_lazy_loaded(true);
  113. // 3. Start intersection-observing a lazy loading element for element.
  114. document().start_intersection_observing_a_lazy_loading_element(*this);
  115. // 4. Return.
  116. return;
  117. }
  118. // 7. Navigate: navigate an iframe or frame given element, url, and referrerPolicy.
  119. navigate_an_iframe_or_frame(*url, referrer_policy);
  120. }
  121. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  122. void HTMLIFrameElement::removed_from(DOM::Node* node)
  123. {
  124. HTMLElement::removed_from(node);
  125. // When an iframe element is removed from a document, the user agent must destroy the nested navigable of the element.
  126. destroy_the_child_navigable();
  127. }
  128. // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images
  129. void HTMLIFrameElement::apply_presentational_hints(CSS::StyleProperties& style) const
  130. {
  131. for_each_attribute([&](auto& name, auto& value) {
  132. if (name == HTML::AttributeNames::width) {
  133. if (auto parsed_value = parse_dimension_value(value))
  134. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  135. } else if (name == HTML::AttributeNames::height) {
  136. if (auto parsed_value = parse_dimension_value(value))
  137. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  138. }
  139. });
  140. }
  141. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  142. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  143. {
  144. // FIXME: 1. Assert: element's content navigable is not null.
  145. if (!element.content_navigable()) {
  146. // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
  147. dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
  148. return;
  149. }
  150. // 2. Let childDocument be element's content navigable's active document.
  151. [[maybe_unused]] auto child_document = element.content_navigable()->active_document();
  152. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  153. // FIXME: 4. Set childDocument's iframe load in progress flag.
  154. // 5. Fire an event named load at element.
  155. element.dispatch_event(DOM::Event::create(element.realm(), HTML::EventNames::load));
  156. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  157. }
  158. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  159. i32 HTMLIFrameElement::default_tab_index_value() const
  160. {
  161. // See the base function for the spec comments.
  162. return 0;
  163. }
  164. void HTMLIFrameElement::visit_edges(Cell::Visitor& visitor)
  165. {
  166. Base::visit_edges(visitor);
  167. visit_lazy_loading_element(visitor);
  168. }
  169. void HTMLIFrameElement::set_current_navigation_was_lazy_loaded(bool value)
  170. {
  171. m_current_navigation_was_lazy_loaded = value;
  172. // An iframe element whose current navigation was lazy loaded boolean is false potentially delays the load event.
  173. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:potentially-delays-the-load-event
  174. set_potentially_delays_the_load_event(!value);
  175. }
  176. }