HTMLFrameElement.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLFrameElementPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/HTML/EventNames.h>
  13. #include <LibWeb/HTML/HTMLFrameElement.h>
  14. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  15. namespace Web::HTML {
  16. GC_DEFINE_ALLOCATOR(HTMLFrameElement);
  17. HTMLFrameElement::HTMLFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : NavigableContainer(document, move(qualified_name))
  19. {
  20. // https://html.spec.whatwg.org/multipage/obsolete.html#frames:potentially-delays-the-load-event
  21. // The frame element potentially delays the load event.
  22. set_potentially_delays_the_load_event(true);
  23. }
  24. HTMLFrameElement::~HTMLFrameElement() = default;
  25. void HTMLFrameElement::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFrameElement);
  29. }
  30. // https://html.spec.whatwg.org/multipage/obsolete.html#frames:html-element-insertion-steps
  31. void HTMLFrameElement::inserted()
  32. {
  33. Base::inserted();
  34. // 1. If insertedNode is not in a document tree, then return.
  35. if (!in_a_document_tree())
  36. return;
  37. // 2. If insertedNode's root's browsing context is null, then return.
  38. if (root().document().browsing_context() == nullptr)
  39. return;
  40. // 3. Create a new child navigable for insertedNode.
  41. MUST(create_new_child_navigable(GC::create_function(realm().heap(), [this] {
  42. // 4. Process the frame attributes for insertedNode, with initialInsertion set to true.
  43. process_the_frame_attributes(true);
  44. set_content_navigable_initialized();
  45. })));
  46. }
  47. // https://html.spec.whatwg.org/multipage/obsolete.html#frames:html-element-removing-steps
  48. void HTMLFrameElement::removed_from(DOM::Node* node)
  49. {
  50. Base::removed_from(node);
  51. // The frame HTML element removing steps, given removedNode, are to destroy a child navigable given removedNode.
  52. destroy_the_child_navigable();
  53. }
  54. // https://html.spec.whatwg.org/multipage/obsolete.html#frames:frame-3
  55. void HTMLFrameElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  56. {
  57. Base::attribute_changed(name, old_value, value, namespace_);
  58. // Whenever a frame element with a non-null content navigable has its src attribute set, changed, or removed, the
  59. // user agent must process the frame attributes.
  60. if (content_navigable() && name == HTML::AttributeNames::src)
  61. process_the_frame_attributes();
  62. }
  63. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  64. i32 HTMLFrameElement::default_tab_index_value() const
  65. {
  66. // See the base function for the spec comments.
  67. return 0;
  68. }
  69. void HTMLFrameElement::adjust_computed_style(CSS::StyleProperties& style)
  70. {
  71. // https://drafts.csswg.org/css-display-3/#unbox
  72. if (style.display().is_contents())
  73. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
  74. }
  75. // https://html.spec.whatwg.org/multipage/obsolete.html#process-the-frame-attributes
  76. void HTMLFrameElement::process_the_frame_attributes(bool initial_insertion)
  77. {
  78. // 1. Let url be the result of running the shared attribute processing steps for iframe and frame elements given
  79. // element and initialInsertion.
  80. auto url = shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
  81. // 2. If url is null, then return.
  82. if (!url.has_value())
  83. return;
  84. // 3. If url matches about:blank and initialInsertion is true, then:
  85. if (url_matches_about_blank(*url) && initial_insertion) {
  86. // 1. Fire an event named load at element.
  87. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load));
  88. // 2. Return.
  89. return;
  90. }
  91. // 3. Navigate an iframe or frame given element, url, and the empty string.
  92. navigate_an_iframe_or_frame(*url, ReferrerPolicy::ReferrerPolicy::EmptyString);
  93. }
  94. }