diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 93192c9..adb17c5 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -242,12 +242,12 @@ impl<'a> DustRenderer<'a> { fn new_breadcrumbs_partial<'b>( &'b self, - maybe_breadcrumbs: &'b Vec>, - explicit_context_maybe_breadcrumbs: &'a Vec>, + breadcrumbs: &'b Vec>, + explicit_context_breadcrumbs: &'a Vec>, injected_context: Option<&'b dyn IntoContextElement>, explicit_context: &Option>, - ) { - /* + ) -> Option>> { + // If none of the additional contexts are present, return None // to signal that the original breadcrumbs should be used // rather than incurring a copy here. @@ -258,11 +258,10 @@ impl<'a> DustRenderer<'a> { // If there is an explicit context, then drop all the current // context - let mut parent = match explicit_context { - Some(_) => None, - None => maybe_breadcrumbs, + let mut new_stack = match explicit_context { + Some(_) => Vec::with_capacity(3), + None => breadcrumbs.clone(), }; - let mut new_nodes: Vec = Vec::new(); injected_context.map(|ctx| { // Special case: when there is no explicit context, the @@ -271,32 +270,27 @@ impl<'a> DustRenderer<'a> { // added after the current context but before the explicit // context. match explicit_context { - None => { - let (new_parent, passed_nodes) = - Self::split_tree_at_predicate(parent, |b| b.get_ice().is_pseudo_element()); - parent = new_parent; - new_nodes.extend(passed_nodes.iter().map(|b| b.get_element().clone())); - } - _ => new_nodes.push(BreadcrumbTreeElement::from_borrowed(ctx)), + None => new_stack.insert(Self::get_index_of_first_non_pseudo_element(&new_stack).unwrap_or(0), BreadcrumbTreeElement::from_borrowed(ctx)), + _ => new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx)) } }); + explicit_context.as_ref().map(|path| { // TODO: should resolving the value here use // explicit_context_maybe_breadcrumbs or // maybe_breadcrumbs? - walk_path(explicit_context_maybe_breadcrumbs, &path.keys) - .map(|ice| ice.into_context_element(self, maybe_breadcrumbs)) + walk_path(explicit_context_breadcrumbs, &path.keys) + .map(|ice| ice.into_context_element(self, breadcrumbs)) .ok() .flatten() .map(|val| { if val.get_context_element_reference().is_truthy() { - new_nodes.push(std::convert::From::from(val)); + new_stack.push(std::convert::From::from(val)); } }); }); - Some((parent, new_nodes))*/ - todo!() + Some(new_stack) } fn preprocess_filters(filters: &Vec) -> Vec { @@ -313,6 +307,13 @@ impl<'a> DustRenderer<'a> { } final_filters } + + fn get_index_of_first_non_pseudo_element<'b>(breadcrumbs: &'b Vec>) -> Option + { + breadcrumbs + .iter() + .rposition(|b| std::borrow::Borrow::::borrow(b).is_pseudo_element()) + } } struct BlockContext<'a> {