Skip to content

Commit

Permalink
Merge pull request #657 from cmrschwarz/fix_611
Browse files Browse the repository at this point in the history
Fix #611
  • Loading branch information
sunng87 authored Jul 14, 2024
2 parents 95a53a8 + cc6ad7e commit 5db326a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl<'reg> Registry<'reg> {
tpl_str.as_ref(),
TemplateOptions {
name: Some(name.to_owned()),
is_partial: false,
prevent_indent: self.prevent_indent,
},
)?;
Expand Down Expand Up @@ -593,6 +594,7 @@ impl<'reg> Registry<'reg> {
TemplateOptions {
name: Some(name.to_owned()),
prevent_indent: self.prevent_indent,
is_partial: false,
},
)
})
Expand Down
18 changes: 16 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Template {
#[derive(Default)]
pub(crate) struct TemplateOptions {
pub(crate) prevent_indent: bool,
pub(crate) is_partial: bool,
pub(crate) name: Option<String>,
}

Expand Down Expand Up @@ -570,9 +571,15 @@ impl Template {
source: &str,
current_span: &Span<'_>,
prevent_indent: bool,
is_partial: bool,
) -> bool {
let with_trailing_newline =
support::str::starts_with_empty_line(&source[current_span.end()..]);
let continuation = &source[current_span.end()..];

let mut with_trailing_newline = support::str::starts_with_empty_line(continuation);

// For full templates, we behave as if there was a trailing newline if we encounter
// the end of input. See #611.
with_trailing_newline |= !is_partial && continuation.is_empty();

if with_trailing_newline {
let with_leading_newline =
Expand Down Expand Up @@ -770,6 +777,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let indent_before_write = trim_line_required && !exp.omit_pre_ws;
Expand Down Expand Up @@ -812,6 +820,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let indent_before_write = trim_line_required && !exp.omit_pre_ws;
Expand Down Expand Up @@ -884,6 +893,7 @@ impl Template {
source,
&span,
prevent_indent,
options.is_partial,
);

// indent for partial expression >
Expand Down Expand Up @@ -919,6 +929,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let mut h = helper_stack.pop_front().unwrap();
Expand Down Expand Up @@ -948,6 +959,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let mut d = decorator_stack.pop_front().unwrap();
Expand Down Expand Up @@ -981,6 +993,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let text = span
Expand All @@ -996,6 +1009,7 @@ impl Template {
source,
&span,
true,
options.is_partial,
);

let text = span
Expand Down
46 changes: 46 additions & 0 deletions tests/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,49 @@ foo
output
);
}

//regression test for #611
#[test]
fn tag_before_eof_becomes_standalone_in_full_template() {
let input = r#"<ul>
{{#each a}}
{{!-- comment --}}
<li>{{this}}</li>
{{/each}}"#;
let output = r#"<ul>
<li>1</li>
<li>2</li>
<li>3</li>
"#;
let hbs = Handlebars::new();

assert_eq!(
hbs.render_template(input, &json!({"a": [1, 2, 3]}))
.unwrap(),
output
);
}

#[test]
fn tag_before_eof_does_not_become_standalone_in_partial() {
let input = r#"{{#*inline "partial"}}
<ul>
{{#each a}}
<li>{{this}}</li>
{{/each}}{{/inline}}
{{> partial}}"#;

let output = r#"
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
"#;
let hbs = Handlebars::new();

assert_eq!(
hbs.render_template(input, &json!({"a": [1, 2, 3]}))
.unwrap(),
output
);
}

0 comments on commit 5db326a

Please sign in to comment.