Skip to main content

script/
stylesheet_loader.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::io::{Read, Seek, Write};
6
7use crossbeam_channel::Sender;
8use cssparser::SourceLocation;
9use encoding_rs::UTF_8;
10use js::context::JSContext;
11use net_traits::mime_classifier::MimeClassifier;
12use net_traits::request::{CorsSettings, Destination, RequestId};
13use net_traits::{
14    FetchMetadata, FilteredMetadata, LoadContext, Metadata, NetworkError, ReferrerPolicy,
15    ResourceFetchTiming,
16};
17use servo_arc::Arc;
18use servo_base::id::PipelineId;
19use servo_config::pref;
20use servo_url::ServoUrl;
21use style::context::QuirksMode;
22use style::global_style_data::STYLE_THREAD_POOL;
23use style::media_queries::MediaList;
24use style::shared_lock::{Locked, SharedRwLock};
25use style::stylesheets::import_rule::{ImportLayer, ImportSheet, ImportSupportsCondition};
26use style::stylesheets::{
27    ImportRule, Origin, Stylesheet, StylesheetLoader as StyleStylesheetLoader, UrlExtraData,
28};
29use style::values::CssUrl;
30
31use crate::document_loader::LoadType;
32use crate::dom::bindings::inheritance::Castable;
33use crate::dom::bindings::refcounted::Trusted;
34use crate::dom::bindings::reflector::DomGlobal;
35use crate::dom::bindings::root::DomRoot;
36use crate::dom::csp::{GlobalCspReporting, Violation};
37use crate::dom::document::Document;
38use crate::dom::element::Element;
39use crate::dom::eventtarget::EventTarget;
40use crate::dom::globalscope::GlobalScope;
41use crate::dom::html::htmlelement::HTMLElement;
42use crate::dom::html::htmllinkelement::{HTMLLinkElement, RequestGenerationId};
43use crate::dom::node::NodeTraits;
44use crate::dom::performance::performanceresourcetiming::InitiatorType;
45use crate::dom::shadowroot::ShadowRoot;
46use crate::dom::window::CSSErrorReporter;
47use crate::fetch::{RequestWithGlobalScope, create_a_potential_cors_request};
48use crate::messaging::{CommonScriptMsg, MainThreadScriptMsg};
49use crate::network_listener::{self, FetchResponseListener, ResourceTimingListener};
50use crate::script_runtime::ScriptThreadEventCategory;
51use crate::task_source::TaskSourceName;
52use crate::unminify::{
53    BeautifyFileType, create_output_file, create_temp_files, execute_js_beautify,
54};
55
56pub(crate) trait StylesheetOwner {
57    /// Returns whether this element was inserted by the parser (i.e., it should
58    /// trigger a document-load-blocking load).
59    fn parser_inserted(&self) -> bool;
60
61    /// <https://html.spec.whatwg.org/multipage/#potentially-render-blocking>
62    fn potentially_render_blocking(&self) -> bool;
63
64    /// Which referrer policy should loads triggered by this owner follow
65    fn referrer_policy(&self, cx: &mut JSContext) -> ReferrerPolicy;
66
67    /// Notes that a new load is pending to finish.
68    fn increment_pending_loads_count(&self);
69
70    /// Returns None if there are still pending loads, or whether any load has
71    /// failed since the loads started.
72    fn load_finished(&self, successful: bool) -> Option<bool>;
73
74    /// Sets origin_clean flag.
75    fn set_origin_clean(&self, origin_clean: bool);
76}
77
78pub(crate) enum StylesheetContextSource {
79    LinkElement,
80    Import(Arc<Locked<ImportRule>>),
81}
82
83/// The context required for asynchronously loading an external stylesheet.
84struct StylesheetContext {
85    /// The element that initiated the request.
86    element: Trusted<HTMLElement>,
87    source: StylesheetContextSource,
88    media: Arc<Locked<MediaList>>,
89    url: ServoUrl,
90    metadata: Option<Metadata>,
91    /// The response body received to date.
92    data: Vec<u8>,
93    /// The node document for elem when the load was initiated.
94    document: Trusted<Document>,
95    shadow_root: Option<Trusted<ShadowRoot>>,
96    origin_clean: bool,
97    /// A token which must match the generation id of the `HTMLLinkElement` for it to load the stylesheet.
98    /// This is ignored for `HTMLStyleElement` and imports.
99    request_generation_id: Option<RequestGenerationId>,
100    /// <https://html.spec.whatwg.org/multipage/#contributes-a-script-blocking-style-sheet>
101    is_script_blocking: bool,
102    /// <https://html.spec.whatwg.org/multipage/#render-blocking>
103    is_render_blocking: bool,
104}
105
106impl StylesheetContext {
107    fn unminify_css(&mut self, file_url: ServoUrl) {
108        let Some(unminified_dir) = self.document.root().window().unminified_css_dir() else {
109            return;
110        };
111
112        let mut style_content = std::mem::take(&mut self.data);
113        if let Some((input, mut output)) = create_temp_files() &&
114            execute_js_beautify(
115                input.path(),
116                output.try_clone().unwrap(),
117                BeautifyFileType::Css,
118            )
119        {
120            output.seek(std::io::SeekFrom::Start(0)).unwrap();
121            output.read_to_end(&mut style_content).unwrap();
122        }
123        match create_output_file(unminified_dir, &file_url, None) {
124            Ok(mut file) => {
125                file.write_all(&style_content).unwrap();
126            },
127            Err(why) => {
128                log::warn!("Could not store script {:?}", why);
129            },
130        }
131
132        self.data = style_content;
133    }
134
135    fn empty_stylesheet(&self, document: &Document) -> Arc<Stylesheet> {
136        let shared_lock = document.style_shared_author_lock().clone();
137        let quirks_mode = document.quirks_mode();
138
139        Arc::new(Stylesheet::from_bytes(
140            &[],
141            UrlExtraData(self.url.get_arc()),
142            None,
143            None,
144            Origin::Author,
145            self.media.clone(),
146            shared_lock,
147            None,
148            None,
149            quirks_mode,
150        ))
151    }
152
153    fn parse(
154        &self,
155        quirks_mode: QuirksMode,
156        shared_lock: SharedRwLock,
157        css_error_reporter: &CSSErrorReporter,
158        loader: ElementStylesheetLoader<'_>,
159    ) -> Arc<Stylesheet> {
160        let metadata = self
161            .metadata
162            .as_ref()
163            .expect("Should never call parse without metadata.");
164
165        let _span = profile_traits::trace_span!("ParseStylesheet").entered();
166        Arc::new(Stylesheet::from_bytes(
167            &self.data,
168            UrlExtraData(metadata.final_url.get_arc()),
169            metadata.charset.as_deref(),
170            // The CSS environment encoding is the result of running the following steps: [CSSSYNTAX]
171            // If el has a charset attribute, get an encoding from that attribute's value. If that succeeds, return the resulting encoding. [ENCODING]
172            // Otherwise, return the document's character encoding. [DOM]
173            //
174            // TODO: Need to implement encoding http://dev.w3.org/csswg/css-syntax/#environment-encoding
175            Some(UTF_8),
176            Origin::Author,
177            self.media.clone(),
178            shared_lock,
179            Some(&loader),
180            Some(css_error_reporter),
181            quirks_mode,
182        ))
183    }
184
185    fn contributes_to_the_styling_processing_model(&self, element: &HTMLElement) -> bool {
186        if !element.upcast::<Element>().is_connected() {
187            return false;
188        }
189
190        // Whether or not this `StylesheetContext` is for a `<link>` element that comes
191        // from a previous generation. This prevents processing of earlier stylsheet URLs
192        // when the URL has changed.
193        //
194        // TODO(mrobinson): Shouldn't we also exit early if this is an import that was originally
195        // imported from a `<link>` element that has advanced a generation as well?
196        if !matches!(&self.source, StylesheetContextSource::LinkElement) {
197            return true;
198        }
199        let link = element.downcast::<HTMLLinkElement>().unwrap();
200        self.request_generation_id
201            .is_none_or(|generation| generation == link.get_request_generation_id())
202    }
203
204    /// <https://html.spec.whatwg.org/multipage/#contributes-a-script-blocking-style-sheet>
205    fn contributes_a_script_blocking_style_sheet(
206        &self,
207        element: &HTMLElement,
208        owner: &dyn StylesheetOwner,
209        document: &Document,
210    ) -> bool {
211        // el was created by that Document's parser.
212        owner.parser_inserted()
213        // el is either a style element or a link element that was an external resource link that
214        // contributes to the styling processing model when the el was created by the parser.
215        && element.downcast::<HTMLLinkElement>().is_none_or(|link|
216            self.contributes_to_the_styling_processing_model(element)
217            // el's style sheet was enabled when the element was created by the parser.
218            && !link.is_effectively_disabled()
219        )
220        // el's media attribute's value matches the environment.
221        && element.media_attribute_matches_media_environment()
222        // The last time the event loop reached step 1, el's root was that Document.
223        && *element.owner_document() == *document
224        // The user agent hasn't given up on loading that particular style sheet yet.
225        // A user agent may give up on loading a style sheet at any time.
226        //
227        // This might happen when we time out a resource, but that happens in `fetch` instead
228    }
229
230    fn decrement_blockers_and_finish_load(
231        self,
232        document: &Document,
233        cx: &mut js::context::JSContext,
234    ) {
235        if self.is_script_blocking {
236            document.decrement_script_blocking_stylesheet_count();
237        }
238
239        if self.is_render_blocking {
240            document.decrement_render_blocking_element_count();
241        }
242
243        document.finish_load(LoadType::Stylesheet(self.url), cx);
244    }
245
246    fn do_post_parse_tasks(
247        self,
248        success: bool,
249        stylesheet: Arc<Stylesheet>,
250        cx: &mut js::context::JSContext,
251    ) {
252        let element = self.element.root();
253        let document = self.document.root();
254        let owner = element
255            .upcast::<Element>()
256            .as_stylesheet_owner()
257            .expect("Stylesheet not loaded by <style> or <link> element!");
258
259        match &self.source {
260            // https://html.spec.whatwg.org/multipage/#link-type-stylesheet%3Aprocess-the-linked-resource
261            StylesheetContextSource::LinkElement => {
262                let link = element
263                    .downcast::<HTMLLinkElement>()
264                    .expect("Should be HTMLinkElement due to StylesheetContextSource");
265                // For failed requests, we should bail out if it is from a previous generation.
266                // Since we can reissue another failed request, which resets the pending load counter
267                // in a link element.
268                if self
269                    .request_generation_id
270                    .is_some_and(|generation| generation != link.get_request_generation_id())
271                {
272                    self.decrement_blockers_and_finish_load(&document, cx);
273                    return;
274                }
275                // https://html.spec.whatwg.org/multipage/#link-type-stylesheet
276                // > When the disabled attribute of a link element with a stylesheet keyword is set,
277                // > disable the associated CSS style sheet.
278                if link.is_effectively_disabled() {
279                    stylesheet.set_disabled(true);
280                }
281                // Step 3. If el has an associated CSS style sheet, remove the CSS style sheet.
282                // Step 4. If success is true, then:
283                // Step 4.1. Create a CSS style sheet with the following properties:
284                //
285                // Note that even in the failure case, we should create an empty stylesheet.
286                // That's why `set_stylesheet` also removes the previous stylesheet
287                link.set_stylesheet(stylesheet);
288            },
289            StylesheetContextSource::Import(import_rule) => {
290                // Construct a new WebFontDocumentContext for the stylesheet
291                let window = element.owner_window();
292                let document_context = window.web_font_context();
293
294                // Layout knows about this stylesheet, because Stylo added it to the Stylist,
295                // but Layout doesn't know about any new web fonts that it contains.
296                document.load_web_fonts_from_stylesheet(&stylesheet, &document_context);
297
298                let mut guard = document.style_shared_author_lock().write();
299                import_rule.write_with(&mut guard).stylesheet = ImportSheet::Sheet(stylesheet);
300            },
301        }
302
303        if let Some(ref shadow_root) = self.shadow_root {
304            shadow_root.root().invalidate_stylesheets();
305        } else {
306            document.invalidate_stylesheets();
307        }
308        owner.set_origin_clean(self.origin_clean);
309
310        // Remaining steps are a combination of
311        // https://html.spec.whatwg.org/multipage/#link-type-stylesheet%3Aprocess-the-linked-resource
312        // and https://html.spec.whatwg.org/multipage/#the-style-element%3Acritical-subresources
313
314        // Step 4.2. Fire an event named load at el.
315        // Step 5. Otherwise, fire an event named error at el.
316        if let Some(any_failed) = owner.load_finished(success) {
317            // Only fire an event if we have no more pending events
318            // (in which case `owner.load_finished` would return None)
319            let event = match any_failed {
320                true => atom!("error"),
321                false => atom!("load"),
322            };
323            element.upcast::<EventTarget>().fire_event(cx, event);
324        }
325        // Regardless if there are other pending events, we need to unblock
326        // rendering for this particular request and signal that the load has finished
327
328        // Step 6. If el contributes a script-blocking style sheet, then:
329        // Step 7. Unblock rendering on el.
330        self.decrement_blockers_and_finish_load(&document, cx);
331    }
332}
333
334impl FetchResponseListener for StylesheetContext {
335    fn process_request_body(&mut self, _: RequestId) {}
336
337    fn process_response(
338        &mut self,
339        _: &mut js::context::JSContext,
340        _: RequestId,
341        metadata: Result<FetchMetadata, NetworkError>,
342    ) {
343        if let Ok(FetchMetadata::Filtered {
344            filtered: FilteredMetadata::Opaque | FilteredMetadata::OpaqueRedirect(_),
345            ..
346        }) = metadata
347        {
348            self.origin_clean = false;
349        }
350
351        self.metadata = metadata.ok().map(|m| match m {
352            FetchMetadata::Unfiltered(m) => m,
353            FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
354        });
355    }
356
357    fn process_response_chunk(
358        &mut self,
359        _: &mut js::context::JSContext,
360        _: RequestId,
361        mut payload: Vec<u8>,
362    ) {
363        self.data.append(&mut payload);
364    }
365
366    fn process_response_eof(
367        mut self,
368        cx: &mut js::context::JSContext,
369        _: RequestId,
370        status: Result<(), NetworkError>,
371        timing: ResourceFetchTiming,
372    ) {
373        network_listener::submit_timing(cx, &self, &status, &timing);
374
375        let document = self.document.root();
376        let Some(metadata) = self.metadata.as_ref() else {
377            let empty_stylesheet = self.empty_stylesheet(&document);
378            self.do_post_parse_tasks(false, empty_stylesheet, cx);
379            return;
380        };
381
382        let element = self.element.root();
383
384        // https://html.spec.whatwg.org/multipage/#link-type-stylesheet:process-the-linked-resource
385        if element.downcast::<HTMLLinkElement>().is_some() {
386            // Step 1. If the resource's Content-Type metadata is not text/css, then set success to false.
387            let is_css = MimeClassifier::is_css(
388                &metadata.resource_content_type_metadata(LoadContext::Style, &self.data),
389            ) || (
390                // From <https://html.spec.whatwg.org/multipage/#link-type-stylesheet>:
391                // > Quirk: If the document has been set to quirks mode, has the same origin as
392                // > the URL of the external resource, and the Content-Type metadata of the
393                // > external resource is not a supported style sheet type, the user agent must
394                // > instead assume it to be text/css.
395                document.quirks_mode() == QuirksMode::Quirks &&
396                    document.origin().immutable().clone() == metadata.final_url.origin()
397            );
398
399            if !is_css {
400                let empty_stylesheet = self.empty_stylesheet(&document);
401                self.do_post_parse_tasks(false, empty_stylesheet, cx);
402                return;
403            }
404
405            // Step 2. If el no longer creates an external resource link that contributes to the styling processing model,
406            // or if, since the resource in question was fetched, it has become appropriate to fetch it again, then:
407            if !self.contributes_to_the_styling_processing_model(&element) {
408                // Step 2.1. Remove el from el's node document's script-blocking style sheet set.
409                self.decrement_blockers_and_finish_load(&document, cx);
410                // Step 2.2. Return.
411                return;
412            }
413        }
414
415        if metadata.status != http::StatusCode::OK {
416            let empty_stylesheet = self.empty_stylesheet(&document);
417            self.do_post_parse_tasks(false, empty_stylesheet, cx);
418            return;
419        }
420
421        self.unminify_css(metadata.final_url.clone());
422
423        let loader = if pref!(dom_parallel_css_parsing_enabled) {
424            ElementStylesheetLoader::Asynchronous(AsynchronousStylesheetLoader::new(&element))
425        } else {
426            ElementStylesheetLoader::Synchronous { element: &element }
427        };
428        loader.parse(self, &element, &document, cx);
429    }
430
431    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
432        let global = &self.resource_timing_global();
433        global.report_csp_violations(violations, None, None);
434    }
435}
436
437impl ResourceTimingListener for StylesheetContext {
438    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
439        let initiator_type = InitiatorType::LocalName(
440            self.element
441                .root()
442                .upcast::<Element>()
443                .local_name()
444                .to_string(),
445        );
446        (initiator_type, self.url.clone())
447    }
448
449    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
450        self.element.root().owner_document().global()
451    }
452}
453
454pub(crate) enum ElementStylesheetLoader<'a> {
455    Synchronous { element: &'a HTMLElement },
456    Asynchronous(AsynchronousStylesheetLoader),
457}
458
459impl<'a> ElementStylesheetLoader<'a> {
460    pub(crate) fn new(element: &'a HTMLElement) -> Self {
461        ElementStylesheetLoader::Synchronous { element }
462    }
463}
464
465impl ElementStylesheetLoader<'_> {
466    pub(crate) fn load_with_element(
467        cx: &mut JSContext,
468        element: &HTMLElement,
469        source: StylesheetContextSource,
470        media: Arc<Locked<MediaList>>,
471        url: ServoUrl,
472        cors_setting: Option<CorsSettings>,
473        integrity_metadata: String,
474    ) {
475        let document = element.owner_document();
476        let shadow_root = element
477            .containing_shadow_root()
478            .map(|shadow_root| Trusted::new(&*shadow_root));
479        let generation = element
480            .downcast::<HTMLLinkElement>()
481            .map(HTMLLinkElement::get_request_generation_id);
482        let mut context = StylesheetContext {
483            element: Trusted::new(element),
484            source,
485            media,
486            url: url.clone(),
487            metadata: None,
488            data: vec![],
489            document: Trusted::new(&*document),
490            shadow_root,
491            origin_clean: true,
492            request_generation_id: generation,
493            is_script_blocking: false,
494            is_render_blocking: false,
495        };
496
497        let owner = element
498            .upcast::<Element>()
499            .as_stylesheet_owner()
500            .expect("Stylesheet not loaded by <style> or <link> element!");
501        let referrer_policy = owner.referrer_policy(cx);
502        owner.increment_pending_loads_count();
503
504        // Final steps of https://html.spec.whatwg.org/multipage/#update-a-style-block
505        // and part of https://html.spec.whatwg.org/multipage/#link-type-stylesheet:linked-resource-fetch-setup-steps
506
507        // If element contributes a script-blocking style sheet, append element to its node document's script-blocking style sheet set.
508        context.is_script_blocking =
509            context.contributes_a_script_blocking_style_sheet(element, owner, &document);
510        if context.is_script_blocking {
511            document.increment_script_blocking_stylesheet_count();
512        }
513
514        // If element's media attribute's value matches the environment and
515        // element is potentially render-blocking, then block rendering on element.
516        context.is_render_blocking = element.media_attribute_matches_media_environment() &&
517            owner.potentially_render_blocking() &&
518            document.allows_adding_render_blocking_elements();
519        if context.is_render_blocking {
520            document.increment_render_blocking_element_count();
521        }
522
523        // https://html.spec.whatwg.org/multipage/#default-fetch-and-process-the-linked-resource
524        let global = element.global();
525        let request = create_a_potential_cors_request(
526            Some(document.webview_id()),
527            url.clone(),
528            Destination::Style,
529            cors_setting,
530            None,
531            global.get_referrer(),
532        )
533        .with_global_scope(&global)
534        .referrer_policy(referrer_policy)
535        .integrity_metadata(integrity_metadata);
536
537        document.fetch(LoadType::Stylesheet(url), request, context);
538    }
539
540    fn parse(
541        self,
542        listener: StylesheetContext,
543        element: &HTMLElement,
544        document: &Document,
545        cx: &mut js::context::JSContext,
546    ) {
547        let shared_lock = document.style_shared_author_lock().clone();
548        let quirks_mode = document.quirks_mode();
549        let window = element.owner_window();
550
551        match self {
552            ElementStylesheetLoader::Synchronous { .. } => {
553                let stylesheet =
554                    listener.parse(quirks_mode, shared_lock, window.css_error_reporter(), self);
555                listener.do_post_parse_tasks(true, stylesheet, cx);
556            },
557            ElementStylesheetLoader::Asynchronous(asynchronous_loader) => {
558                let css_error_reporter = window.css_error_reporter().clone();
559
560                let parse_stylesheet = move || {
561                    let pipeline_id = asynchronous_loader.pipeline_id;
562                    let main_thread_sender = asynchronous_loader.main_thread_sender.clone();
563                    let loader = ElementStylesheetLoader::Asynchronous(asynchronous_loader);
564                    let stylesheet =
565                        listener.parse(quirks_mode, shared_lock, &css_error_reporter, loader);
566
567                    let task = task!(finish_parsing_of_stylesheet_on_main_thread: move |cx| {
568                        listener.do_post_parse_tasks(true, stylesheet, cx);
569                    });
570                    let _ = main_thread_sender.send(MainThreadScriptMsg::Common(
571                        CommonScriptMsg::Task(
572                            ScriptThreadEventCategory::StylesheetLoad,
573                            Box::new(task),
574                            Some(pipeline_id),
575                            TaskSourceName::Networking,
576                        ),
577                    ));
578                };
579
580                let thread_pool = STYLE_THREAD_POOL.pool();
581                if let Some(thread_pool) = thread_pool.as_ref() {
582                    thread_pool.spawn(parse_stylesheet);
583                } else {
584                    parse_stylesheet();
585                }
586            },
587        };
588    }
589}
590
591impl StyleStylesheetLoader for ElementStylesheetLoader<'_> {
592    /// Request a stylesheet after parsing a given `@import` rule, and return
593    /// the constructed `@import` rule.
594    fn request_stylesheet(
595        &self,
596        url: CssUrl,
597        source_location: SourceLocation,
598        lock: &SharedRwLock,
599        media: Arc<Locked<MediaList>>,
600        supports: Option<ImportSupportsCondition>,
601        layer: ImportLayer,
602    ) -> Arc<Locked<ImportRule>> {
603        // Ensure the supports conditions for this @import are true, if not, refuse to load
604        if supports.as_ref().is_some_and(|s| !s.enabled) {
605            return Arc::new(lock.wrap(ImportRule {
606                url,
607                stylesheet: ImportSheet::new_refused(),
608                supports,
609                layer,
610                source_location,
611            }));
612        }
613
614        let resolved_url = match url.url().cloned() {
615            Some(url) => url,
616            None => {
617                return Arc::new(lock.wrap(ImportRule {
618                    url,
619                    stylesheet: ImportSheet::new_refused(),
620                    supports,
621                    layer,
622                    source_location,
623                }));
624            },
625        };
626
627        let import_rule = Arc::new(lock.wrap(ImportRule {
628            url,
629            stylesheet: ImportSheet::new_pending(),
630            supports,
631            layer,
632            source_location,
633        }));
634
635        // TODO (mrnayak) : Whether we should use the original loader's CORS
636        // setting? Fix this when spec has more details.
637        let source = StylesheetContextSource::Import(import_rule.clone());
638
639        match self {
640            ElementStylesheetLoader::Synchronous { element } => {
641                // TODO: https://github.com/servo/servo/issues/44685
642                #[expect(unsafe_code)]
643                let mut cx = unsafe { script_bindings::script_runtime::temp_cx() };
644                Self::load_with_element(
645                    &mut cx,
646                    element,
647                    source,
648                    media,
649                    resolved_url.into(),
650                    None,
651                    "".to_owned(),
652                );
653            },
654            ElementStylesheetLoader::Asynchronous(AsynchronousStylesheetLoader {
655                element,
656                main_thread_sender,
657                pipeline_id,
658            }) => {
659                let element = element.clone();
660                let task = task!(load_import_stylesheet_on_main_thread: move || {
661                    // TODO: https://github.com/servo/servo/issues/44685
662                    #[expect(unsafe_code)]
663                    let mut cx = unsafe { script_bindings::script_runtime::temp_cx() };
664                    Self::load_with_element(
665                        &mut cx,
666                        &element.root(),
667                        source,
668                        media,
669                        resolved_url.into(),
670                        None,
671                        "".to_owned()
672                    );
673                });
674                let _ =
675                    main_thread_sender.send(MainThreadScriptMsg::Common(CommonScriptMsg::Task(
676                        ScriptThreadEventCategory::StylesheetLoad,
677                        Box::new(task),
678                        Some(*pipeline_id),
679                        TaskSourceName::Networking,
680                    )));
681            },
682        }
683
684        import_rule
685    }
686}
687
688pub(crate) struct AsynchronousStylesheetLoader {
689    element: Trusted<HTMLElement>,
690    main_thread_sender: Sender<MainThreadScriptMsg>,
691    pipeline_id: PipelineId,
692}
693
694impl AsynchronousStylesheetLoader {
695    pub(crate) fn new(element: &HTMLElement) -> Self {
696        let window = element.owner_window();
697        Self {
698            element: Trusted::new(element),
699            main_thread_sender: window.main_thread_script_chan().clone(),
700            pipeline_id: window.pipeline_id(),
701        }
702    }
703}