use super::define_import_rule::{DomImportFn, DomImportRule};
use super::sel::{Predicate, SelectorKind};
use crate::dom::{DomNode, DomNodeType};
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Clone)]
pub(crate) struct CompiledRule {
pub(crate) predicate: Predicate,
pub(crate) import: DomImportFn,
}
pub(crate) struct CompiledDispatch {
pub(crate) rules: Vec<CompiledRule>,
by_tag: HashMap<String, Vec<usize>>,
wildcard_indices: Vec<usize>,
text_indices: Vec<usize>,
comment_indices: Vec<usize>,
}
fn merge_sorted_asc(a: &[usize], b: &[usize]) -> Vec<usize> {
let mut out = Vec::with_capacity(a.len() + b.len());
let (mut i, mut j) = (0, 0);
while i < a.len() && j < b.len() {
if a[i] <= b[j] {
out.push(a[i]);
i += 1;
} else {
out.push(b[j]);
j += 1;
}
}
out.extend_from_slice(&a[i..]);
out.extend_from_slice(&b[j..]);
out
}
pub(crate) fn compile_import_rules(rules: &[DomImportRule]) -> CompiledDispatch {
let mut compiled = Vec::with_capacity(rules.len());
let mut by_tag: HashMap<String, Vec<usize>> = HashMap::new();
let mut wildcard_indices = Vec::new();
let mut text_indices = Vec::new();
let mut comment_indices = Vec::new();
let mut seen_names: Vec<&str> = Vec::new();
for (i, rule) in rules.iter().enumerate() {
let sel = &rule.match_;
if let Some(name) = &rule.name {
if seen_names.contains(&name.as_str()) {
crate::warnings::console_warn(&format!(
"[lexical] duplicate DOMImportRule name \"{name}\" — keep names unique to aid debugging."
));
}
if !name.is_empty() {
seen_names.push(name);
}
}
compiled.push(CompiledRule {
predicate: sel.predicate(),
import: rule.import.clone(),
});
match sel.kind {
SelectorKind::Text => text_indices.push(i),
SelectorKind::Comment => comment_indices.push(i),
SelectorKind::Element if sel.tags.is_empty() => wildcard_indices.push(i),
SelectorKind::Element => {
for tag in &sel.tags {
by_tag.entry(tag.clone()).or_default().push(i);
}
}
}
}
if !wildcard_indices.is_empty() {
for list in by_tag.values_mut() {
*list = merge_sorted_asc(list, &wildcard_indices);
}
}
CompiledDispatch {
rules: compiled,
by_tag,
wildcard_indices,
text_indices,
comment_indices,
}
}
pub(crate) fn get_dispatch_indices<'a>(
dispatch: &'a CompiledDispatch,
node: &DomNode,
) -> &'a [usize] {
match node.node_type() {
DomNodeType::Text => &dispatch.text_indices,
DomNodeType::Comment => &dispatch.comment_indices,
DomNodeType::Element => dispatch
.by_tag
.get(&node.node_name())
.unwrap_or(&dispatch.wildcard_indices),
_ => &[],
}
}
pub struct CompiledOverlayRules {
pub(crate) dispatch: Rc<CompiledDispatch>,
pub(crate) rules: Vec<DomImportRule>,
}
impl PartialEq for CompiledOverlayRules {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
#[derive(Clone)]
pub enum DomImportRuleEntry {
Rule(DomImportRule),
Overlay(Rc<CompiledOverlayRules>),
}
impl From<DomImportRule> for DomImportRuleEntry {
fn from(rule: DomImportRule) -> Self {
Self::Rule(rule)
}
}
impl From<Rc<CompiledOverlayRules>> for DomImportRuleEntry {
fn from(overlay: Rc<CompiledOverlayRules>) -> Self {
Self::Overlay(overlay)
}
}
pub(crate) fn flatten_rule_entries(entries: &[DomImportRuleEntry]) -> Vec<DomImportRule> {
let mut out = Vec::new();
for entry in entries {
match entry {
DomImportRuleEntry::Rule(rule) => out.push(rule.clone()),
DomImportRuleEntry::Overlay(overlay) => out.extend(overlay.rules.iter().cloned()),
}
}
out
}
pub fn define_overlay_rules(entries: &[DomImportRuleEntry]) -> Rc<CompiledOverlayRules> {
let rules = flatten_rule_entries(entries);
Rc::new(CompiledOverlayRules {
dispatch: Rc::new(compile_import_rules(&rules)),
rules,
})
}