Add a special case to not render anything if the method parameter to the math helper is a template to match the official DustJS implementation.

This commit is contained in:
Tom Alexander
2020-06-13 22:03:48 -04:00
parent b897656cef
commit 2c5c2d239c
2 changed files with 57 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ use crate::parser::Filter;
use crate::parser::OwnedLiteral;
use crate::parser::PartialNameElement;
use crate::parser::Path;
use crate::parser::RValue;
use crate::parser::Special;
use crate::parser::Template;
use crate::parser::TemplateElement;
@@ -675,8 +676,13 @@ impl<'a> DustRenderer<'a> {
// Generate a ParametersContext with the result of the math operation as key
let converted_value: BreadcrumbTreeElement<'_> = calculated_value.into();
let calculated_param_map: HashMap<&str, Option<BreadcrumbTreeElement<'_>>> =
vec![("key", Some(converted_value))].into_iter().collect();
let dummy_rvalue = RValue::RVLiteral(OwnedLiteral::LPositiveInteger(0));
let calculated_param_map: HashMap<
&str,
(&RValue<'_>, Option<BreadcrumbTreeElement<'_>>),
> = vec![("key", (&dummy_rvalue, Some(converted_value)))]
.into_iter()
.collect();
let calculated_context =
ParametersContext::from_values(None, calculated_param_map);
// calculate are_any_checks_true
@@ -982,6 +988,21 @@ impl<'a> DustRenderer<'a> {
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
math_parameters: &'a ParametersContext<'a>,
) -> Option<IceResult<'a>> {
// Special case: if method is a template then do not render
// anything. This is to match the behavior of dustjs even
// though it works fine.
match math_parameters.get_original_rvalue("method") {
Some(RValue::RVTemplate(template)) => {
if template.iter().any(|pne| match pne {
PartialNameElement::PNReference { .. } => true,
PartialNameElement::PNSpan { .. } => false,
}) {
return None;
}
}
_ => (),
}
let method = match self.tap(breadcrumbs, math_parameters, "method") {
None | Some(Err(_)) => return None,
Some(Ok(ice_result)) => ice_result,