servo / servo/html5ever

Replacing NodeRef with it's child

Open
#433 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.6k
Forks
288
Avg merge
2d 22h
Merged PRs (30d)
8

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Node and NodeRef APIs shown in the issue, especially the existing Node::replace function and the child insertion and detach operations. Trace how replacing a node affects its parent and descendants, then verify that a direct-child replacement preserves the intended DOM structure and supports the recursive Filter example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
web-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.