kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust
MIT License
470 stars 54 forks source link

No easy way to access attributes #17

Closed untitaker closed 8 years ago

untitaker commented 8 years ago

the attributes field on ElementData is a HashMap<QualName, String>. QualName doesn't impl Eq for anything but itself, accessing a specific attribute requires constructing a QualName object.

SimonSapin commented 8 years ago

I’ve added some convenience methods in 29d69a2a995ba58c5efcc689a93846e780524eba and publish it as 0.2.

https://simonsapin.github.io/kuchiki/kuchiki/struct.Attributes.html

for element in doc.select("something") {
    let attributes = element.attributes.borrow();
    if let Some(icon) = attributes.get(atom!("icon")) {
        // ...
    }
}

Use #[macro_use] extern crate string_cache; to get the atom! macro. It "interns" a string a compile-time, but only for strings listed at: https://simonsapin.github.io/kuchiki/string_cache/macro.atom!.html

The methods also accept &str, but in that case they do the string interning themselves at a small run-time cost.

Let me know how it goes in practice or if other convenience things can be added.

untitaker commented 8 years ago

This is slightly better, thank you. Apart from this one I couldn't find any ergonomics-issues.

untitaker commented 7 years ago

Since kuchiki 0.4 this no longer appears to work. I suppose I have to use html5ever-atoms, but their qualname macro requires an additional namespace parameter (not appropriate for html).

EDIT: Apparently local_name! does the trick.