HTMLIFrameElement.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Event.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/HTMLIFrameElement.h>
  10. #include <LibWeb/HTML/Navigable.h>
  11. #include <LibWeb/HTML/Origin.h>
  12. #include <LibWeb/HTML/Parser/HTMLParser.h>
  13. #include <LibWeb/Layout/FrameBox.h>
  14. namespace Web::HTML {
  15. JS_DEFINE_ALLOCATOR(HTMLIFrameElement);
  16. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : NavigableContainer(document, move(qualified_name))
  18. {
  19. }
  20. HTMLIFrameElement::~HTMLIFrameElement() = default;
  21. void HTMLIFrameElement::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLIFrameElementPrototype>(realm, "HTMLIFrameElement"));
  25. }
  26. JS::GCPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  27. {
  28. return heap().allocate_without_realm<Layout::FrameBox>(document(), *this, move(style));
  29. }
  30. void HTMLIFrameElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  31. {
  32. HTMLElement::attribute_changed(name, value);
  33. if (m_content_navigable)
  34. process_the_iframe_attributes();
  35. }
  36. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
  37. void HTMLIFrameElement::inserted()
  38. {
  39. HTMLElement::inserted();
  40. // When an iframe element element is inserted into a document whose browsing context is non-null, the user agent must run these steps:
  41. if (in_a_document_tree() && document().browsing_context()) {
  42. // 1. Create a new child navigable for element.
  43. MUST(create_new_child_navigable());
  44. // 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.
  45. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  46. process_the_iframe_attributes(true);
  47. }
  48. }
  49. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
  50. bool HTMLIFrameElement::will_lazy_load_element() const
  51. {
  52. // 1. If scripting is disabled for element, then return false.
  53. if (document().is_scripting_disabled())
  54. return false;
  55. // FIXME: 2. If element's lazy loading attribute is in the Lazy state, then return true.
  56. // 3. Return false.
  57. return false;
  58. }
  59. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
  60. void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
  61. {
  62. if (!content_navigable())
  63. return;
  64. // 1. If element's srcdoc attribute is specified, then:
  65. if (has_attribute(HTML::AttributeNames::srcdoc)) {
  66. // 1. Set element's current navigation was lazy loaded boolean to false.
  67. m_current_navigation_was_lazy_loaded = false;
  68. // 2. If the will lazy load element steps given element return true, then:
  69. if (will_lazy_load_element()) {
  70. // 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.
  71. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  72. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  73. // FIXME: 4. Return.
  74. }
  75. // 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.
  76. navigate_an_iframe_or_frame(AK::URL("about:srcdoc"sv), ReferrerPolicy::ReferrerPolicy::EmptyString, get_attribute(HTML::AttributeNames::srcdoc));
  77. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  78. return;
  79. }
  80. // 1. Let url be the result of running the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
  81. auto url = shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  82. // 2. If url is null, then return.
  83. if (!url.has_value()) {
  84. return;
  85. }
  86. // 3. If url matches about:blank and initialInsertion is true, then:
  87. if (url_matches_about_blank(*url) && initial_insertion) {
  88. // 1. Run the iframe load event steps given element.
  89. run_iframe_load_event_steps(*this);
  90. // 2. Return.
  91. return;
  92. }
  93. // FIXME: 4. Let referrerPolicy be the current state of element's referrerpolicy content attribute.
  94. auto referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString;
  95. // 5. Set element's current navigation was lazy loaded boolean to false.
  96. m_current_navigation_was_lazy_loaded = false;
  97. // 6. If the will lazy load element steps given element return true, then:
  98. if (will_lazy_load_element()) {
  99. // FIXME: 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate.
  100. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  101. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  102. // 4. Return.
  103. return;
  104. }
  105. // 7. Navigate: navigate an iframe or frame given element, url, and referrerPolicy.
  106. navigate_an_iframe_or_frame(*url, referrer_policy);
  107. }
  108. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  109. void HTMLIFrameElement::removed_from(DOM::Node* node)
  110. {
  111. HTMLElement::removed_from(node);
  112. // When an iframe element is removed from a document, the user agent must destroy the nested navigable of the element.
  113. destroy_the_child_navigable();
  114. }
  115. // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images
  116. void HTMLIFrameElement::apply_presentational_hints(CSS::StyleProperties& style) const
  117. {
  118. for_each_attribute([&](auto& name, auto& value) {
  119. if (name == HTML::AttributeNames::width) {
  120. if (auto parsed_value = parse_dimension_value(value))
  121. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  122. } else if (name == HTML::AttributeNames::height) {
  123. if (auto parsed_value = parse_dimension_value(value))
  124. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  125. }
  126. });
  127. }
  128. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  129. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  130. {
  131. // FIXME: 1. Assert: element's content navigable is not null.
  132. if (!element.content_navigable()) {
  133. // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
  134. dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
  135. return;
  136. }
  137. // 2. Let childDocument be element's content navigable's active document.
  138. [[maybe_unused]] auto child_document = element.content_navigable()->active_document();
  139. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  140. // FIXME: 4. Set childDocument's iframe load in progress flag.
  141. // 5. Fire an event named load at element.
  142. element.dispatch_event(DOM::Event::create(element.realm(), HTML::EventNames::load));
  143. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  144. }
  145. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  146. i32 HTMLIFrameElement::default_tab_index_value() const
  147. {
  148. // See the base function for the spec comments.
  149. return 0;
  150. }
  151. }