Skip to main content

selectors

Macro selectors 

Source
macro_rules! selectors {
    ($( $vis:vis $name:ident = $css:literal; )+) => { ... };
}
Expand description

Declares lazily parsed, process-wide CSS selector accessors.

Each generated function parses its CSS literal at most once and returns the same &'static selector thereafter. Invalid selectors panic on first use.

ยงExample

millipede_html::selectors! {
    pub title_sel = "title";
    product = "article.product_pod h3 a";
}

struct HandlerContext {
    html: millipede_html::scraper::Html,
}

fn handler(ctx: &HandlerContext) {
    for title in ctx.html.select(title_sel()) {
        println!("{}", title.text().collect::<String>());
    }
    let _products = ctx.html.select(product()).count();
}

let ctx = HandlerContext {
    html: millipede_html::scraper::Html::parse_document(
        "<title>Millipede</title><article class='product_pod'><h3><a>Item</a></h3></article>",
    ),
};
handler(&ctx);