kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to modify a value of an Attribute

Skallwar opened this issue · comments

Hi,
I need to modify the dom tree by replacing all 'src' values.
As data() returns a reference instead of a mutable reference, is it possible to do so ?

The keyword mut in &mut Foo and calling it a "mutable" reference are misleading. It is better to think of it as an exclusive reference, with &Foo being a shared reference.

Nodes in a Kuchiki tree are reference-counted, so there can be multiple NodeRefs (internally Rc<Node>) pointing to the same node, so references are almost always shared. (Rc implements Deref but not DerefMut.) There is no Node::data_mut method because it wouldn’t be very useful since it would require a &mut Node exclusive reference that you typically don’t have.

An exclusive reference trivially enables mutability, but it’s not the only way. Cell, RefCell, and Mutex are all wrapper types that provide mutability through shared references, in exchanges for some restrictions (such as RefCell’s runtime checking that its borrow_mut is not used while another borrow is used, so it can if fact provide an exclusive reference to its contents).

Attributes in Kuchiki are kept in the ElementData::attributes which is a RefCell.

Thanks for that, it's working now