HTMLIFrameElement.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Layout/FrameBox.h>
  11. #include <LibWeb/Origin.h>
  12. namespace Web::HTML {
  13. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : BrowsingContextContainer(document, move(qualified_name))
  15. {
  16. }
  17. HTMLIFrameElement::~HTMLIFrameElement() = default;
  18. RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  19. {
  20. return adopt_ref(*new Layout::FrameBox(document(), *this, move(style)));
  21. }
  22. void HTMLIFrameElement::parse_attribute(const FlyString& name, const String& value)
  23. {
  24. HTMLElement::parse_attribute(name, value);
  25. if (name == HTML::AttributeNames::src)
  26. load_src(value);
  27. }
  28. void HTMLIFrameElement::inserted()
  29. {
  30. BrowsingContextContainer::inserted();
  31. if (is_connected())
  32. load_src(attribute(HTML::AttributeNames::src));
  33. }
  34. void HTMLIFrameElement::load_src(const String& value)
  35. {
  36. if (!m_nested_browsing_context)
  37. return;
  38. if (value.is_null())
  39. return;
  40. auto url = document().parse_url(value);
  41. if (!url.is_valid()) {
  42. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  43. return;
  44. }
  45. if (url.protocol() == "file" && document().origin().protocol() != "file") {
  46. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  47. return;
  48. }
  49. dbgln("Loading iframe document from {}", value);
  50. m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
  51. }
  52. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
  53. void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
  54. {
  55. // 1. Assert: element's nested browsing context is not null.
  56. VERIFY(element.nested_browsing_context());
  57. // 2. Let childDocument be the active document of element's nested browsing context.
  58. [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
  59. // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
  60. // FIXME: 4. Set childDocument's iframe load in progress flag.
  61. // 5. Fire an event named load at element.
  62. element.dispatch_event(DOM::Event::create(HTML::EventNames::load));
  63. // FIXME: 6. Unset childDocument's iframe load in progress flag.
  64. }
  65. }