Skip to main content

script/dom/html/embedded_content/
htmliframeelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::rc::Rc;
7
8use content_security_policy::sandboxing_directive::{
9    SandboxingFlagSet, parse_a_sandboxing_directive,
10};
11use dom_struct::dom_struct;
12use embedder_traits::ViewportDetails;
13use html5ever::{LocalName, Prefix, local_name, ns};
14use js::context::JSContext;
15use js::rust::HandleObject;
16use net_traits::ReferrerPolicy;
17use net_traits::request::Destination;
18use profile_traits::ipc as ProfiledIpc;
19use script_bindings::cell::DomRefCell;
20use script_traits::{NewPipelineInfo, UpdatePipelineIdReason};
21use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
22use servo_constellation_traits::{
23    IFrameLoadInfo, IFrameLoadInfoWithData, LoadData, LoadOrigin, NavigationHistoryBehavior,
24    ScriptToConstellationMessage, TargetSnapshotParams,
25};
26use servo_url::ServoUrl;
27use style::attr::{AttrValue, LengthOrPercentageOrAuto};
28use stylo_atoms::Atom;
29
30use crate::document_loader::{LoadBlocker, LoadType};
31use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
32use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
33use crate::dom::bindings::codegen::UnionTypes::TrustedHTMLOrString;
34use crate::dom::bindings::error::Fallible;
35use crate::dom::bindings::inheritance::Castable;
36use crate::dom::bindings::refcounted::Trusted;
37use crate::dom::bindings::reflector::DomGlobal;
38use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
39use crate::dom::bindings::str::{DOMString, USVString};
40use crate::dom::document::Document;
41use crate::dom::domtokenlist::DOMTokenList;
42use crate::dom::element::attributes::storage::AttrRef;
43use crate::dom::element::{AttributeMutation, Element, reflect_referrer_policy_attribute};
44use crate::dom::eventtarget::EventTarget;
45use crate::dom::globalscope::GlobalScope;
46use crate::dom::html::htmlelement::HTMLElement;
47use crate::dom::node::virtualmethods::VirtualMethods;
48use crate::dom::node::{BindContext, Node, NodeDamage, NodeTraits, UnbindContext};
49use crate::dom::performance::performanceresourcetiming::InitiatorType;
50use crate::dom::trustedtypes::trustedhtml::TrustedHTML;
51use crate::dom::windowproxy::WindowProxy;
52use crate::navigation::{
53    determine_creation_sandboxing_flags, determine_iframe_element_referrer_policy,
54};
55use crate::network_listener::ResourceTimingListener;
56use crate::script_thread::{ScriptThread, with_script_thread};
57use crate::script_window_proxies::ScriptWindowProxies;
58
59#[derive(PartialEq)]
60enum PipelineType {
61    InitialAboutBlank,
62    Navigation,
63}
64
65#[derive(Clone, Copy, PartialEq)]
66pub(crate) enum ProcessingMode {
67    FirstTime,
68    NotFirstTime,
69}
70
71/// <https://html.spec.whatwg.org/multipage/#lazy-load-resumption-steps>
72#[derive(Clone, Copy, Default, MallocSizeOf, PartialEq)]
73enum LazyLoadResumptionSteps {
74    #[default]
75    None,
76    SrcDoc,
77}
78
79#[dom_struct]
80pub(crate) struct HTMLIFrameElement {
81    htmlelement: HTMLElement,
82    #[no_trace]
83    webview_id: Cell<Option<WebViewId>>,
84    #[no_trace]
85    browsing_context_id: Cell<Option<BrowsingContextId>>,
86    #[no_trace]
87    pipeline_id: Cell<Option<PipelineId>>,
88    #[no_trace]
89    pending_pipeline_id: Cell<Option<PipelineId>>,
90    #[no_trace]
91    about_blank_pipeline_id: Cell<Option<PipelineId>>,
92    sandbox: MutNullableDom<DOMTokenList>,
93    #[no_trace]
94    sandboxing_flag_set: Cell<Option<SandboxingFlagSet>>,
95    load_blocker: DomRefCell<Option<LoadBlocker>>,
96    throttled: Cell<bool>,
97    #[conditional_malloc_size_of]
98    script_window_proxies: Rc<ScriptWindowProxies>,
99    /// <https://html.spec.whatwg.org/multipage/#current-navigation-was-lazy-loaded>
100    current_navigation_was_lazy_loaded: Cell<bool>,
101    /// <https://html.spec.whatwg.org/multipage/#lazy-load-resumption-steps>
102    #[no_trace]
103    lazy_load_resumption_steps: Cell<LazyLoadResumptionSteps>,
104    /// Keeping track of whether the iframe will be navigated
105    /// outside of the processing of it's attribute(for example: form navigation).
106    /// This is necessary to prevent the iframe load event steps
107    /// from asynchronously running for the initial blank document
108    /// while script at this point(when the flag is set)
109    /// expects those to run only for the navigated documented.
110    pending_navigation: Cell<bool>,
111    /// Whether a load event was synchronously fired, for example when
112    /// an empty iframe is attached. In that case, we shouldn't fire a
113    /// subsequent asynchronous load event.
114    already_fired_synchronous_load_event: Cell<bool>,
115}
116
117impl HTMLIFrameElement {
118    /// <https://html.spec.whatwg.org/multipage/#shared-attribute-processing-steps-for-iframe-and-frame-elements>,
119    fn shared_attribute_processing_steps_for_iframe_and_frame_elements(
120        &self,
121        _mode: ProcessingMode,
122    ) -> Option<ServoUrl> {
123        let element = self.upcast::<Element>();
124        // Step 2. If element has a src attribute specified, and its value is not the empty string, then:
125        let url = element
126            .get_attribute_string_value(&local_name!("src"))
127            .and_then(|url| {
128                if url.is_empty() {
129                    None
130                } else {
131                    // Step 2.1. Let maybeURL be the result of encoding-parsing a URL given that attribute's value,
132                    // relative to element's node document.
133                    // Step 2.2. If maybeURL is not failure, then set url to maybeURL.
134                    self.owner_document().encoding_parse_a_url(&url).ok()
135                }
136            })
137            // Step 1. Let url be the URL record about:blank.
138            .unwrap_or_else(|| ServoUrl::parse("about:blank").unwrap());
139        // Step 3. If the inclusive ancestor navigables of element's node navigable contains
140        // a navigable whose active document's URL equals url with exclude fragments set to true, then return null.
141        // TODO
142
143        // Step 4. If url matches about:blank and initialInsertion is true, then perform the URL and history update steps
144        // given element's content navigable's active document and url.
145        // TODO
146
147        // Step 5. Return url.
148        Some(url)
149    }
150
151    pub(crate) fn navigate_or_reload_child_browsing_context(
152        &self,
153        load_data: LoadData,
154        history_handling: NavigationHistoryBehavior,
155        mode: ProcessingMode,
156        target_snapshot_params: TargetSnapshotParams,
157        cx: &mut JSContext,
158    ) {
159        // In case we fired a synchronous load event, but navigate away
160        // in the event listener of that event, then we should still
161        // fire a second asynchronous load event when that navigation
162        // finishes. Therefore, on any navigation (but not the initial
163        // about blank), we should always set this to false, regardless
164        // of whether we synchronously fired a load in the same microtask.
165        self.already_fired_synchronous_load_event.set(false);
166
167        self.start_new_pipeline(
168            cx,
169            load_data,
170            PipelineType::Navigation,
171            history_handling,
172            mode,
173            target_snapshot_params,
174        );
175    }
176
177    fn start_new_pipeline(
178        &self,
179        cx: &mut JSContext,
180        mut load_data: LoadData,
181        pipeline_type: PipelineType,
182        history_handling: NavigationHistoryBehavior,
183        mode: ProcessingMode,
184        target_snapshot_params: TargetSnapshotParams,
185    ) {
186        let document = self.owner_document();
187
188        {
189            let load_blocker = &self.load_blocker;
190            // Any oustanding load is finished from the point of view of the blocked
191            // document; the new navigation will continue blocking it.
192            LoadBlocker::terminate(load_blocker, cx);
193
194            *load_blocker.borrow_mut() = Some(LoadBlocker::new(
195                &document,
196                LoadType::Subframe(load_data.url.clone()),
197            ));
198        }
199
200        if load_data.url.scheme() != "javascript" {
201            self.continue_navigation(
202                cx,
203                load_data,
204                pipeline_type,
205                history_handling,
206                target_snapshot_params,
207            );
208            return;
209        }
210
211        // TODO(jdm): The spec uses the navigate algorithm here, but
212        //   our iframe navigation is not yet unified enough to follow that.
213        //   Eventually we should remove the task and invoke ScriptThread::navigate instead.
214        let iframe = Trusted::new(self);
215        let doc = Trusted::new(&*document);
216        document
217            .global()
218            .task_manager()
219            .networking_task_source()
220            .queue(task!(navigate_to_javascript: move |cx| {
221                let this = iframe.root();
222                let window_proxy = this.GetContentWindow();
223                if let Some(window_proxy) = window_proxy {
224                    // If this method returns false we are not creating a new
225                    // document and the frame can be considered loaded.
226                    if !ScriptThread::navigate_to_javascript_url(
227                        cx,
228                        &this.owner_global(),
229                        &window_proxy.global(),
230                        &mut load_data,
231                        Some(this.upcast()),
232                        Some(mode == ProcessingMode::FirstTime),
233                    ) {
234                        LoadBlocker::terminate(&this.load_blocker, cx);
235                        return;
236                    }
237                    load_data.about_base_url = doc.root().about_base_url();
238                }
239                this.continue_navigation(cx, load_data, pipeline_type, history_handling, target_snapshot_params);
240            }));
241    }
242
243    fn continue_navigation(
244        &self,
245        cx: &mut JSContext,
246        load_data: LoadData,
247        pipeline_type: PipelineType,
248        history_handling: NavigationHistoryBehavior,
249        target_snapshot_params: TargetSnapshotParams,
250    ) {
251        let browsing_context_id = match self.browsing_context_id() {
252            None => return warn!("Attempted to start a new pipeline on an unattached iframe."),
253            Some(id) => id,
254        };
255
256        let webview_id = match self.webview_id() {
257            None => return warn!("Attempted to start a new pipeline on an unattached iframe."),
258            Some(id) => id,
259        };
260
261        let window = self.owner_window();
262        let old_pipeline_id = self.pipeline_id();
263        let new_pipeline_id = PipelineId::new();
264        self.pending_pipeline_id.set(Some(new_pipeline_id));
265
266        let load_info = IFrameLoadInfo {
267            parent_pipeline_id: window.pipeline_id(),
268            browsing_context_id,
269            webview_id,
270            new_pipeline_id,
271            is_private: false, // FIXME
272            inherited_secure_context: load_data.inherited_secure_context,
273            history_handling,
274            target_snapshot_params,
275        };
276
277        let viewport_details = window
278            .get_iframe_viewport_details_if_known(browsing_context_id)
279            .unwrap_or_else(|| ViewportDetails {
280                hidpi_scale_factor: window.device_pixel_ratio(),
281                ..Default::default()
282            });
283
284        match pipeline_type {
285            PipelineType::InitialAboutBlank => {
286                self.about_blank_pipeline_id.set(Some(new_pipeline_id));
287
288                let load_info = IFrameLoadInfoWithData {
289                    info: load_info,
290                    load_data: load_data.clone(),
291                    old_pipeline_id,
292                    viewport_details,
293                    theme: window.theme(),
294                };
295                window
296                    .as_global_scope()
297                    .script_to_constellation_chan()
298                    .send(ScriptToConstellationMessage::ScriptNewIFrame(load_info))
299                    .unwrap();
300
301                let new_pipeline_info = NewPipelineInfo {
302                    parent_info: Some(window.pipeline_id()),
303                    new_pipeline_id,
304                    browsing_context_id,
305                    webview_id,
306                    opener: None,
307                    load_data,
308                    viewport_details,
309                    user_content_manager_id: None,
310                    theme: window.theme(),
311                    target_snapshot_params,
312                };
313
314                self.pipeline_id.set(Some(new_pipeline_id));
315                with_script_thread(|script_thread| {
316                    script_thread.spawn_pipeline(cx, new_pipeline_info);
317                });
318            },
319            PipelineType::Navigation => {
320                let load_info = IFrameLoadInfoWithData {
321                    info: load_info,
322                    load_data,
323                    old_pipeline_id,
324                    viewport_details,
325                    theme: window.theme(),
326                };
327                window
328                    .as_global_scope()
329                    .script_to_constellation_chan()
330                    .send(ScriptToConstellationMessage::ScriptLoadedURLInIFrame(
331                        load_info,
332                    ))
333                    .unwrap();
334            },
335        }
336    }
337
338    /// When an iframe is first inserted into the document,
339    /// an "about:blank" document is created,
340    /// and synchronously processed by the script thread.
341    /// This initial synchronous load should have no noticeable effect in script.
342    /// See the note in `iframe_load_event_steps`.
343    pub(crate) fn is_initial_blank_document(&self) -> bool {
344        self.pending_pipeline_id.get() == self.about_blank_pipeline_id.get()
345    }
346
347    /// <https://html.spec.whatwg.org/multipage/#navigate-an-iframe-or-frame>
348    fn navigate_an_iframe_or_frame(
349        &self,
350        cx: &mut JSContext,
351        load_data: LoadData,
352        mode: ProcessingMode,
353    ) {
354        // Step 2. If element's content navigable's active document is not completely loaded,
355        // then set historyHandling to "replace".
356        let history_handling = if !self
357            .GetContentDocument()
358            .is_some_and(|doc| doc.completely_loaded())
359        {
360            NavigationHistoryBehavior::Replace
361        } else {
362            // Step 1. Let historyHandling be "auto".
363            NavigationHistoryBehavior::Auto
364        };
365        // Step 3. If element is an iframe, then set element's pending resource-timing start time
366        // to the current high resolution time given element's node document's relevant global object.
367        // TODO
368
369        // Step 4. Navigate element's content navigable to url using element's node document,
370        // with historyHandling set to historyHandling, referrerPolicy set to referrerPolicy,
371        // documentResource set to srcdocString, and initialInsertion set to initialInsertion.
372        let target_snapshot_params = snapshot_self(self);
373        self.navigate_or_reload_child_browsing_context(
374            load_data,
375            history_handling,
376            mode,
377            target_snapshot_params,
378            cx,
379        );
380    }
381
382    /// <https://html.spec.whatwg.org/multipage/#will-lazy-load-element-steps>
383    fn will_lazy_load_element_steps(&self) -> bool {
384        // Step 1. If scripting is disabled for element, then return false.
385        if !self.owner_document().scripting_enabled() {
386            return false;
387        }
388        // Step 2. If element's lazy loading attribute is in the Lazy state, then return true.
389        // Step 3. Return false.
390        self.Loading() == "lazy"
391    }
392
393    /// Step 1.3. of <https://html.spec.whatwg.org/multipage/#process-the-iframe-attributes>
394    fn navigate_to_the_srcdoc_resource(&self, mode: ProcessingMode, cx: &mut JSContext) {
395        // Step 1.3. Navigate to the srcdoc resource: Navigate an iframe or frame given element,
396        // about:srcdoc, the empty string, and the value of element's srcdoc attribute.
397        let url = ServoUrl::parse("about:srcdoc").unwrap();
398        let document = self.owner_document();
399        let window = self.owner_window();
400        let pipeline_id = Some(window.pipeline_id());
401        let mut load_data = LoadData::new(
402            LoadOrigin::Script(document.origin().snapshot()),
403            url,
404            Some(document.base_url()),
405            pipeline_id,
406            window.as_global_scope().get_referrer(),
407            document.get_referrer_policy(),
408            Some(window.as_global_scope().is_secure_context()),
409            Some(document.insecure_requests_policy()),
410            document.has_trustworthy_ancestor_or_current_origin(),
411            self.sandboxing_flag_set(),
412        );
413        load_data.destination = Destination::IFrame;
414        load_data.policy_container = Some(window.as_global_scope().policy_container());
415        load_data.srcdoc = String::from(
416            self.upcast::<Element>()
417                .get_string_attribute(&local_name!("srcdoc")),
418        );
419
420        self.navigate_an_iframe_or_frame(cx, load_data, mode);
421    }
422
423    /// <https://html.spec.whatwg.org/multipage/#the-iframe-element:potentially-delays-the-load-event>
424    fn mark_navigation_as_lazy_loaded(&self, cx: &mut JSContext) {
425        // > An iframe element whose current navigation was lazy loaded boolean is false potentially delays the load event.
426        self.current_navigation_was_lazy_loaded.set(true);
427        let blocker = &self.load_blocker;
428        LoadBlocker::terminate(blocker, cx);
429    }
430
431    /// <https://html.spec.whatwg.org/multipage/#process-the-iframe-attributes>
432    fn process_the_iframe_attributes(&self, mode: ProcessingMode, cx: &mut JSContext) {
433        let element = self.upcast::<Element>();
434
435        // Step 1. If `element`'s `srcdoc` attribute is specified, then:
436        //
437        // Note that this also includes the empty string
438        if element.has_attribute(&local_name!("srcdoc")) {
439            // Step 1.1. Set element's current navigation was lazy loaded boolean to false.
440            self.current_navigation_was_lazy_loaded.set(false);
441            // Step 1.2. If the will lazy load element steps given element return true, then:
442            if self.will_lazy_load_element_steps() {
443                // Step 1.2.1. Set element's lazy load resumption steps to the rest of this algorithm
444                // starting with the step labeled navigate to the srcdoc resource.
445                self.lazy_load_resumption_steps
446                    .set(LazyLoadResumptionSteps::SrcDoc);
447                // Step 1.2.2. Set element's current navigation was lazy loaded boolean to true.
448                self.mark_navigation_as_lazy_loaded(cx);
449                // Step 1.2.3. Start intersection-observing a lazy loading element for element.
450                // TODO
451                // Step 1.2.4. Return.
452                return;
453            }
454            // Step 1.3. Navigate to the srcdoc resource: Navigate an iframe or frame given element,
455            // about:srcdoc, the empty string, and the value of element's srcdoc attribute.
456            self.navigate_to_the_srcdoc_resource(mode, cx);
457            return;
458        }
459
460        let window = self.owner_window();
461
462        // https://html.spec.whatwg.org/multipage/#attr-iframe-name
463        // Note: the spec says to set the name 'when the nested browsing context is created'.
464        // The current implementation sets the name on the window,
465        // when the iframe attributes are first processed.
466        if mode == ProcessingMode::FirstTime &&
467            let Some(window) = self.GetContentWindow()
468        {
469            window.set_name(
470                element
471                    .get_name()
472                    .map_or(DOMString::from(""), |n| DOMString::from(&*n)),
473            );
474        }
475
476        // Step 2.1. Let url be the result of running the shared attribute processing steps
477        // for iframe and frame elements given element and initialInsertion.
478        let Some(url) = self.shared_attribute_processing_steps_for_iframe_and_frame_elements(mode)
479        else {
480            // Step 2.2. If url is null, then return.
481            return;
482        };
483
484        // Step 2.3. If url matches about:blank and initialInsertion is true, then:
485        if url.matches_about_blank() && mode == ProcessingMode::FirstTime {
486            // We should **not** send a load event in `iframe_load_event_steps`.
487            self.already_fired_synchronous_load_event.set(true);
488            // Step 2.3.1. Run the iframe load event steps given element.
489            self.run_iframe_load_event_steps(cx);
490            // Step 2.3.2. Return.
491            return;
492        }
493
494        // Step 2.4: Let referrerPolicy be the current state of element's referrerpolicy content
495        // attribute.
496        let document = self.owner_document();
497        let referrer_policy_token = self.ReferrerPolicy();
498
499        // Note: despite not being explicitly stated in the spec steps, this falls back to
500        // document's referrer policy here because it satisfies the expectations that when unset,
501        // the iframe should inherit the referrer policy of its parent
502        let referrer_policy = match ReferrerPolicy::from(&*referrer_policy_token.str()) {
503            ReferrerPolicy::EmptyString => document.get_referrer_policy(),
504            policy => policy,
505        };
506
507        // TODO(#25748):
508        // By spec, we return early if there's an ancestor browsing context
509        // "whose active document's url, ignoring fragments, is equal".
510        // However, asking about ancestor browsing contexts is more nuanced than
511        // it sounds and not implemented here.
512        // Within a single origin, we can do it by walking window proxies,
513        // and this check covers only that single-origin case, protecting
514        // against simple typo self-includes but nothing more elaborate.
515        let mut ancestor = window.GetParent();
516        while let Some(a) = ancestor {
517            if let Some(ancestor_url) = a.document().map(|d| d.url()) &&
518                ancestor_url.scheme() == url.scheme() &&
519                ancestor_url.username() == url.username() &&
520                ancestor_url.password() == url.password() &&
521                ancestor_url.host() == url.host() &&
522                ancestor_url.port() == url.port() &&
523                ancestor_url.path() == url.path() &&
524                ancestor_url.query() == url.query()
525            {
526                return;
527            }
528            ancestor = a.parent().map(DomRoot::from_ref);
529        }
530
531        let (creator_pipeline_id, about_base_url) = if url.matches_about_blank() {
532            (Some(window.pipeline_id()), Some(document.base_url()))
533        } else {
534            (None, document.about_base_url())
535        };
536
537        let propagate_encoding_to_child_document = url.origin().same_origin(&window.origin());
538        let mut load_data = LoadData::new(
539            LoadOrigin::Script(document.origin().snapshot()),
540            url,
541            about_base_url,
542            creator_pipeline_id,
543            window.as_global_scope().get_referrer(),
544            referrer_policy,
545            Some(window.as_global_scope().is_secure_context()),
546            Some(document.insecure_requests_policy()),
547            document.has_trustworthy_ancestor_or_current_origin(),
548            self.sandboxing_flag_set(),
549        );
550        load_data.destination = Destination::IFrame;
551        load_data.policy_container = Some(window.as_global_scope().policy_container());
552        if propagate_encoding_to_child_document {
553            load_data.container_document_encoding = Some(document.encoding());
554        }
555
556        let pipeline_id = self.pipeline_id();
557        // If the initial `about:blank` page is the current page, load with replacement enabled,
558        // see https://html.spec.whatwg.org/multipage/#the-iframe-element:about:blank-3
559        let is_about_blank =
560            pipeline_id.is_some() && pipeline_id == self.about_blank_pipeline_id.get();
561
562        let history_handling = if is_about_blank {
563            NavigationHistoryBehavior::Replace
564        } else {
565            NavigationHistoryBehavior::Push
566        };
567
568        let target_snapshot_params = snapshot_self(self);
569        self.navigate_or_reload_child_browsing_context(
570            load_data,
571            history_handling,
572            mode,
573            target_snapshot_params,
574            cx,
575        );
576    }
577
578    /// <https://html.spec.whatwg.org/multipage/#create-a-new-child-navigable>
579    /// Synchronously create a new browsing context(This is not a navigation).
580    /// The pipeline started here should remain unnoticeable to script, but this is not easy
581    /// to refactor because it appears other features have come to rely on the current behavior.
582    /// For now only the iframe load event steps are skipped in some cases for this initial document,
583    /// and we still fire load and pageshow events as part of `maybe_queue_document_completion`.
584    /// Also, some controversy spec-wise remains: <https://github.com/whatwg/html/issues/4965>
585    fn create_nested_browsing_context(&self, cx: &mut JSContext) {
586        let url = ServoUrl::parse("about:blank").unwrap();
587        let document = self.owner_document();
588        let window = self.owner_window();
589        let pipeline_id = Some(window.pipeline_id());
590        let mut load_data = LoadData::new(
591            LoadOrigin::Script(document.origin().snapshot()),
592            url,
593            Some(document.base_url()),
594            pipeline_id,
595            window.as_global_scope().get_referrer(),
596            document.get_referrer_policy(),
597            Some(window.as_global_scope().is_secure_context()),
598            Some(document.insecure_requests_policy()),
599            document.has_trustworthy_ancestor_or_current_origin(),
600            self.sandboxing_flag_set(),
601        );
602        load_data.is_initial_about_blank = true;
603        load_data.destination = Destination::IFrame;
604        load_data.policy_container = Some(window.as_global_scope().policy_container());
605
606        let browsing_context_id = BrowsingContextId::new();
607        let webview_id = window.window_proxy().webview_id();
608        self.pipeline_id.set(None);
609        self.pending_pipeline_id.set(None);
610        self.webview_id.set(Some(webview_id));
611        self.browsing_context_id.set(Some(browsing_context_id));
612        self.start_new_pipeline(
613            cx,
614            load_data,
615            PipelineType::InitialAboutBlank,
616            NavigationHistoryBehavior::Push,
617            ProcessingMode::FirstTime,
618            snapshot_self(self),
619        );
620    }
621
622    fn destroy_nested_browsing_context(&self) {
623        self.pipeline_id.set(None);
624        self.pending_pipeline_id.set(None);
625        self.about_blank_pipeline_id.set(None);
626        self.webview_id.set(None);
627        if let Some(browsing_context_id) = self.browsing_context_id.take() {
628            self.script_window_proxies.remove(browsing_context_id)
629        }
630    }
631
632    /// Returns true if the contained pipeline was updated, false otherwise.
633    /// This can occur if the iframe's nested browsing context has changed
634    /// since the asynchronous update was started.
635    pub(crate) fn update_pipeline_id(
636        &self,
637        new_pipeline_id: PipelineId,
638        reason: UpdatePipelineIdReason,
639        cx: &mut JSContext,
640    ) -> bool {
641        // For all updates except the one for the initial blank document,
642        // we need to set the flag back to false because the navigation is complete,
643        // because the goal is to, when a navigation is pending, to skip the async load
644        // steps of the initial blank document.
645        if !self.is_initial_blank_document() {
646            self.pending_navigation.set(false);
647        }
648        if self.pending_pipeline_id.get() != Some(new_pipeline_id) &&
649            reason == UpdatePipelineIdReason::Navigation
650        {
651            return false;
652        }
653
654        self.pipeline_id.set(Some(new_pipeline_id));
655
656        // Only terminate the load blocker if the pipeline id was updated due to a traversal.
657        // The load blocker will be terminated for a navigation in iframe_load_event_steps.
658        if reason == UpdatePipelineIdReason::Traversal {
659            let blocker = &self.load_blocker;
660            LoadBlocker::terminate(blocker, cx);
661        }
662
663        self.upcast::<Node>().dirty(cx.no_gc(), NodeDamage::Other);
664        true
665    }
666
667    fn new_inherited(
668        local_name: LocalName,
669        prefix: Option<Prefix>,
670        document: &Document,
671    ) -> HTMLIFrameElement {
672        HTMLIFrameElement {
673            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
674            browsing_context_id: Cell::new(None),
675            webview_id: Cell::new(None),
676            pipeline_id: Cell::new(None),
677            pending_pipeline_id: Cell::new(None),
678            about_blank_pipeline_id: Cell::new(None),
679            sandbox: Default::default(),
680            sandboxing_flag_set: Cell::new(None),
681            load_blocker: DomRefCell::new(None),
682            throttled: Cell::new(false),
683            script_window_proxies: ScriptThread::window_proxies(),
684            current_navigation_was_lazy_loaded: Default::default(),
685            lazy_load_resumption_steps: Default::default(),
686            pending_navigation: Default::default(),
687            already_fired_synchronous_load_event: Default::default(),
688        }
689    }
690
691    pub(crate) fn new(
692        cx: &mut JSContext,
693        local_name: LocalName,
694        prefix: Option<Prefix>,
695        document: &Document,
696        proto: Option<HandleObject>,
697    ) -> DomRoot<HTMLIFrameElement> {
698        Node::reflect_node_with_proto(
699            cx,
700            Box::new(HTMLIFrameElement::new_inherited(
701                local_name, prefix, document,
702            )),
703            document,
704            proto,
705        )
706    }
707
708    #[inline]
709    pub(crate) fn pipeline_id(&self) -> Option<PipelineId> {
710        self.pipeline_id.get()
711    }
712
713    #[inline]
714    pub(crate) fn browsing_context_id(&self) -> Option<BrowsingContextId> {
715        self.browsing_context_id.get()
716    }
717
718    #[inline]
719    pub(crate) fn webview_id(&self) -> Option<WebViewId> {
720        self.webview_id.get()
721    }
722
723    #[inline]
724    pub(crate) fn sandboxing_flag_set(&self) -> SandboxingFlagSet {
725        self.sandboxing_flag_set
726            .get()
727            .unwrap_or_else(SandboxingFlagSet::empty)
728    }
729
730    pub(crate) fn set_throttled(&self, throttled: bool) {
731        if self.throttled.get() != throttled {
732            self.throttled.set(throttled);
733        }
734    }
735
736    /// Note a pending navigation.
737    /// This is used to ignore the async load event steps for
738    /// the initial blank document if those haven't run yet.
739    pub(crate) fn note_pending_navigation(&self) {
740        self.pending_navigation.set(true);
741    }
742
743    /// <https://html.spec.whatwg.org/multipage/#iframe-load-event-steps>
744    pub(crate) fn iframe_load_event_steps(&self, loaded_pipeline: PipelineId, cx: &mut JSContext) {
745        // TODO(#9592): assert that the load blocker is present at all times when we
746        //              can guarantee that it's created for the case of iframe.reload().
747        if Some(loaded_pipeline) != self.pending_pipeline_id.get() {
748            return;
749        }
750
751        // TODO 1. Assert: element's content navigable is not null.
752
753        // TODO 2-4 Mark resource timing.
754
755        // TODO 5 Set childDocument's iframe load in progress flag.
756
757        // Note: in the spec, these steps are either run synchronously as part of
758        // "If url matches about:blank and initialInsertion is true, then:"
759        // in `process the iframe attributes`,
760        // or asynchronously when navigation completes.
761        //
762        // In our current implementation,
763        // we arrive here always asynchronously in the following two cases:
764        // 1. as part of loading the initial blank document
765        //    created in `create_nested_browsing_context`
766        // 2. optionally, as part of loading a second document created as
767        //    as part of the first processing of the iframe attributes.
768        //
769        // To preserve the logic of the spec--firing the load event once--in the context of
770        // our current implementation, we must not fire the load event
771        // for the initial blank document if we know that a navigation is ongoing,
772        // which can be deducted from `pending_navigation` or the presence of an src.
773        //
774        // Additionally, to prevent a race condition with navigations,
775        // in all cases, skip the load event if there is a pending navigation.
776        // See #40348
777        //
778        // TODO: run these step synchronously as part of processing the iframe attributes.
779        let should_fire_event = if self.is_initial_blank_document() {
780            // If this is the initial blank doc:
781            // do not fire if there is a pending navigation,
782            // or if the iframe has an src.
783            !self.pending_navigation.get() &&
784                !self.upcast::<Element>().has_attribute(&local_name!("src"))
785        } else {
786            // If this is not the initial blank doc:
787            // do not fire if there is a pending navigation.
788            !self.pending_navigation.get()
789        };
790
791        // If we already fired a synchronous load event, we shouldn't fire another
792        // one in this method.
793        let should_fire_event =
794            !self.already_fired_synchronous_load_event.replace(false) && should_fire_event;
795        if should_fire_event {
796            self.run_iframe_load_event_steps(cx);
797        } else {
798            debug!(
799                "suppressing load event for iframe, loaded {:?}",
800                loaded_pipeline
801            );
802        }
803    }
804
805    /// <https://html.spec.whatwg.org/multipage/#iframe-load-event-steps>
806    pub(crate) fn run_iframe_load_event_steps(&self, cx: &mut JSContext) {
807        // TODO 1. Assert: element's content navigable is not null.
808
809        // Step 2. Let childDocument be element's content navigable's active document.
810        let child_document = self.GetContentDocument();
811
812        // Step 3. If childDocument has its mute iframe load flag set, then return.
813        // Step 5. Set childDocument's iframe load in progress flag.
814        if let Some(document) = child_document {
815            if document.mute_iframe_load_flag() {
816                let blocker = &self.load_blocker;
817                LoadBlocker::terminate(blocker, cx);
818                return;
819            }
820            document.set_iframe_load_in_progress(true);
821        }
822
823        // Step 4. If element's pending resource-timing start time is not null, then:
824        // TODO
825
826        // Step 6. Fire an event named load at element.
827        self.upcast::<EventTarget>().fire_event(cx, atom!("load"));
828
829        let blocker = &self.load_blocker;
830        LoadBlocker::terminate(blocker, cx);
831
832        // Step 7. Unset childDocument's iframe load in progress flag
833        if let Some(child_document) = self.GetContentDocument() {
834            child_document.set_iframe_load_in_progress(false);
835        }
836    }
837
838    /// Parse the `sandbox` attribute value given the [`Attr`]. This sets the `sandboxing_flag_set`
839    /// property or clears it is the value isn't specified. Notably, an unspecified sandboxing
840    /// attribute (no sandboxing) is different from an empty one (full sandboxing).
841    fn parse_sandbox_attribute(&self) {
842        let sandbox_value =
843            self.upcast::<Element>()
844                .with_attribute(&ns!(), &local_name!("sandbox"), |attribute| {
845                    let tokens: Vec<_> = attribute
846                        .value()
847                        .as_tokens()
848                        .iter()
849                        .map(|atom| atom.to_string().to_ascii_lowercase())
850                        .collect();
851                    parse_a_sandboxing_directive(&tokens)
852                });
853        self.sandboxing_flag_set.set(sandbox_value);
854    }
855
856    /// Step 4.2. of <https://html.spec.whatwg.org/multipage/#destroy-a-document-and-its-descendants>
857    pub(crate) fn destroy_document_and_its_descendants(&self, cx: &mut JSContext) {
858        let Some(pipeline_id) = self.pipeline_id.get() else {
859            return;
860        };
861        // Step 4.2. Destroy a document and its descendants given childNavigable's active document and incrementDestroyed.
862        if let Some(exited_document) = ScriptThread::find_document(pipeline_id) {
863            exited_document.destroy_document_and_its_descendants(cx);
864        }
865        self.destroy_nested_browsing_context();
866    }
867
868    /// <https://html.spec.whatwg.org/multipage/#destroy-a-child-navigable>
869    fn destroy_child_navigable(&self, cx: &mut JSContext) {
870        let blocker = &self.load_blocker;
871        LoadBlocker::terminate(blocker, cx);
872
873        // Step 1. Let navigable be container's content navigable.
874        let Some(browsing_context_id) = self.browsing_context_id() else {
875            // Step 2. If navigable is null, then return.
876            return;
877        };
878        // Store now so that we can destroy the context and delete the
879        // document later
880        let pipeline_id = self.pipeline_id.get();
881
882        // Step 3. Set container's content navigable to null.
883        //
884        // Resetting the pipeline_id to None is required here so that
885        // if this iframe is subsequently re-added to the document
886        // the load doesn't think that it's a navigation, but instead
887        // a new iframe. Without this, the constellation gets very
888        // confused.
889        self.destroy_nested_browsing_context();
890
891        // Step 4. Inform the navigation API about child navigable destruction given navigable.
892        // TODO
893
894        // Step 5. Destroy a document and its descendants given navigable's active document.
895        let (sender, receiver) =
896            ProfiledIpc::channel(self.global().time_profiler_chan().clone()).unwrap();
897        let msg = ScriptToConstellationMessage::RemoveIFrame(browsing_context_id, sender);
898        self.owner_window()
899            .as_global_scope()
900            .script_to_constellation_chan()
901            .send(msg)
902            .unwrap();
903        let _exited_pipeline_ids = receiver.recv().unwrap();
904        let Some(pipeline_id) = pipeline_id else {
905            return;
906        };
907        if let Some(exited_document) = ScriptThread::find_document(pipeline_id) {
908            exited_document.destroy_document_and_its_descendants(cx);
909        }
910
911        // Step 6. Let parentDocState be container's node navigable's active session history entry's document state.
912        // TODO
913
914        // Step 7. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
915        // TODO
916
917        // Step 8. Let traversable be container's node navigable's traversable navigable.
918        // TODO
919
920        // Step 9. Append the following session history traversal steps to traversable:
921        // TODO
922
923        // Step 10. Invoke WebDriver BiDi navigable destroyed with navigable.
924        // TODO
925    }
926}
927
928impl LayoutDom<'_, HTMLIFrameElement> {
929    #[inline]
930    pub(crate) fn pipeline_id(self) -> Option<PipelineId> {
931        (self.unsafe_get()).pipeline_id.get()
932    }
933
934    #[inline]
935    pub(crate) fn browsing_context_id(self) -> Option<BrowsingContextId> {
936        (self.unsafe_get()).browsing_context_id.get()
937    }
938
939    pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
940        self.upcast::<Element>()
941            .get_attr_for_layout(&ns!(), &local_name!("width"))
942            .map(AttrValue::as_dimension)
943            .cloned()
944            .unwrap_or(LengthOrPercentageOrAuto::Auto)
945    }
946
947    pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
948        self.upcast::<Element>()
949            .get_attr_for_layout(&ns!(), &local_name!("height"))
950            .map(AttrValue::as_dimension)
951            .cloned()
952            .unwrap_or(LengthOrPercentageOrAuto::Auto)
953    }
954}
955
956impl HTMLIFrameElementMethods<crate::DomTypeHolder> for HTMLIFrameElement {
957    // https://html.spec.whatwg.org/multipage/#dom-iframe-src
958    make_url_getter!(Src, "src");
959
960    // https://html.spec.whatwg.org/multipage/#dom-iframe-src
961    make_url_setter!(SetSrc, "src");
962
963    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-srcdoc>
964    fn Srcdoc(&self) -> TrustedHTMLOrString {
965        let element = self.upcast::<Element>();
966        element.get_trusted_html_attribute(&local_name!("srcdoc"))
967    }
968
969    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-srcdoc>
970    fn SetSrcdoc(&self, cx: &mut JSContext, value: TrustedHTMLOrString) -> Fallible<()> {
971        // Step 1: Let compliantString be the result of invoking the
972        // Get Trusted Type compliant string algorithm with TrustedHTML,
973        // this's relevant global object, the given value, "HTMLIFrameElement srcdoc", and "script".
974        let element = self.upcast::<Element>();
975        let value = TrustedHTML::get_trusted_type_compliant_string(
976            cx,
977            &element.owner_global(),
978            value,
979            "HTMLIFrameElement srcdoc",
980        )?;
981        // Step 2: Set an attribute value given this, srcdoc's local name, and compliantString.
982        element.set_attribute(
983            cx,
984            &local_name!("srcdoc"),
985            AttrValue::String(value.str().to_owned()),
986        );
987        Ok(())
988    }
989
990    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-sandbox>
991    ///
992    /// The supported tokens for sandbox's DOMTokenList are the allowed values defined in the
993    /// sandbox attribute and supported by the user agent. These range of possible values is
994    /// defined here: <https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox>
995    fn Sandbox(&self, cx: &mut JSContext) -> DomRoot<DOMTokenList> {
996        self.sandbox.or_init(|| {
997            DOMTokenList::new(
998                cx,
999                self.upcast::<Element>(),
1000                &local_name!("sandbox"),
1001                Some(vec![
1002                    Atom::from("allow-downloads"),
1003                    Atom::from("allow-forms"),
1004                    Atom::from("allow-modals"),
1005                    Atom::from("allow-orientation-lock"),
1006                    Atom::from("allow-pointer-lock"),
1007                    Atom::from("allow-popups"),
1008                    Atom::from("allow-popups-to-escape-sandbox"),
1009                    Atom::from("allow-presentation"),
1010                    Atom::from("allow-same-origin"),
1011                    Atom::from("allow-scripts"),
1012                    Atom::from("allow-top-navigation"),
1013                    Atom::from("allow-top-navigation-by-user-activation"),
1014                    Atom::from("allow-top-navigation-to-custom-protocols"),
1015                ]),
1016            )
1017        })
1018    }
1019
1020    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-contentwindow>
1021    fn GetContentWindow(&self) -> Option<DomRoot<WindowProxy>> {
1022        self.browsing_context_id
1023            .get()
1024            .and_then(|id| self.script_window_proxies.find_window_proxy(id))
1025    }
1026
1027    /// <https://html.spec.whatwg.org/multipage/#concept-bcc-content-document>
1028    fn GetContentDocument(&self) -> Option<DomRoot<Document>> {
1029        // Step 1. If container's content navigable is null, then return null.
1030        let pipeline_id = self.pipeline_id.get()?;
1031
1032        // Step 2. Let document be container's content navigable's active document.
1033        // Note that this lookup will fail if the document is dissimilar-origin,
1034        // so we should return None in that case.
1035        let document = ScriptThread::find_document(pipeline_id)?;
1036        // Step 3. If document's origin and container's node document's origin are not same origin-domain, then return null.
1037        if !self
1038            .owner_document()
1039            .origin()
1040            .same_origin_domain(&document.origin())
1041        {
1042            return None;
1043        }
1044        // Step 4. Return document.
1045        Some(document)
1046    }
1047
1048    /// <https://html.spec.whatwg.org/multipage/#attr-iframe-referrerpolicy>
1049    fn ReferrerPolicy(&self) -> DOMString {
1050        reflect_referrer_policy_attribute(self.upcast::<Element>())
1051    }
1052
1053    // https://html.spec.whatwg.org/multipage/#attr-iframe-referrerpolicy
1054    make_setter!(SetReferrerPolicy, "referrerpolicy");
1055
1056    // https://html.spec.whatwg.org/multipage/#attr-iframe-allowfullscreen
1057    make_bool_getter!(AllowFullscreen, "allowfullscreen");
1058    // https://html.spec.whatwg.org/multipage/#attr-iframe-allowfullscreen
1059    make_bool_setter!(SetAllowFullscreen, "allowfullscreen");
1060
1061    // <https://html.spec.whatwg.org/multipage/#dom-dim-width>
1062    make_getter!(Width, "width");
1063    // <https://html.spec.whatwg.org/multipage/#dom-dim-width>
1064    make_dimension_setter!(SetWidth, "width");
1065
1066    // <https://html.spec.whatwg.org/multipage/#dom-dim-height>
1067    make_getter!(Height, "height");
1068    // <https://html.spec.whatwg.org/multipage/#dom-dim-height>
1069    make_dimension_setter!(SetHeight, "height");
1070
1071    // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:attr-iframe-frameborder
1072    make_getter!(FrameBorder, "frameborder");
1073    // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:attr-iframe-frameborder
1074    make_setter!(SetFrameBorder, "frameborder");
1075
1076    // https://html.spec.whatwg.org/multipage/#dom-iframe-name
1077    // A child browsing context checks the name of its iframe only at the time
1078    // it is created; subsequent name sets have no special effect.
1079    make_atomic_setter!(SetName, "name");
1080
1081    // https://html.spec.whatwg.org/multipage/#dom-iframe-name
1082    // This is specified as reflecting the name content attribute of the
1083    // element, not the name of the child browsing context.
1084    make_getter!(Name, "name");
1085
1086    // https://html.spec.whatwg.org/multipage/#attr-iframe-loading
1087    // > The loading attribute is a lazy loading attribute. Its purpose is to indicate the policy for loading iframe elements that are outside the viewport.
1088    make_enumerated_getter!(
1089        Loading,
1090        "loading",
1091        "lazy" | "eager",
1092        // https://html.spec.whatwg.org/multipage/#lazy-loading-attribute
1093        // > The attribute's missing value default and invalid value default are both the Eager state.
1094        missing => "eager",
1095        invalid => "eager"
1096    );
1097
1098    // https://html.spec.whatwg.org/multipage/#attr-iframe-loading
1099    make_setter!(SetLoading, "loading");
1100
1101    // https://html.spec.whatwg.org/multipage/#dom-iframe-longdesc
1102    make_url_getter!(LongDesc, "longdesc");
1103
1104    // https://html.spec.whatwg.org/multipage/#dom-iframe-longdesc
1105    make_url_setter!(SetLongDesc, "longdesc");
1106}
1107
1108impl VirtualMethods for HTMLIFrameElement {
1109    fn super_type(&self) -> Option<&dyn VirtualMethods> {
1110        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
1111    }
1112
1113    fn attribute_mutated(
1114        &self,
1115        cx: &mut JSContext,
1116        attr: AttrRef<'_>,
1117        mutation: AttributeMutation,
1118    ) {
1119        self.super_type()
1120            .unwrap()
1121            .attribute_mutated(cx, attr, mutation);
1122        match *attr.local_name() {
1123            // From <https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox>:
1124            //
1125            // > When an iframe element's sandbox attribute is set or changed while
1126            // > it has a non-null content navigable, the user agent must parse the
1127            // > sandboxing directive given the attribute's value and the iframe
1128            // > element's iframe sandboxing flag set.
1129            //
1130            // > When an iframe element's sandbox attribute is removed while it has
1131            // > a non-null content navigable, the user agent must empty the iframe
1132            // > element's iframe sandboxing flag set.
1133            local_name!("sandbox") if self.browsing_context_id.get().is_some() => {
1134                self.parse_sandbox_attribute();
1135            },
1136            local_name!("srcdoc") => {
1137                // https://html.spec.whatwg.org/multipage/#the-iframe-element:the-iframe-element-9
1138                // "Whenever an iframe element with a non-null nested browsing context has its
1139                // srcdoc attribute set, changed, or removed, the user agent must process the
1140                // iframe attributes."
1141                // but we can't check that directly, since the child browsing context
1142                // may be in a different script thread. Instead, we check to see if the parent
1143                // is in a document tree and has a browsing context, which is what causes
1144                // the child browsing context to be created.
1145
1146                // trigger the processing of iframe attributes whenever "srcdoc" attribute is set, changed or removed
1147                if self.upcast::<Node>().is_connected_with_browsing_context() {
1148                    debug!("iframe srcdoc modified while in browsing context.");
1149                    self.process_the_iframe_attributes(ProcessingMode::NotFirstTime, cx);
1150                }
1151            },
1152            local_name!("src") => {
1153                // https://html.spec.whatwg.org/multipage/#the-iframe-element
1154                // "Similarly, whenever an iframe element with a non-null nested browsing context
1155                // but with no srcdoc attribute specified has its src attribute set, changed, or removed,
1156                // the user agent must process the iframe attributes,"
1157                // but we can't check that directly, since the child browsing context
1158                // may be in a different script thread. Instead, we check to see if the parent
1159                // is in a document tree and has a browsing context, which is what causes
1160                // the child browsing context to be created.
1161                if self.upcast::<Node>().is_connected_with_browsing_context() {
1162                    debug!("iframe src set while in browsing context.");
1163                    self.process_the_iframe_attributes(ProcessingMode::NotFirstTime, cx);
1164                }
1165            },
1166            local_name!("loading") => {
1167                // https://html.spec.whatwg.org/multipage/#attr-iframe-loading
1168                // > When the loading attribute's state is changed to the Eager state, the user agent must run these steps:
1169                if !mutation.is_removal() && &**attr.value() == "lazy" {
1170                    return;
1171                }
1172
1173                // Step 1. Let resumptionSteps be the iframe element's lazy load resumption steps.
1174                // Step 3. Set the iframe's lazy load resumption steps to null.
1175                let previous_resumption_steps = self
1176                    .lazy_load_resumption_steps
1177                    .replace(LazyLoadResumptionSteps::None);
1178                match previous_resumption_steps {
1179                    // Step 2. If resumptionSteps is null, then return.
1180                    LazyLoadResumptionSteps::None => (),
1181                    LazyLoadResumptionSteps::SrcDoc => {
1182                        // Step 4. Invoke resumptionSteps.
1183                        self.navigate_to_the_srcdoc_resource(ProcessingMode::NotFirstTime, cx);
1184                    },
1185                }
1186            },
1187            _ => {},
1188        }
1189    }
1190
1191    fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
1192        match attr.local_name() {
1193            &local_name!("width") | &local_name!("height") => true,
1194            _ => self
1195                .super_type()
1196                .unwrap()
1197                .attribute_affects_presentational_hints(attr),
1198        }
1199    }
1200
1201    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
1202        match *name {
1203            local_name!("sandbox") => AttrValue::from_serialized_tokenlist(value.into()),
1204            local_name!("width") => AttrValue::from_dimension(value.into()),
1205            local_name!("height") => AttrValue::from_dimension(value.into()),
1206            _ => self
1207                .super_type()
1208                .unwrap()
1209                .parse_plain_attribute(name, value),
1210        }
1211    }
1212
1213    /// <https://html.spec.whatwg.org/multipage/#the-iframe-element:html-element-post-connection-steps>
1214    fn post_connection_steps(&self, cx: &mut JSContext) {
1215        if let Some(s) = self.super_type() {
1216            s.post_connection_steps(cx);
1217        }
1218
1219        // This isn't mentioned any longer in the specification, but still seems important. This is
1220        // likely due to the fact that we have deviated a great deal with it comes to navigables
1221        // and browsing contexts.
1222        if !self.upcast::<Node>().is_connected_with_browsing_context() {
1223            return;
1224        }
1225
1226        debug!("<iframe> running post connection steps");
1227
1228        // Step 1: If insertedNode has a sandbox attribute, then parse the sandboxing directive
1229        // given the attribute's value and insertedNode's iframe sandboxing flag set.
1230        self.parse_sandbox_attribute();
1231
1232        // Step 2. Create a new child navigable for insertedNode.
1233        self.create_nested_browsing_context(cx);
1234
1235        // Step 3. Process the iframe attributes for insertedNode, with initialInsertion set to true.
1236        self.process_the_iframe_attributes(ProcessingMode::FirstTime, cx);
1237    }
1238
1239    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
1240        if let Some(super_type) = self.super_type() {
1241            super_type.bind_to_tree(cx, context);
1242        }
1243
1244        self.owner_document().iframes_mut().add(self);
1245    }
1246
1247    /// <https://html.spec.whatwg.org/multipage/#the-iframe-element:html-element-removing-steps>
1248    fn unbind_from_tree(&self, cx: &mut JSContext, context: &UnbindContext) {
1249        if let Some(super_type) = self.super_type() {
1250            super_type.unbind_from_tree(cx, context);
1251        }
1252
1253        // The iframe HTML element removing steps, given removedNode, are to destroy a child
1254        // navigable given removedNode
1255        self.destroy_child_navigable(cx);
1256
1257        self.owner_document().iframes_mut().remove(self);
1258    }
1259}
1260
1261/// IframeContext is a wrapper around [`HTMLIFrameElement`] that implements the [`ResourceTimingListener`] trait.
1262/// Note: this implementation of `resource_timing_global` returns the parent document's global scope, not the iframe's global scope.
1263pub(crate) struct IframeContext<'a> {
1264    // The iframe element that this context is associated with.
1265    element: &'a HTMLIFrameElement,
1266    // The URL of the iframe document.
1267    url: ServoUrl,
1268}
1269
1270impl<'a> IframeContext<'a> {
1271    /// Creates a new IframeContext from a reference to an HTMLIFrameElement.
1272    pub fn new(element: &'a HTMLIFrameElement) -> Self {
1273        Self {
1274            element,
1275            url: element
1276                .shared_attribute_processing_steps_for_iframe_and_frame_elements(
1277                    ProcessingMode::NotFirstTime,
1278                )
1279                .expect("Must always have a URL when navigating"),
1280        }
1281    }
1282}
1283
1284impl<'a> ResourceTimingListener for IframeContext<'a> {
1285    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
1286        (
1287            InitiatorType::LocalName("iframe".to_string()),
1288            self.url.clone(),
1289        )
1290    }
1291
1292    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
1293        self.element.upcast::<Node>().owner_doc().global()
1294    }
1295}
1296
1297fn snapshot_self(iframe: &HTMLIFrameElement) -> TargetSnapshotParams {
1298    let child_navigable = iframe.GetContentWindow();
1299    TargetSnapshotParams {
1300        sandboxing_flags: determine_creation_sandboxing_flags(
1301            child_navigable.as_deref(),
1302            Some(iframe.upcast()),
1303        ),
1304        iframe_element_referrer_policy: determine_iframe_element_referrer_policy(Some(
1305            iframe.upcast(),
1306        )),
1307    }
1308}