Repository: servo/servo
File: components/script/dom/servoparser/mod.rs (Rust)

Code before:
            navigation_params: NavigationParams {
                policy_container: Default::default(),
                content_type: None,
                link_headers: vec![],
                final_sandboxing_flag_set: creation_sandboxing_flag_set,
                resource_header: vec![],
                about_base_url: Default::default(),
            },
        }
    }

    pub(crate) fn set_policy_container(&mut self, policy_container: Option<&PolicyContainer>) {
        let Some(policy_container) = policy_container else {
            return;
        };
        self.navigation_params.policy_container = policy_container.clone();
    }

    pub(crate) fn set_about_base_url(&mut self, about_base_url: Option<ServoUrl>) {
        self.navigation_params.about_base_url = about_base_url;
    }

    pub(crate) fn get_document(&self) -> Option<DomRoot<Document>> {
        self.parser
            .as_ref()
            .map(|parser| parser.root().get_document())
    }

    /// <https://html.spec.whatwg.org/multipage/#creating-a-policy-container-from-a-fetch-response>
    fn create_policy_container_from_fetch_response(metadata: &Metadata) -> PolicyContainer {
        // Step 1. If response's URL's scheme is "blob", then return a clone of response's URL's blob URL entry's environment's policy container.
        // TODO
        // Step 2. Let result be a new policy container.
        // Step 7. Return result.
        PolicyContainer {
            // Step 3. Set result's CSP list to the result of parsing a response's Content Security Policies given response.
            csp_list: parse_csp_list_from_metadata(&metadata.headers),
            // Step 5. Set result's referrer policy to the result of parsing the `Referrer-Policy` header given response. [REFERRERPOLICY]
            referrer_policy: ReferrerPolicy::parse_header_for_response(&metadata.headers),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#initialise-the-document-object>
    fn initialize_document_object(&self, document: &Document) {
        // Step 9. Let document be a new Document, with
        document.set_policy_container(self.navigation_params.policy_container.clone());
        document.set_active_sandboxing_flag_set(self.navigation_params.final_sandboxing_flag_set);
        document.set_about_base_url(self.navigation_params.about_base_url.clone());
        // Step 17. Process link headers given document, navigationParams's response, and "pre-media".
        process_link_headers(
            &self.navigation_params.link_headers,

Review comment (suggestion):
```suggestion
            .map(|parser| parser.root().document.as_rooted())
```

Code after:
                final_sandboxing_flag_set: creation_sandboxing_flag_set,
                resource_header: vec![],
                about_base_url: Default::default(),
            },
        }
    }

    pub(crate) fn set_policy_container(&mut self, policy_container: Option<&PolicyContainer>) {
        let Some(policy_container) = policy_container else {
            return;
        };
        self.navigation_params.policy_container = policy_container.clone();
    }

    pub(crate) fn set_about_base_url(&mut self, about_base_url: Option<ServoUrl>) {
        self.navigation_params.about_base_url = about_base_url;
    }

    pub(crate) fn get_document(&self) -> Option<DomRoot<Document>> {
        self.parser
            .as_ref()
            .map(|parser| parser.root().document.as_rooted())
    }

    /// <https://html.spec.whatwg.org/multipage/#creating-a-policy-container-from-a-fetch-response>
    fn create_policy_container_from_fetch_response(metadata: &Metadata) -> PolicyContainer {
        // Step 1. If response's URL's scheme is "blob", then return a clone of response's URL's blob URL entry's environment's policy container.
        // TODO
        // Step 2. Let result be a new policy container.
        // Step 7. Return result.
        PolicyContainer {
            // Step 3. Set result's CSP list to the result of parsing a response's Content Security Policies given response.
            csp_list: parse_csp_list_from_metadata(&metadata.headers),
            // Step 5. Set result's referrer policy to the result of parsing the `Referrer-Policy` header given response. [REFERRERPOLICY]
            referrer_policy: ReferrerPolicy::parse_header_for_response(&metadata.headers),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#initialise-the-document-object>
    fn initialize_document_object(&self, document: &Document) {
        // Step 9. Let document be a new Document, with
        document.set_policy_container(self.navigation_params.policy_container.clone());
        document.set_active_sandboxing_flag_set(self.navigation_params.final_sandboxing_flag_set);
        document.set_about_base_url(self.navigation_params.about_base_url.clone());
        // Step 17. Process link headers given document, navigationParams's response, and "pre-media".
        process_link_headers(
            &self.navigation_params.link_headers,
            document,
            LinkProcessingPhase::PreMedia,
        );
    }