HTMLIFrameElement.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(&document.window().cached_web_prototype("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. if (!is_connected())
  34. return;
  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. load_src(attribute(HTML::AttributeNames::src));
  40. }
  41. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  42. void HTMLIFrameElement::removed_from(DOM::Node* node)
  43. {
  44. HTMLElement::removed_from(node);
  45. discard_nested_browsing_context();
  46. }
  47. void HTMLIFrameElement::load_src(String const& value)
  48. {
  49. if (!m_nested_browsing_context)
  50. return;
  51. if (value.is_null())
  52. return;
  53. auto url = document().parse_url(value);
  54. if (!url.is_valid()) {
  55. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  56. return;
  57. }
  58. if (url.protocol() == "file" && document().origin().protocol() != "file") {
  59. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  60. return;
  61. }
  62. dbgln("Loading iframe document from {}", value);
  63. m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
  64. }
  65. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  66. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  67. {
  68. // 1. Assert: element's nested browsing context is not null.
  69. VERIFY(element.nested_browsing_context());
  70. // 2. Let childDocument be the active document of element's nested browsing context.
  71. [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
  72. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  73. // FIXME: 4. Set childDocument's iframe load in progress flag.
  74. // 5. Fire an event named load at element.
  75. element.dispatch_event(*DOM::Event::create(element.document().window(), HTML::EventNames::load));
  76. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  77. }
  78. }