HTMLIFrameElement.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/Origin.h>
  11. #include <LibWeb/HTML/Parser/HTMLParser.h>
  12. #include <LibWeb/Layout/FrameBox.h>
  13. namespace Web::HTML {
  14. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : BrowsingContextContainer(document, move(qualified_name))
  16. {
  17. }
  18. HTMLIFrameElement::~HTMLIFrameElement() = default;
  19. JS::ThrowCompletionOr<void> HTMLIFrameElement::initialize(JS::Realm& realm)
  20. {
  21. MUST_OR_THROW_OOM(Base::initialize(realm));
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLIFrameElementPrototype>(realm, "HTMLIFrameElement"));
  23. return {};
  24. }
  25. JS::GCPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  26. {
  27. return heap().allocate_without_realm<Layout::FrameBox>(document(), *this, move(style));
  28. }
  29. void HTMLIFrameElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  30. {
  31. HTMLElement::parse_attribute(name, value);
  32. if (name == HTML::AttributeNames::src)
  33. load_src(value);
  34. }
  35. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
  36. void HTMLIFrameElement::inserted()
  37. {
  38. HTMLElement::inserted();
  39. // When an iframe element element is inserted into a document whose browsing context is non-null, the user agent must run these steps:
  40. if (document().browsing_context()) {
  41. // 1. Create a new nested browsing context for element.
  42. create_new_nested_browsing_context();
  43. // 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.
  44. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  45. process_the_iframe_attributes(true);
  46. }
  47. }
  48. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
  49. bool HTMLIFrameElement::will_lazy_load_element() const
  50. {
  51. // 1. If scripting is disabled for element, then return false.
  52. if (document().is_scripting_disabled())
  53. return false;
  54. // FIXME: 2. If element's lazy loading attribute is in the Lazy state, then return true.
  55. // 3. Return false.
  56. return false;
  57. }
  58. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
  59. void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
  60. {
  61. // 1. If element's srcdoc attribute is specified, then:
  62. if (has_attribute(HTML::AttributeNames::srcdoc)) {
  63. // 2. Set element's current navigation was lazy loaded boolean to false.
  64. m_current_navigation_was_lazy_loaded = false;
  65. // 3. If the will lazy load element steps given element return true, then:
  66. if (will_lazy_load_element()) {
  67. // 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.
  68. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  69. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  70. // FIXME: 4. Return.
  71. }
  72. // FIXME: 4. Navigate to the srcdoc resource: navigate an iframe or frame given element and a new response whose URL list is « about:srcdoc », header list is « (`Content-Type`, `text/html`) », and body is the value of element's srcdoc attribute.
  73. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  74. return;
  75. }
  76. // 2. Otherwise, run the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
  77. shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  78. }
  79. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  80. void HTMLIFrameElement::removed_from(DOM::Node* node)
  81. {
  82. HTMLElement::removed_from(node);
  83. // When an iframe element is removed from a document, the user agent must discard the element's nested browsing context,
  84. // if it is not null, and then set the element's nested browsing context to null.
  85. if (m_nested_browsing_context) {
  86. m_nested_browsing_context->discard();
  87. m_nested_browsing_context = nullptr;
  88. }
  89. }
  90. void HTMLIFrameElement::load_src(DeprecatedString const& value)
  91. {
  92. if (!m_nested_browsing_context)
  93. return;
  94. if (value.is_null())
  95. return;
  96. auto url = document().parse_url(value);
  97. if (!url.is_valid()) {
  98. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  99. return;
  100. }
  101. if (url.scheme() == "file" && document().origin().scheme() != "file") {
  102. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  103. return;
  104. }
  105. dbgln("Loading iframe document from {}", value);
  106. m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
  107. }
  108. // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images
  109. void HTMLIFrameElement::apply_presentational_hints(CSS::StyleProperties& style) const
  110. {
  111. for_each_attribute([&](auto& name, auto& value) {
  112. if (name == HTML::AttributeNames::width) {
  113. if (auto parsed_value = parse_dimension_value(value))
  114. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  115. } else if (name == HTML::AttributeNames::height) {
  116. if (auto parsed_value = parse_dimension_value(value))
  117. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  118. }
  119. });
  120. }
  121. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  122. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  123. {
  124. // FIXME: 1. Assert: element's nested browsing context is not null.
  125. if (!element.nested_browsing_context()) {
  126. // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
  127. dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
  128. return;
  129. }
  130. // 2. Let childDocument be the active document of element's nested browsing context.
  131. [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
  132. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  133. // FIXME: 4. Set childDocument's iframe load in progress flag.
  134. // 5. Fire an event named load at element.
  135. element.dispatch_event(DOM::Event::create(element.realm(), HTML::EventNames::load).release_value_but_fixme_should_propagate_errors());
  136. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  137. }
  138. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  139. i32 HTMLIFrameElement::default_tab_index_value() const
  140. {
  141. // See the base function for the spec comments.
  142. return 0;
  143. }
  144. }