Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra whitespace added to each when the /each isn't followed by \n #611

Closed
jjcomer opened this issue Sep 27, 2023 · 1 comment · Fixed by #657
Closed

Extra whitespace added to each when the /each isn't followed by \n #611

jjcomer opened this issue Sep 27, 2023 · 1 comment · Fixed by #657
Assignees

Comments

@jjcomer
Copy link

jjcomer commented Sep 27, 2023

We noticed this issues when upgrading to 4.x. If an /each is not followed by a \n the rendered template will contain extra whitespace. This throws off formats which expect consistent whitespace.

Here is a repro:

#[test]
    fn no_newline_for_each() {
        let reg = Registry::new();

        let tpl = r#"<ul>
  {{#each a}}
    {{!-- comment --}}
    <li>{{this}}</li>
  {{/each}}"#;
        assert_eq!(
            r#"<ul>
    <li>0</li>
    <li>1</li>
    <li>2</li>"#,
            reg.render_template(tpl, &json!({"a": [0, 1, 2]})).unwrap()
        );
    }

Using the template:

r#"<ul>
  {{#each a}}
    {{!-- comment --}}
    <li>{{this}}</li>
  {{/each}}"#

Results in:

---- helpers::helper_each::test::no_newline_for_each stdout ----
thread 'helpers::helper_each::test::no_newline_for_each' panicked at 'assertion failed: `(left == right)`
  left: `"<ul>\n    <li>0</li>\n    <li>1</li>\n    <li>2</li>"`,
 right: `"<ul>\n    <li>0</li>\n      <li>1</li>\n      <li>2</li>\n  "`', src/helpers/helper_each.rs:620:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Using the template:

r#"<ul>
  {{#each a}}
    {{!-- comment --}}
    <li>{{this}}</li>
  {{/each}}aa"#

Results in:

---- helpers::helper_each::test::no_newline_for_each stdout ----
thread 'helpers::helper_each::test::no_newline_for_each' panicked at 'assertion failed: `(left == right)`
  left: `"<ul>\n    <li>0</li>\n    <li>1</li>\n    <li>2</li>"`,
 right: `"<ul>\n    <li>0</li>\n      <li>1</li>\n      <li>2</li>\n  aa"`', src/helpers/helper_each.rs:620:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Using the template:

r#"<ul>
  {{#each a}}
    {{!-- comment --}}
    <li>{{this}}</li>
  {{/each}}a
"#;

Results in:

-- helpers::helper_each::test::no_newline_for_each stdout ----
thread 'helpers::helper_each::test::no_newline_for_each' panicked at 'assertion failed: `(left == right)`
  left: `"<ul>\n    <li>0</li>\n    <li>1</li>\n    <li>2</li>"`,
 right: `"<ul>\n    <li>0</li>\n      <li>1</li>\n      <li>2</li>\n  a\n"`', src/helpers/helper_each.rs:621:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Using the template:

r#"<ul>
  {{#each a}}
    {{!-- comment --}}
    <li>{{this}}</li>
  {{/each}}
"#

Results in:

---- helpers::helper_each::test::no_newline_for_each stdout ----
thread 'helpers::helper_each::test::no_newline_for_each' panicked at 'assertion failed: `(left == right)`
  left: `"<ul>\n    <li>0</li>\n    <li>1</li>\n    <li>2</li>"`,
 right: `"<ul>\n    <li>0</li>\n    <li>1</li>\n    <li>2</li>\n"`', src/helpers/helper_each.rs:621:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

As long as the /each is followed by a \n we get predictable whitespace.

@sunng87 sunng87 self-assigned this Sep 28, 2023
@sunng87 sunng87 mentioned this issue Jul 9, 2024
cmrschwarz added a commit to cmrschwarz/handlebars-rust that referenced this issue Jul 13, 2024
@cmrschwarz cmrschwarz mentioned this issue Jul 13, 2024
sunng87 added a commit that referenced this issue Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants