From 25eb9b10a0c696963135f07ca8c986143c7b6c88 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 25 May 2020 19:22:20 -0400 Subject: [PATCH] I did it backwards, I needed to use the explicit context from the block, not the inline partial. --- .../explicit_context_setting/README.md | 2 +- src/renderer/inline_partial_tree.rs | 50 ++++--------------- src/renderer/renderer.rs | 24 ++++----- 3 files changed, 20 insertions(+), 56 deletions(-) diff --git a/js/test_cases/explicit_context_setting/README.md b/js/test_cases/explicit_context_setting/README.md index d0ea503..ffb7a36 100644 --- a/js/test_cases/explicit_context_setting/README.md +++ b/js/test_cases/explicit_context_setting/README.md @@ -31,7 +31,7 @@ Explicitly setting a context on an inline partial does not work, but it does not Explicitly setting a context on a block does work. -The explicit context for inline partials is evaluated in the context for that template file containing the block, not the inline partial (so, whatever the context is when we invoke the partial containing the block). +The explicit context for blocks is evaluated in the context for that template file containing the block, not the inline partial (so, whatever the context is when we invoke the partial containing the block). References ---------- diff --git a/src/renderer/inline_partial_tree.rs b/src/renderer/inline_partial_tree.rs index 0dabb39..d0a2020 100644 --- a/src/renderer/inline_partial_tree.rs +++ b/src/renderer/inline_partial_tree.rs @@ -1,23 +1,18 @@ use crate::parser::Body; use crate::parser::DustTag; use crate::parser::Template; -use crate::parser::{Path, TemplateElement}; +use crate::parser::TemplateElement; use std::collections::HashMap; -pub struct InlinePartialTreeValue<'a> { - explicit_context: &'a Option>, - body: &'a Option>, -} - pub struct InlinePartialTreeElement<'a> { parent: Option<&'a InlinePartialTreeElement<'a>>, - blocks: HashMap<&'a str, InlinePartialTreeValue<'a>>, + blocks: HashMap<&'a str, &'a Option>>, } impl<'a> InlinePartialTreeElement<'a> { pub fn new( parent: Option<&'a InlinePartialTreeElement<'a>>, - blocks: HashMap<&'a str, InlinePartialTreeValue<'a>>, + blocks: HashMap<&'a str, &'a Option>>, ) -> InlinePartialTreeElement<'a> { InlinePartialTreeElement { parent: parent, @@ -25,40 +20,21 @@ impl<'a> InlinePartialTreeElement<'a> { } } - fn get_value(&self, name: &str) -> Option<&InlinePartialTreeValue<'a>> { + pub fn get_block(&self, name: &str) -> Option<&'a Option>> { match self.blocks.get(name) { None => match self.parent { None => None, - Some(parent_tree_element) => parent_tree_element.get_value(name), + Some(parent_tree_element) => parent_tree_element.get_block(name), }, Some(interior) => Some(interior), } } - - /// Recursively get the body of a block by name - /// - /// Returning a double-option because there is a functional - /// difference between not finding the block and not having a - /// body. - pub fn get_block(&self, name: &str) -> Option<&'a Option>> { - self.get_value(name).map(|interior| interior.body) - } - - /// Recursively get the explicit from the inline partial by name - /// - /// Returning a double-option because there is a functional - /// difference between not finding the block and not having a - /// body. - pub fn get_explicit_context(&self, name: &str) -> Option<&Option>> { - self.get_value(name) - .map(|interior| interior.explicit_context) - } } pub fn extract_inline_partials<'a>( template: &'a Template<'a>, -) -> HashMap<&'a str, InlinePartialTreeValue<'a>> { - let mut blocks: HashMap<&'a str, InlinePartialTreeValue<'a>> = HashMap::new(); +) -> HashMap<&'a str, &'a Option>> { + let mut blocks: HashMap<&'a str, &'a Option>> = HashMap::new(); extract_inline_partials_from_body(&mut blocks, &template.contents); @@ -66,7 +42,7 @@ pub fn extract_inline_partials<'a>( } fn extract_inline_partials_from_body<'a, 'b>( - blocks: &'b mut HashMap<&'a str, InlinePartialTreeValue<'a>>, + blocks: &'b mut HashMap<&'a str, &'a Option>>, body: &'a Body<'a>, ) { for elem in &body.elements { @@ -81,7 +57,7 @@ fn extract_inline_partials_from_body<'a, 'b>( } fn extract_inline_partials_from_tag<'a, 'b>( - blocks: &'b mut HashMap<&'a str, InlinePartialTreeValue<'a>>, + blocks: &'b mut HashMap<&'a str, &'a Option>>, tag: &'a DustTag, ) { match tag { @@ -121,13 +97,7 @@ fn extract_inline_partials_from_tag<'a, 'b>( } DustTag::DTPartial(..) => (), DustTag::DTInlinePartial(named_block) => { - blocks.insert( - &named_block.name, - InlinePartialTreeValue { - explicit_context: &named_block.explicit_context, - body: &named_block.contents, - }, - ); + blocks.insert(&named_block.name, &named_block.contents); } DustTag::DTBlock(..) => (), DustTag::DTHelperEquals(parameterized_block) => { diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index e41bec0..db1fde2 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -343,27 +343,21 @@ impl<'a> DustRenderer<'a> { return Ok("".to_owned()); } DustTag::DTBlock(named_block) => { - let new_breadcrumbs = blocks - .blocks - .get_explicit_context(named_block.name) - .map(|explicit_context| { - Self::new_breadcrumbs( - breadcrumbs, - blocks.breadcrumbs, - None, - explicit_context, - None, - ) - }) - .flatten(); + let new_breadcrumbs = Self::new_breadcrumbs( + breadcrumbs, + blocks.breadcrumbs, + None, + &named_block.explicit_context, + None, + ); return match blocks.blocks.get_block(named_block.name) { None => self.render_maybe_body( &named_block.contents, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ), - Some(interior) => self.render_maybe_body( - interior, + Some(inline_partial) => self.render_maybe_body( + inline_partial, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ),