Repository: servo/servo
File: components/script/dom/html/htmlbaseelement.rs (Rust)

Code before:
    pub(crate) fn clear_frozen_base_url(&self) {
        *self.frozen_base_url.borrow_mut() = None;
    }

    /// <https://html.spec.whatwg.org/multipage/#set-the-frozen-base-url>
    pub(crate) fn set_frozen_base_url(&self) {
        // Step 1. Let document be element's node document.
        let document = self.owner_document();
        // Step 2. Let urlRecord be the result of parsing the value of element's href content attribute
        // with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.)
        let attr = self
            .upcast::<Element>()
            .get_attribute(&ns!(), &local_name!("href"));
        let Some(href_value) = attr.as_ref().map(|attr| attr.value()) else {
            unreachable!("Must always have a href set when setting frozen base URL");
        };
        let document_fallback_url = document.fallback_base_url();
        let url_record = document_fallback_url.join(&href_value).ok();
        // Step 3. If any of the following are true:
        if
        // urlRecord is failure;
        url_record.as_ref().is_none_or(|url_record|
            // urlRecord's scheme is "data" or "javascript"; or
            url_record.scheme() == "data" || url_record.scheme() == "javascript")
        // running Is base allowed for Document? on urlRecord and document returns "Blocked",
        || url_record.as_ref().is_some_and(|url_record| {
            !document
                .get_csp_list()
                .is_base_allowed_for_document(
                    document.window().upcast::<GlobalScope>(),
                    url_record,
                    document.origin().immutable(),
                )
        }) {
            // then set element's frozen base URL to document's fallback base URL and return.
            *self.frozen_base_url.borrow_mut() = Some(document_fallback_url);
            return;
        }
        // Step 4. Set element's frozen base URL to urlRecord.
        *self.frozen_base_url.borrow_mut() = url_record;
        // Step 5. Respond to base URL changes given document.
        // TODO
    }

    /// <https://html.spec.whatwg.org/multipage/#frozen-base-url>
    pub(crate) fn frozen_base_url(&self) -> ServoUrl {
        self.frozen_base_url
            .borrow()
            .clone()
            .expect("Must only retrieve frozen base URL for valid base elements")
    }

Review comment (refactor):
Nit: you can move this into the `is_none_or` check, since at that point we already have the `url_record`

Code after:
    pub(crate) fn clear_frozen_base_url(&self) {
        *self.frozen_base_url.borrow_mut() = None;
    }

    /// <https://html.spec.whatwg.org/multipage/#set-the-frozen-base-url>
    pub(crate) fn set_frozen_base_url(&self) {
        // Step 1. Let document be element's node document.
        let document = self.owner_document();
        // Step 2. Let urlRecord be the result of parsing the value of element's href content attribute
        // with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.)
        let attr = self
            .upcast::<Element>()
            .get_attribute(&ns!(), &local_name!("href"));
        let Some(href_value) = attr.as_ref().map(|attr| attr.value()) else {
            unreachable!("Must always have a href set when setting frozen base URL");
        };
        let document_fallback_url = document.fallback_base_url();
        let url_record = document_fallback_url.join(&href_value).ok();
        // Step 3. If any of the following are true:
        if
        // urlRecord is failure;
        url_record.as_ref().is_none_or(|url_record|
            // urlRecord's scheme is "data" or "javascript"; or
            url_record.scheme() == "data" || url_record.scheme() == "javascript"
            // running Is base allowed for Document? on urlRecord and document returns "Blocked",
            || !document
                .get_csp_list()
                .is_base_allowed_for_document(
                    document.window().upcast::<GlobalScope>(),
                    &url_record.clone().into_url(),
                    &document.origin().immutable().clone().into_url_origin(),
                ))
        {
            // then set element's frozen base URL to document's fallback base URL and return.
            *self.frozen_base_url.borrow_mut() = Some(document_fallback_url);
            return;
        }
        // Step 4. Set element's frozen base URL to urlRecord.
        *self.frozen_base_url.borrow_mut() = url_record;
        // Step 5. Respond to base URL changes given document.
        // TODO
    }

    /// <https://html.spec.whatwg.org/multipage/#frozen-base-url>
    pub(crate) fn frozen_base_url(&self) -> ServoUrl {
        self.frozen_base_url
            .borrow()
            .clone()
            .expect("Must only retrieve frozen base URL for valid base elements")
    }
}