HTMLIFrameElement.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLIFrameElementPrototype.h>
  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/Origin.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. set_prototype(&document.window().ensure_web_prototype<Bindings::HTMLIFrameElementPrototype>("HTMLIFrameElement"));
  18. }
  19. HTMLIFrameElement::~HTMLIFrameElement() = default;
  20. RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  21. {
  22. return adopt_ref(*new Layout::FrameBox(document(), *this, move(style)));
  23. }
  24. void HTMLIFrameElement::parse_attribute(FlyString const& name, String const& value)
  25. {
  26. HTMLElement::parse_attribute(name, value);
  27. if (name == HTML::AttributeNames::src)
  28. load_src(value);
  29. }
  30. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
  31. void HTMLIFrameElement::inserted()
  32. {
  33. HTMLElement::inserted();
  34. if (!is_connected())
  35. return;
  36. // 1. Create a new nested browsing context for element.
  37. create_new_nested_browsing_context();
  38. // 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.
  39. // 3. Process the iframe attributes for element, with initialInsertion set to true.
  40. load_src(attribute(HTML::AttributeNames::src));
  41. }
  42. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
  43. void HTMLIFrameElement::removed_from(DOM::Node* node)
  44. {
  45. HTMLElement::removed_from(node);
  46. discard_nested_browsing_context();
  47. }
  48. void HTMLIFrameElement::load_src(String const& value)
  49. {
  50. if (!m_nested_browsing_context)
  51. return;
  52. if (value.is_null())
  53. return;
  54. auto url = document().parse_url(value);
  55. if (!url.is_valid()) {
  56. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  57. return;
  58. }
  59. if (url.protocol() == "file" && document().origin().protocol() != "file") {
  60. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  61. return;
  62. }
  63. dbgln("Loading iframe document from {}", value);
  64. m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
  65. }
  66. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  67. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  68. {
  69. // 1. Assert: element's nested browsing context is not null.
  70. VERIFY(element.nested_browsing_context());
  71. // 2. Let childDocument be the active document of element's nested browsing context.
  72. [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
  73. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  74. // FIXME: 4. Set childDocument's iframe load in progress flag.
  75. // 5. Fire an event named load at element.
  76. element.dispatch_event(*DOM::Event::create(element.document().window(), HTML::EventNames::load));
  77. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  78. }
  79. }