HTMLIFrameElement.cpp 7.3 KB

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