HTMLIFrameElement.cpp 3.2 KB

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