Repository: servo/servo
File: components/script/stylesheet_loader.rs (Rust)

Code before:
    }

    fn do_post_parse_tasks(self, success: bool, stylesheet: Arc<Stylesheet>) {
        let element = self.element.root();
        let document = self.document.root();
        let owner = element
            .upcast::<Element>()
            .as_stylesheet_owner()
            .expect("Stylesheet not loaded by <style> or <link> element!");

        // From <https://html.spec.whatwg.org/multipage/#link-type-stylesheet>:
        // > If `el` no longer creates an external resource link that contributes to the
        // > styling processing model, or if, since the resource in question was fetched, it
        // > has become appropriate to fetch it again, then:
        // >   1. Remove el from el's node document's script-blocking style sheet set.
        // >   2. Return.
        if !self.contributes_to_the_styling_processing_model(&element) {
            // Always consider ignored loads as successful, as they shouldn't cause any subsequent
            // successful loads to fire an "error" event.
            self.finish_load(true, owner, &element, &document);
            return;
        }

        match &self.source {
            StylesheetContextSource::LinkElement => {
                let link = element.downcast::<HTMLLinkElement>().unwrap();
                if link.is_effectively_disabled() {
                    stylesheet.set_disabled(true);
                }
                link.set_stylesheet(stylesheet);
            },
            StylesheetContextSource::Import(import_rule) => {
                // Construct a new WebFontDocumentContext for the stylesheet
                let window = element.owner_window();
                let document_context = window.web_font_context();

                // Layout knows about this stylesheet, because Stylo added it to the Stylist,
                // but Layout doesn't know about any new web fonts that it contains.
                document.load_web_fonts_from_stylesheet(&stylesheet, &document_context);

                let mut guard = document.style_shared_lock().write();
                import_rule.write_with(&mut guard).stylesheet = ImportSheet::Sheet(stylesheet);
            },
        }

        if let Some(ref shadow_root) = self.shadow_root {
            shadow_root.root().invalidate_stylesheets();
        } else {
            document.invalidate_stylesheets();
        }
        owner.set_origin_clean(self.origin_clean);

Review comment (suggestion):
See #40744:

```suggestion
                let link = element.downcast::<HTMLLinkElement>().expect("Should be HTMLinkElement due to StylesheetContextSource");
```



Code after:
    }

    fn do_post_parse_tasks(self, success: bool, stylesheet: Arc<Stylesheet>) {
        let element = self.element.root();
        let document = self.document.root();
        let owner = element
            .upcast::<Element>()
            .as_stylesheet_owner()
            .expect("Stylesheet not loaded by <style> or <link> element!");

        // From <https://html.spec.whatwg.org/multipage/#link-type-stylesheet>:
        // > If `el` no longer creates an external resource link that contributes to the
        // > styling processing model, or if, since the resource in question was fetched, it
        // > has become appropriate to fetch it again, then:
        // >   1. Remove el from el's node document's script-blocking style sheet set.
        // >   2. Return.
        if !self.contributes_to_the_styling_processing_model(&element) {
            // Always consider ignored loads as successful, as they shouldn't cause any subsequent
            // successful loads to fire an "error" event.
            self.finish_load(true, owner, &element, &document);
            return;
        }

        match &self.source {
            StylesheetContextSource::LinkElement => {
                let link = element
                    .downcast::<HTMLLinkElement>()
                    .expect("Should be HTMLinkElement due to StylesheetContextSource");
                if link.is_effectively_disabled() {
                    stylesheet.set_disabled(true);
                }
                link.set_stylesheet(stylesheet);
            },
            StylesheetContextSource::Import(import_rule) => {
                // Construct a new WebFontDocumentContext for the stylesheet
                let window = element.owner_window();
                let document_context = window.web_font_context();

                // Layout knows about this stylesheet, because Stylo added it to the Stylist,
                // but Layout doesn't know about any new web fonts that it contains.
                document.load_web_fonts_from_stylesheet(&stylesheet, &document_context);

                let mut guard = document.style_shared_lock().write();
                import_rule.write_with(&mut guard).stylesheet = ImportSheet::Sheet(stylesheet);
            },
        }

        if let Some(ref shadow_root) = self.shadow_root {
            shadow_root.root().invalidate_stylesheets();
        } else {
            document.invalidate_stylesheets();