Serenity Operating System
1/*
2 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
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/HTML/Parser/HTMLParser.h>
13#include <LibWeb/Layout/FrameBox.h>
14
15namespace Web::HTML {
16
17HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName qualified_name)
18 : BrowsingContextContainer(document, move(qualified_name))
19{
20}
21
22HTMLIFrameElement::~HTMLIFrameElement() = default;
23
24JS::ThrowCompletionOr<void> HTMLIFrameElement::initialize(JS::Realm& realm)
25{
26 MUST_OR_THROW_OOM(Base::initialize(realm));
27 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLIFrameElementPrototype>(realm, "HTMLIFrameElement"));
28
29 return {};
30}
31
32JS::GCPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
33{
34 return heap().allocate_without_realm<Layout::FrameBox>(document(), *this, move(style));
35}
36
37void HTMLIFrameElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
38{
39 HTMLElement::parse_attribute(name, value);
40 if (name == HTML::AttributeNames::src)
41 load_src(value);
42}
43
44// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-6
45void HTMLIFrameElement::inserted()
46{
47 HTMLElement::inserted();
48
49 // When an iframe element element is inserted into a document whose browsing context is non-null, the user agent must run these steps:
50 if (document().browsing_context()) {
51 // 1. Create a new nested browsing context for element.
52 create_new_nested_browsing_context();
53
54 // 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.
55
56 // 3. Process the iframe attributes for element, with initialInsertion set to true.
57 process_the_iframe_attributes(true);
58 }
59}
60
61// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
62bool HTMLIFrameElement::will_lazy_load_element() const
63{
64 // 1. If scripting is disabled for element, then return false.
65 if (document().is_scripting_disabled())
66 return false;
67
68 // FIXME: 2. If element's lazy loading attribute is in the Lazy state, then return true.
69
70 // 3. Return false.
71 return false;
72}
73
74// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#process-the-iframe-attributes
75void HTMLIFrameElement::process_the_iframe_attributes(bool initial_insertion)
76{
77 // 1. If element's srcdoc attribute is specified, then:
78 if (has_attribute(HTML::AttributeNames::srcdoc)) {
79 // 2. Set element's current navigation was lazy loaded boolean to false.
80 m_current_navigation_was_lazy_loaded = false;
81
82 // 3. If the will lazy load element steps given element return true, then:
83 if (will_lazy_load_element()) {
84 // FIXME: 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate to the srcdoc resource.
85 // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
86 // FIXME: 3. Start intersection-observing a lazy loading element for element.
87 // FIXME: 4. Return.
88 }
89
90 // FIXME: 4. Navigate to the srcdoc resource: navigate an iframe or frame given element and a new response whose URL list is « about:srcdoc », header list is « (`Content-Type`, `text/html`) », and body is the value of element's srcdoc attribute.
91
92 // FIXME: The resulting Document must be considered an iframe srcdoc document.
93
94 return;
95 }
96
97 // 2. Otherwise, run the shared attribute processing steps for iframe and frame elements given element and initialInsertion.
98 shared_attribute_processing_steps_for_iframe_and_frame(initial_insertion);
99}
100
101// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:the-iframe-element-7
102void HTMLIFrameElement::removed_from(DOM::Node* node)
103{
104 HTMLElement::removed_from(node);
105
106 // When an iframe element is removed from a document, the user agent must discard the element's nested browsing context,
107 // if it is not null, and then set the element's nested browsing context to null.
108 if (m_nested_browsing_context) {
109 m_nested_browsing_context->discard();
110 m_nested_browsing_context = nullptr;
111 }
112}
113
114void HTMLIFrameElement::load_src(DeprecatedString const& value)
115{
116 if (!m_nested_browsing_context)
117 return;
118
119 if (value.is_null())
120 return;
121
122 auto url = document().parse_url(value);
123 if (!url.is_valid()) {
124 dbgln("iframe failed to load URL: Invalid URL: {}", value);
125 return;
126 }
127 if (url.scheme() == "file" && document().origin().scheme() != "file") {
128 dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
129 return;
130 }
131
132 dbgln("Loading iframe document from {}", value);
133 m_nested_browsing_context->loader().load(url, FrameLoader::Type::IFrame);
134}
135
136// https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images
137void HTMLIFrameElement::apply_presentational_hints(CSS::StyleProperties& style) const
138{
139 for_each_attribute([&](auto& name, auto& value) {
140 if (name == HTML::AttributeNames::width) {
141 if (auto parsed_value = parse_dimension_value(value))
142 style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
143 } else if (name == HTML::AttributeNames::height) {
144 if (auto parsed_value = parse_dimension_value(value))
145 style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
146 }
147 });
148}
149
150// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
151void run_iframe_load_event_steps(HTML::HTMLIFrameElement& element)
152{
153 // FIXME: 1. Assert: element's nested browsing context is not null.
154 if (!element.nested_browsing_context()) {
155 // FIXME: For some reason, we sometimes end up here in the middle of SunSpider.
156 dbgln("FIXME: run_iframe_load_event_steps called with null nested browsing context");
157 return;
158 }
159
160 // 2. Let childDocument be the active document of element's nested browsing context.
161 [[maybe_unused]] auto* child_document = element.nested_browsing_context()->active_document();
162
163 // FIXME: 3. If childDocument has its mute iframe load flag set, then return.
164
165 // FIXME: 4. Set childDocument's iframe load in progress flag.
166
167 // 5. Fire an event named load at element.
168 element.dispatch_event(DOM::Event::create(element.realm(), HTML::EventNames::load).release_value_but_fixme_should_propagate_errors());
169
170 // FIXME: 6. Unset childDocument's iframe load in progress flag.
171}
172
173// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
174i32 HTMLIFrameElement::default_tab_index_value() const
175{
176 // See the base function for the spec comments.
177 return 0;
178}
179
180}