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

Code before:
            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> {
        let start = self.start_container();
        let window = start.owner_window();

        let client_rects = self
            .client_rects()
            .map(|rect| {
                DOMRect::new(
                    window.upcast(),
                    rect.origin.x.to_f64_px(),
                    rect.origin.y.to_f64_px(),
                    rect.size.width.to_f64_px(),
                    rect.size.height.to_f64_px(),
                    can_gc,
                )
            })
            .collect();

        DOMRectList::new(&window, client_rects, can_gc)
    }

    /// <https://drafts.csswg.org/cssom-view/#dom-range-getboundingclientrect>
    fn GetBoundingClientRect(&self, can_gc: CanGc) -> DomRoot<DOMRect> {
        let window = self.start_container().owner_window();


Review comment (suggestion):
```suggestion
        let fragment_node = element.parse_fragment(compliant_string, can_gc)?;
```

Code after:
        // 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)?;

        // Step 8. For each script of fragment node's script element descendants:
        for node in fragment_node
            .upcast::<Node>()
            .traverse_preorder(ShadowIncluding::No)
        {
            if let Some(script) = node.downcast::<HTMLScriptElement>() {
                // Step 8.1. Set script's already started to false.
                script.set_already_started(false);
                // Step 8.2. Set script's parser document to null.
                script.set_parser_inserted(false);
            }
        }

        // Step 9. Return fragment node.
        Ok(fragment_node)
    }

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

        let client_rects = self
            .client_rects()