HTMLIFrameElement.cpp 2.5 KB

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