Add test for priorities while looping and split the new_breadcrumbs function into two separate new_breadcrumbs functions because sections and partials have different orderings.

master
Tom Alexander 4 years ago
parent 108cffb771
commit 79f52ecdee
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -4,6 +4,8 @@ Partials: Explicit context takes priority over parameters
Sections: New context takes priority, then parameters, then explicit
Sections with loops: Loop variables ($idx and $len) take priority over parameters and explicit context
$idx and $len
-------------

@ -326,3 +326,34 @@ Section vs Partial priority{~n}
{pet}{~n}
{/some_global}
{>priority:explicit pet="snake"/}
Section vs Partial priority Failed Walk{~n}
======================================={~n}
{#doesnotexist:explicit pet="snake"}
MAIN {pet}{~n}
{:else}
ELSE {pet}{~n}
{/doesnotexist}
{>priority:explicit pet="snake"/}
Section Loop set $idx as a parameter{~n}
===================================={~n}
{#loop $idx="7"}
$idx is {$idx}{~n}
$len is {$len}{~n}
{/loop}
Section Loop set $idx in explicit context{~n}
========================================={~n}
{#loop:has_idx}
$idx is {$idx}{~n}
$len is {$len}{~n}
{/loop}
Section Loop set $idx in explicit context and parameter{~n}
======================================================={~n}
{#loop:has_idx $idx="7"}
$idx is {$idx}{~n}
$len is {$len}{~n}
{/loop}

@ -785,6 +785,80 @@ impl<'a> DustRenderer<'a> {
new_context_element.map(|ctx| new_stack.push(ctx));
Some(new_stack)
}
fn new_breadcrumbs_section<'b>(
breadcrumbs: &'b Vec<&'b dyn ContextElement>,
index_context: Option<&'b dyn ContextElement>,
injected_context: Option<&'b dyn ContextElement>,
explicit_context: &Option<Path<'b>>,
new_context_element: Option<&'b dyn ContextElement>,
) -> Option<Vec<&'b dyn ContextElement>> {
// 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.
match (
index_context,
injected_context,
explicit_context,
new_context_element,
) {
(None, None, None, None) => return None,
_ => (),
}
let mut new_stack = match explicit_context {
Some(_) => Vec::with_capacity(4),
None => breadcrumbs.clone(),
};
explicit_context.as_ref().map(|path| {
walk_path(breadcrumbs, &path.keys).map(|val| {
if val.is_truthy() {
new_stack.push(val)
}
});
});
injected_context.map(|ctx| new_stack.push(ctx));
new_context_element.map(|ctx| new_stack.push(ctx));
index_context.map(|ctx| new_stack.push(ctx));
Some(new_stack)
}
fn new_breadcrumbs_partial<'b>(
breadcrumbs: &'b Vec<&'b dyn ContextElement>,
explicit_context_breadcrumbs: &'b Vec<&'b dyn ContextElement>,
injected_context: Option<&'b dyn ContextElement>,
explicit_context: &Option<Path<'b>>,
) -> Option<Vec<&'b dyn ContextElement>> {
// 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.
match (injected_context, explicit_context) {
(None, None) => return None,
_ => (),
};
let mut new_stack = match explicit_context {
Some(_) => Vec::with_capacity(3),
None => breadcrumbs.clone(),
};
injected_context.map(|ctx| {
// Special case: when there is no explicit context, the
// injected context gets inserted 1 spot behind the
// current context. Otherwise, the injected context gets
// added after the current context but before the explicit
// context.
match explicit_context {
None => new_stack.insert(std::cmp::max(new_stack.len() - 1, 0), ctx),
_ => new_stack.push(ctx),
}
});
explicit_context.as_ref().map(|path| {
walk_path(explicit_context_breadcrumbs, &path.keys).map(|val| {
if val.is_truthy() {
new_stack.push(val)
}
});
});
Some(new_stack)
}
}
struct BlockContext<'a> {

Loading…
Cancel
Save