HTMLIFrameElement.cpp 8.7 KB

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