HTMLIFrameElement.cpp 10 KB

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