Skip to content

Commit

Permalink
fix: maintain whitespace around templated values (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
noaoh authored Sep 27, 2023
1 parent 4deabea commit 92d3cd1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ func stripNonCriticalElementWhitespace(input []parser.Node) (output []parser.Nod
output = append(output, curr)
continue
}

_, prevIsStringExpr := prev.(parser.StringExpression)
_, nextIsStringExpr := next.(parser.StringExpression)
if prevIsStringExpr || nextIsStringExpr {
// Allow whitespace that comes before or after a template expression.
output = append(output, curr)
continue
}
}
return
}
Expand Down
5 changes: 5 additions & 0 deletions generator/test-text-whitespace/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func TestTextWhitespace(t *testing.T) {
input: WhiteSpaceAroundValues(),
expected: WhiteSpaceAroundValuesExpected,
},
{
name: "whitespace around templated values is maintained",
input: WhiteSpaceAroundTemplatedValues("templ", "allows whitespace around templated values."),
expected: WhiteSpaceAroundTemplatedValuesExpected,
},
} {
w := new(strings.Builder)
err := test.input.Render(context.Background(), w)
Expand Down
6 changes: 6 additions & 0 deletions generator/test-text-whitespace/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ templ WhiteSpaceAroundValues() {
}

const WhiteSpaceAroundValuesExpected = `<p>templ allows strings to be included in sentences.</p>`

const WhiteSpaceAroundTemplatedValuesExpected = `<div>templ allows whitespace around templated values.</div>`

templ WhiteSpaceAroundTemplatedValues(prefix, statement string) {
<div>{prefix} {statement}</div>
}
44 changes: 44 additions & 0 deletions generator/test-text-whitespace/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92d3cd1

Please sign in to comment.