kuchiki-rs / kuchiki

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No easy way to access attributes

untitaker opened this issue · comments

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.

I’ve added some convenience methods in 29d69a2 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.

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

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.