Greatly expand the explicit_context_setting test.

Turns out explicit context setting works in a lot more places than the official dustjs pages indicate. Also some things don't make much sense (like setting it on a block works but not on an inline partial).
This commit is contained in:
Tom Alexander 2020-05-24 18:16:16 -04:00
parent e27ab16e06
commit 1152ff9974
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
10 changed files with 211 additions and 10 deletions

View File

@ -1 +1,40 @@
It seems $idx and $len do not survive through an explicit context setting, which will work perfectly with my injected-context architecture.
$idx and $len
-------------
$idx and $len do not survive through an explicit context setting, which will work perfectly with my injected-context architecture.
You can use $idx and $len as your explicit context, but as scalar values I do not think there is a way to access them anyway.
Exists and Not-Exists
=====================
Looks like you can exlicitly set a context with exists and not-exists tags too. This works out well in the parser because I am using the same code for those blocks.
Partials
--------
Explicitly setting a context in a partial also works. The explicit context takes priority over the parameters in the partial tag.
This works for both regular named partials and quoted partials.
Helpers
-------
Explicitly setting a context in a helper works too.
Blocks and Inline Partials
--------------------------
Explicitly setting a context on an inline partial does not work, but it does not error out either, so I need to add support for this in the parser.
Explicitly setting a context on a block does work.
References
----------
Explicitly setting a context does not work on a reference, but it has revealed that invalid dust is rendered verbatim. I'm going to leave that commented out until I can address that in a later branch.
Paths
-----
Explicit contexts do support deep paths

View File

@ -0,0 +1 @@
{+pet_line}BLOCK {$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}{/pet_line}

View File

@ -0,0 +1 @@
{+pet_line:explicit}BLOCK {$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}{/pet_line}

View File

@ -19,5 +19,10 @@
],
"explicit": {
"pet": "cat"
},
"deep_explicit": {
"explicit": {
"pet": "cat"
}
}
}

View File

@ -1,4 +1,6 @@
{! First we do it without explicit context setting which results in being able to read some_global but not pet !}
Section Regular{~n}
==============={~n}
{#loop}
{#person}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
@ -6,8 +8,152 @@
{/loop}
{! Then we do it with explicit context setting which should result in being able ot read pet but not some_global !}
Section Explicit{~n}
================{~n}
{#loop}
{#person:explicit}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
{! What happens if we try to use an explicit context with an exists block instead of a section !}
Exists Regular{~n}
=============={~n}
{#loop}
{?person}
{$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
Exists Explicit{~n}
==============={~n}
{#loop}
{?person:explicit}
{$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
{! Can you explicitly set the context for a partial? !}
Partial Regular{~n}
==============={~n}
{#loop}
{>other/}
{/loop}
Partial Explicit{~n}
================{~n}
{#loop}
{>other:explicit/}
{/loop}
Quoted Partial Explicit{~n}
======================={~n}
{#loop}
{>"other":explicit/}
{/loop}
Partial Regular with parameters{~n}
==============================={~n}
{#loop}
{>other pet="rabbit"/}
{/loop}
Partial Explicit with parameters{~n}
================================{~n}
{#loop}
{>other:explicit pet="rabbit"/}
{/loop}
{! Can you explicitly set the context for a helper? !}
Helper Regular{~n}
=============={~n}
{#loop}
{@eq key="foo" value="foo"}
{$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}
{/eq}
{/loop}
Helper Explicit{~n}
==============={~n}
{#loop}
{@eq:explicit key="foo" value="foo"}
{$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}
{/eq}
{/loop}
{! Can you explicitly set the context for inline partials or blocks? !}
Block Regular{~n}
============={~n}
{#loop}
{>default/}
{/loop}
Inline Partial Regular{~n}
======================{~n}
{#loop}
{>override/}
{/loop}
Block Explicit{~n}
=============={~n}
{#loop}
{>default_explicit/}
{/loop}
Inline Partial Explicit{~n}
======================={~n}
{#loop}
{>override_explicit/}
{/loop}
Inline Partial and Block Explicit{~n}
================================={~n}
{#loop}
{>override_double_explicit/}
{/loop}
{! Can you explicitly set the context for references? !}
{! Commented out until I add support for rendering invalid dust verbatim
Reference Regular{~n}
================={~n}
{.}{~n}
Reference Explicit{~n}
=================={~n}
{.:some_global}{~n}
!}
{! Can you explicitly set the context with a path? !}
Path Regular{~n}
============{~n}
{#loop}
{#person}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
Path Explicit{~n}
============={~n}
{#loop}
{#person:deep_explicit.explicit}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
{! Can you explicitly set the context to a variable? !}
Variable Regular{~n}
================{~n}
{#loop}
{#person}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}
Variable Explicit{~n}
================={~n}
{#loop}
{#person:$idx}
{$idx}: {name} has a pet {pet} but not a {some_global}{~n}
{/person}
{/loop}

View File

@ -0,0 +1 @@
{$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}

View File

@ -0,0 +1,2 @@
{>default/}
{<pet_line}INLINE PARTIAL {$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}{/pet_line}

View File

@ -0,0 +1,2 @@
{>default_explicit/}
{<pet_line:explicit}INLINE PARTIAL {$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}{/pet_line}

View File

@ -0,0 +1,2 @@
{>default/}
{<pet_line:explicit}INLINE PARTIAL {$idx}: {person.name} has a pet {pet} but not a {some_global}{~n}{/pet_line}

View File

@ -341,15 +341,17 @@ where
F: Fn(Container<'a>) -> DustTag<'a>,
{
move |i: &'a str| {
let (i, (opening_name, inner, maybe_else, _closing_name)) = verify(
tuple((
delimited(tag(open_matcher), path, tag("}")),
opt(body),
opt(preceded(tag("{:else}"), opt(body))),
delimited(tag("{/"), path, tag("}")),
)),
|(open, _inn, _maybe_else, close)| open == close,
)(i)?;
let (i, (opening_name, maybe_explicit_context, inner, maybe_else, _closing_name)) =
verify(
tuple((
preceded(tag(open_matcher), path),
terminated(opt(preceded(tag(":"), path)), tag("}")),
opt(body),
opt(preceded(tag("{:else}"), opt(body))),
delimited(tag("{/"), path, tag("}")),
)),
|(open, _maybe_explicit, _inn, _maybe_else, close)| open == close,
)(i)?;
Ok((
i,