HTMLIFrameElement.cpp 9.4 KB

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