Repository: jaytaph/gosub-browser
File: src/html5_parser/parser.rs (Rust)

Code before:

            if idx == 0 {
                // Reached the beginning of the list
                return None;
            }

            idx -= 1;
        }
    }

    /// Adds a marker to the active formatting stack
    fn active_formatting_elements_push_marker(&mut self) {
        self.active_formatting_elements.push(ActiveElement::Marker);
    }

    /// Clear the active formatting stack until we reach the first marker
    fn active_formatting_elements_clear_until_marker(&mut self) {
        while let Some(active_elem) = self.active_formatting_elements.pop() {
            if let ActiveElement::Marker = active_elem {
                // Found the marker
                return;
            }
        }
    }

    /// Remove the given node_id from the active formatting elements list. Wil do nothing when the node is not found
    fn active_formatting_elements_remove(&mut self, target_node_id: NodeId) {
        self.active_formatting_elements
            .retain(|node_id| match node_id {
                ActiveElement::Node(node_id) => *node_id != target_node_id,
                _ => true,
            });
    }

    /// Push a node onto the active formatting stack, make sure only max 3 of them can be added (between markers)
    fn active_formatting_elements_push(&mut self, node_id: NodeId) {
        let mut idx = self.active_formatting_elements.len();
        if idx == 0 {
            self.active_formatting_elements
                .push(ActiveElement::Node(node_id));
            return;
        }

        // Fetch the node we want to push, so we can compare
        let element_node = get_node_by_id!(self.document, node_id);

        let mut found = 0;
        loop {
            let active_elem = *self
                .active_formatting_elements
                .get(idx - 1)

Review comment (suggestion):
```suggestion
    /// Remove the given node_id from the active formatting elements list. Will do nothing when the node is not found
```

Code after:

            if idx == 0 {
                // Reached the beginning of the list
                return None;
            }

            idx -= 1;
        }
    }

    /// Adds a marker to the active formatting stack
    fn active_formatting_elements_push_marker(&mut self) {
        self.active_formatting_elements.push(ActiveElement::Marker);
    }

    /// Clear the active formatting stack until we reach the first marker
    fn active_formatting_elements_clear_until_marker(&mut self) {
        while let Some(active_elem) = self.active_formatting_elements.pop() {
            if let ActiveElement::Marker = active_elem {
                // Found the marker
                return;
            }
        }
    }

    /// Remove the given node_id from the active formatting elements list. Will do nothing when the node is not found
    fn active_formatting_elements_remove(&mut self, target_node_id: NodeId) {
        self.active_formatting_elements
            .retain(|node_id| match node_id {
                ActiveElement::Node(node_id) => *node_id != target_node_id,
                _ => true,
            });
    }

    /// Push a node onto the active formatting stack, make sure only max 3 of them can be added (between markers)
    fn active_formatting_elements_push(&mut self, node_id: NodeId) {
        let mut idx = self.active_formatting_elements.len();
        if idx == 0 {
            self.active_formatting_elements
                .push(ActiveElement::Node(node_id));
            return;
        }

        // Fetch the node we want to push, so we can compare
        let element_node = get_node_by_id!(self.document, node_id);

        let mut found = 0;
        loop {
            let active_elem = *self
                .active_formatting_elements
                .get(idx - 1)