HTMLIFrameElement.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/Layout/FrameBox.h>
  12. namespace Web::HTML {
  13. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : BrowsingContextContainer(document, move(qualified_name))
  15. {
  16. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLIFrameElement"));
  17. }
  18. HTMLIFrameElement::~HTMLIFrameElement() = default;
  19. RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  20. {
  21. return adopt_ref(*new Layout::FrameBox(document(), *this, move(style)));
  22. }
  23. void HTMLIFrameElement::parse_attribute(FlyString const& name, String const& value)
  24. {
  25. HTMLElement::parse_attribute(name, value);
  26. if (name == HTML::AttributeNames::src)
  27. load_src(value);
  28. }
  29. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
  30. void HTMLIFrameElement::inserted()
  31. {
  32. HTMLElement::inserted();
  33. // When an iframe element element is inserted into a document whose browsing context is non-null, the user agent must run these steps:
  34. if (document().browsing_context()) {
  35. // 1. Create a new nested browsing context for element.
  36. create_new_nested_browsing_context();
  37. // 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.
  38. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  39. process_the_iframe_attributes(true);
  40. load_src(attribute(HTML::AttributeNames::src));
  41. }
  42. }
  43. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
  44. bool HTMLIFrameElement::will_lazy_load_element() const
  45. {
  46. // 1. If scripting is disabled for element, then return false.
  47. if (document().is_scripting_disabled())
  48. return false;
  49. // FIXME: 2. If element's lazy loading attribute is in the Lazy state, then return true.
  50. // 3. Return false.
  51. return false;
  52. }
  53. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
  54. void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
  55. {
  56. // 1. If element's srcdoc attribute is specified, then:
  57. if (has_attribute(HTML::AttributeNames::srcdoc)) {
  58. // 2. Set element's current navigation was lazy loaded boolean to false.
  59. m_current_navigation_was_lazy_loaded = false;
  60. // 3. If the will lazy load element steps given element return true, then:
  61. if (will_lazy_load_element()) {
  62. // 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.
  63. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  64. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  65. // FIXME: 4. Return.
  66. }
  67. // 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.
  68. // FIXME: The resulting Document must be considered an iframe srcdoc document.
  69. return;
  70. }
  71. // 2. Otherwise, run the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
  72. shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  73. }
  74. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  75. void HTMLIFrameElement::removed_from(DOM::Node* node)
  76. {
  77. HTMLElement::removed_from(node);
  78. // When an iframe element is removed from a document, the user agent must discard the element's nested browsing context,
  79. // if it is not null, and then set the element's nested browsing context to null.
  80. if (m_nested_browsing_context) {
  81. m_nested_browsing_context->discard();
  82. m_nested_browsing_context = nullptr;
  83. }
  84. }
  85. void HTMLIFrameElement::load_src(String const& value)
  86. {
  87. if (!m_nested_browsing_context)
  88. return;
  89. if (value.is_null())
  90. return;
  91. auto url = document().parse_url(value);
  92. if (!url.is_valid()) {
  93. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  94. return;
  95. }
  96. if (url.scheme() == "file" && document().origin().scheme() != "file") {
  97. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  98. return;
  99. }
  100. dbgln("Loading iframe document from {}", value);
  101. m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
  102. }
  103. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  104. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  105. {
  106. // FIXME: 1. Assert: element's nested browsing context is not null.
  107. if (!element.nested_browsing_context()) {
  108. // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
  109. dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
  110. return;
  111. }
  112. // 2. Let childDocument be the active document of element's nested browsing context.
  113. [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
  114. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  115. // FIXME: 4. Set childDocument's iframe load in progress flag.
  116. // 5. Fire an event named load at element.
  117. element.dispatch_event(*DOM::Event::create(element.realm(), HTML::EventNames::load));
  118. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  119. }
  120. }