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

Code before:
            ),
            SubmissionValue::None => {},
            SubmissionValue::USVString(string) => {
                entry_list.push(FormDatum {
                    ty: DOMString::from("string"),
                    name,
                    value: FormDatumValue::String(DOMString::from(string.to_string())),
                });
            },
            SubmissionValue::File(file) => {
                entry_list.push(FormDatum {
                    ty: DOMString::from("file"),
                    name,
                    value: FormDatumValue::File(DomRoot::from_ref(file)),
                });
            },
        }
    }

    pub(crate) fn is_invalid(&self, can_gc: CanGc) -> bool {
        self.is_target_form_associated() &&
            self.is_instance_validatable() &&
            !self.satisfies_constraints(can_gc)
    }

    pub(crate) fn custom_states(&self) -> &MutNullableDom<CustomStateSet> {
        &self.states
    }
}

impl ElementInternalsMethods<crate::DomTypeHolder> for ElementInternals {
    /// <https://html.spec.whatwg.org/multipage/#dom-elementinternals-shadowroot>
    fn GetShadowRoot(&self) -> Option<DomRoot<ShadowRoot>> {
        // Step 1. Let target be this's target element.
        // Step 2. If target is not a shadow host, then return null.
        // Step 3. Let shadow be target's shadow root.
        let shadow = self.target_element.upcast::<Element>().shadow_root()?;

        // Step 4. If shadow's available to element internals is false, then return null.
        if !shadow.is_available_to_element_internals() {
            return None;
        }

        // Step 5. Return shadow.
        Some(shadow)
    }

    /// <https://html.spec.whatwg.org/multipage#dom-elementinternals-setformvalue>
    fn SetFormValue(
        &self,
        value: Option<FileOrUSVStringOrFormData>,

Review comment (suggestion):
I prefer not to return types that callers can mutate, when possible. I think I would prefer to leave this method and add a `custom_states_for_layout` that calls `.get_inner_as_layout()` instead.

Code after:
            ),
            SubmissionValue::None => {},
            SubmissionValue::USVString(string) => {
                entry_list.push(FormDatum {
                    ty: DOMString::from("string"),
                    name,
                    value: FormDatumValue::String(DOMString::from(string.to_string())),
                });
            },
            SubmissionValue::File(file) => {
                entry_list.push(FormDatum {
                    ty: DOMString::from("file"),
                    name,
                    value: FormDatumValue::File(DomRoot::from_ref(file)),
                });
            },
        }
    }

    pub(crate) fn is_invalid(&self, can_gc: CanGc) -> bool {
        self.is_target_form_associated() &&
            self.is_instance_validatable() &&
            !self.satisfies_constraints(can_gc)
    }

    pub(crate) fn custom_states(&self) -> Option<DomRoot<CustomStateSet>> {
        self.states.get()
    }

    pub(crate) fn custom_states_for_layout<'a>(&'a self) -> Option<LayoutDom<'a, CustomStateSet>> {
        #[expect(unsafe_code)]
        unsafe {
            self.states.get_inner_as_layout()
        }
    }
}

impl ElementInternalsMethods<crate::DomTypeHolder> for ElementInternals {
    /// <https://html.spec.whatwg.org/multipage/#dom-elementinternals-shadowroot>
    fn GetShadowRoot(&self) -> Option<DomRoot<ShadowRoot>> {
        // Step 1. Let target be this's target element.
        // Step 2. If target is not a shadow host, then return null.
        // Step 3. Let shadow be target's shadow root.
        let shadow = self.target_element.upcast::<Element>().shadow_root()?;

        // Step 4. If shadow's available to element internals is false, then return null.
        if !shadow.is_available_to_element_internals() {
            return None;
        }

        // Step 5. Return shadow.