use std::borrow::Cow;
use simdhtml::document::Document;
use simdhtml::document::id::NodeId;
use crate::bitset::BitSet;
use crate::error::Result;
use crate::record::{MatchKind, MatchRecord};
use super::ast::SelectorList;
use super::cache::QueryCache;
use super::eval::{Eval, common_pseudo};
pub trait Projection {
type Output<'a>;
fn finish<'a>(
doc: &'a Document<'_>,
selector: &SelectorList,
mask: BitSet,
) -> Result<Self::Output<'a>>;
}
pub struct Count;
impl Projection for Count {
type Output<'a> = usize;
fn finish<'a>(
doc: &'a Document<'_>,
selector: &SelectorList,
mask: BitSet,
) -> Result<Self::Output<'a>> {
if common_pseudo(selector).is_some() {
let cache = QueryCache::root();
Ok(Eval::new(doc, &cache).records_for(selector, &mask).len())
} else {
Ok(mask.count_ones() as usize)
}
}
}
pub struct Html;
impl Projection for Html {
type Output<'a> = Vec<Cow<'a, str>>;
fn finish<'a>(
doc: &'a Document<'_>,
selector: &SelectorList,
mask: BitSet,
) -> Result<Self::Output<'a>> {
let cache = QueryCache::root();
Ok(Eval::new(doc, &cache)
.records_for(selector, &mask)
.into_iter()
.map(|record| extract(doc, &record))
.collect())
}
}
pub struct Texts;
impl Projection for Texts {
type Output<'a> = Vec<Cow<'a, str>>;
fn finish<'a>(
doc: &'a Document<'_>,
selector: &SelectorList,
mask: BitSet,
) -> Result<Self::Output<'a>> {
let cache = QueryCache::root();
Ok(Eval::new(doc, &cache)
.records_for(selector, &mask)
.into_iter()
.map(|record| match record.kind {
MatchKind::Element => Cow::Owned(crate::access::text_content(doc, record.node)),
MatchKind::Text => doc.text(record.node),
MatchKind::Attr => {
let span = record.attr.as_ref().expect("attr record");
crate::access::attr_value_span(doc, span)
}
})
.collect())
}
}
#[cfg(test)]
pub fn select(doc: &Document<'_>, selector: &SelectorList) -> Result<Vec<MatchRecord>> {
let cache = QueryCache::root();
let ctx = Eval::new(doc, &cache);
ctx.select(selector, None)
}
pub fn select_within_cache(
doc: &Document<'_>,
selector: &SelectorList,
roots: &[NodeId],
cache: &QueryCache,
) -> Result<Vec<MatchRecord>> {
Eval::new(doc, cache).select(selector, Some(roots))
}
pub fn select_as_within_cache<'a, P: Projection>(
doc: &'a Document<'_>,
selector: &SelectorList,
roots: &[NodeId],
cache: &QueryCache,
) -> Result<P::Output<'a>> {
let ctx = Eval::new(doc, cache);
let mask = ctx.mask_for(selector, Some(roots))?;
P::finish(doc, selector, mask)
}
pub fn extract<'a>(doc: &'a Document<'_>, record: &MatchRecord) -> Cow<'a, str> {
match record.kind {
MatchKind::Element => {
let bytes = crate::access::outer_html(doc, record.node);
Cow::Borrowed(std::str::from_utf8(bytes).expect("Document input is valid UTF-8"))
}
MatchKind::Text => doc.text(record.node),
MatchKind::Attr => {
let span = record.attr.as_ref().expect("attr record has a value span");
crate::access::attr_value_span(doc, span)
}
}
}