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

Code before:
        if let Some(text_node) = end_node.downcast::<Text>() {
            let char_data = text_node.upcast::<CharacterData>();
            s.push_str(&char_data.SubstringData(0, self.end_offset()).unwrap());
        }

        // Step 6.
        s
    }

    /// <https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#extensions-to-the-range-interface>
    fn CreateContextualFragment(
        &self,
        fragment: DOMString,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<DocumentFragment>> {
        // Step 1.
        let node = self.start_container();
        let owner_doc = node.owner_doc();
        let element = match node.type_id() {
            NodeTypeId::Document(_) | NodeTypeId::DocumentFragment(_) => None,
            NodeTypeId::Element(_) => Some(DomRoot::downcast::<Element>(node).unwrap()),
            NodeTypeId::CharacterData(CharacterDataTypeId::Comment) |
            NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => node.GetParentElement(),
            NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) |
            NodeTypeId::DocumentType => unreachable!(),
            NodeTypeId::Attr => unreachable!(),
        };

        // Step 2.
        let element = Element::fragment_parsing_context(&owner_doc, element.as_deref(), can_gc);

        // Step 3.
        let fragment_node = element.parse_fragment(fragment, can_gc)?;

        // Step 4.
        for node in fragment_node
            .upcast::<Node>()
            .traverse_preorder(ShadowIncluding::No)
        {
            if let Some(script) = node.downcast::<HTMLScriptElement>() {
                script.set_already_started(false);
                script.set_parser_inserted(false);
            }
        }

        // Step 5.
        Ok(fragment_node)
    }

    /// <https://drafts.csswg.org/cssom-view/#dom-range-getclientrects>
    fn GetClientRects(&self, can_gc: CanGc) -> DomRoot<DOMRectList> {

Review comment (suggestion):
```suggestion
        let compliant_string = TrustedHTML::get_trusted_script_compliant_string(
```

Code after:

        // Step 5.
        if let Some(text_node) = end_node.downcast::<Text>() {
            let char_data = text_node.upcast::<CharacterData>();
            s.push_str(&char_data.SubstringData(0, self.end_offset()).unwrap());
        }

        // Step 6.
        s
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-range-createcontextualfragment>
    fn CreateContextualFragment(
        &self,
        fragment: TrustedHTMLOrString,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<DocumentFragment>> {
        // Step 2. Let node be this's start node.
        //
        // Required to obtain the global, so we do this first. Shouldn't be an
        // observable difference.
        let node = self.start_container();
        // Step 1. Let compliantString be the result of invoking the
        // Get Trusted Type compliant string algorithm with TrustedHTML,
        // this's relevant global object, string, "Range createContextualFragment", and "script".
        let fragment = TrustedHTML::get_trusted_script_compliant_string(
            node.owner_window().upcast(),
            fragment,
            "Range createContextualFragment",
            can_gc,
        )?;
        let owner_doc = node.owner_doc();
        let element = match node.type_id() {
            // Step 3. Let element be null.
            NodeTypeId::Document(_) | NodeTypeId::DocumentFragment(_) => None,
            // Step 4. If node implements Element, set element to node.
            NodeTypeId::Element(_) => Some(DomRoot::downcast::<Element>(node).unwrap()),
            // Step 5. Otherwise, if node implements Text or Comment, set element to node's parent element.
            NodeTypeId::CharacterData(CharacterDataTypeId::Comment) |
            NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => node.GetParentElement(),
            NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) |
            NodeTypeId::DocumentType => unreachable!(),
            NodeTypeId::Attr => unreachable!(),
        };

        // Step 6. If element is null or all of the following are true:
        let element = Element::fragment_parsing_context(&owner_doc, element.as_deref(), can_gc);

        // Step 7. Let fragment node be the result of invoking the fragment parsing algorithm steps with element and compliantString.
        let fragment_node = element.parse_fragment(fragment, can_gc)?;