Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing NodeRef with it's child #433

Open
ArmenAg opened this issue Feb 2, 2021 · 0 comments
Open

Replacing NodeRef with it's child #433

ArmenAg opened this issue Feb 2, 2021 · 0 comments

Comments

@ArmenAg
Copy link

ArmenAg commented Feb 2, 2021

I'm working on a project that requires manipulating DOM to simplify HTML. One example of a transform is removing all nested <div> to the innermost <div> element.

My first attempt was to do the following

impl Filter for CollapseTwoConsecutiveDivElementsFilter {
    fn filter(&self, node: NodeRef) -> bool {
        let mut g_has_mutated = false;
        if CollapseTwoConsecutiveDivElementsFilter::is_div_element(&node)
            && node.children().count() == 1
        {
            if CollapseTwoConsecutiveDivElementsFilter::is_div_element(&node.first_child().unwrap())
            {
                node.first_child().unwrap().insert_before(node.clone());
                node.clone().detach();
                g_has_mutated = true;
            }
        }
        for child in node.clone().borrow_mut().children() {
            g_has_mutated |= self.filter(child.clone());
        }

        return g_has_mutated;
    }
}

but I quickly realized I'm not augmenting node, therefore i get the wrong behavior. I saw implemented internally of Node there was a .replace function. Is it possible to do replace current NodeRef with it's direct child?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant