lol-html icon indicating copy to clipboard operation
lol-html copied to clipboard

Allow manipulating attributes while iterating over them

Open Fxxxlei opened this issue 2 years ago • 1 comments

I would like to have a function similar to Vec::retain_mut to remove or modify the attributes of an element while iterating over them.

This is currently doable, but involves iterating over the attributes, collecting and cloning their names, potentially cloning the new value, and searching the attribute in the attribute list every time we want to remove or modify an attribute.

The proposed function would make this much more efficient and simpler to implement. I guess this function would have to be available on StartTag and on Element. I am not quite sure about the naming, but I'd be willing to contribute this.

Fxxxlei avatar Feb 08 '23 17:02 Fxxxlei

Since we have to reset StartTag::raw if any attribute is modified, we cannot just pass a mutable reference to an Attribute.

We could either pass a wrapper to an Attribute, or return an enum in our callback (Noop, Remove, Replace(new_value))

Each solution has some drawback. The solution with the wrapper is more complex, while the solution with the return value requires the new value to be owned. I personally prefer the second solution.

Fxxxlei avatar Feb 08 '23 18:02 Fxxxlei