use std::borrow::Cow;
use super::parse::parse_html;
use crate::converter::utility::preprocessing::{html_inert_starts, unwrap_kept_inert_elements};
use crate::error::Result;
use crate::options::HiddenContent;
pub(super) fn remove_kept_tags<'a>(
html: &'a str,
hidden_content: HiddenContent,
selectors: &[String],
) -> Result<Cow<'a, str>> {
let excluded = excluded_starts(html, hidden_content, selectors)?;
Ok(unwrap_kept_inert_elements(html, hidden_content, &excluded))
}
fn excluded_starts(html: &str, hidden_content: HiddenContent, selectors: &[String]) -> Result<Vec<usize>> {
if hidden_content == HiddenContent::Drop || selectors.is_empty() {
return Ok(Vec::new());
}
let dom = parse_html(html)?;
let parser = dom.parser();
let mut starts = Vec::new();
for selector in selectors {
if let Some(matches) = dom.query_selector(selector) {
starts
.extend(matches.filter_map(|handle| handle.get(parser)?.as_tag().map(|tag| tag.boundaries(parser).0)));
}
}
if !starts.is_empty() {
let html_starts = html_inert_starts(html, hidden_content);
starts.retain(|start| html_starts.binary_search(start).is_ok());
}
starts.sort_unstable();
Ok(starts)
}