Skip to content

Commit f2a178d

Browse files
committed
parser: strip trailing space on the line after -%} even if the space doesn't end with \n
1 parent 4b53e6a commit f2a178d

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

parser/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type scanner struct {
7272
rewind bool
7373
}
7474

75-
var tailOfLine = regexp.MustCompile(`^[[:blank:]]*\r?\n`)
75+
var tailOfLine = regexp.MustCompile(`^[[:blank:]]*(?:\r*\n)?`)
7676
var prevBlank = regexp.MustCompile(`[[:blank:]]+$`)
7777

7878
func newScanner(r io.Reader, filePath string) *scanner {

parser/scanner_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestScannerEndTagWithMinus(t *testing.T) {
2222
[]tt{
2323
{ID: tagName, Value: "foo"},
2424
{ID: tagContents, Value: "baz aaa"},
25-
{ID: text, Value: " awer"},
25+
{ID: text, Value: "awer"},
2626
{ID: tagName, Value: "aa="},
2727
{ID: tagContents, Value: ""},
2828
})
@@ -34,13 +34,14 @@ func TestScannerEndTagWithMinus(t *testing.T) {
3434
{ID: tagName, Value: "aa="},
3535
{ID: tagContents, Value: "-%"},
3636
})
37-
testScannerSuccess(t, "{%foo -%} \t\r\n\tawer{%aa= - %}",
37+
testScannerSuccess(t, "{%foo -%} \t\r\r\n\n\tawer{%aa= - %} \nbb",
3838
[]tt{
3939
{ID: tagName, Value: "foo"},
4040
{ID: tagContents, Value: ""},
41-
{ID: text, Value: "\tawer"},
41+
{ID: text, Value: "\n\tawer"},
4242
{ID: tagName, Value: "aa="},
4343
{ID: tagContents, Value: "-"},
44+
{ID: text, Value: " \nbb"},
4445
})
4546
testScannerSuccess(t, "{%foo-%}\n awer{%bar -%}\n",
4647
[]tt{
@@ -63,11 +64,13 @@ func TestScannerEndTagWithMinus(t *testing.T) {
6364
}
6465

6566
func TestScannerBeginTagWithMinus(t *testing.T) {
66-
testScannerSuccess(t, "x\n {%-foo%}",
67+
testScannerSuccess(t, "x\n {%-foo%} {%- bar%}",
6768
[]tt{
6869
{ID: text, Value: "x\n"},
6970
{ID: tagName, Value: "foo"},
7071
{ID: tagContents, Value: ""},
72+
{ID: tagName, Value: "bar"},
73+
{ID: tagContents, Value: ""},
7174
})
7275
testScannerSuccess(t, "{%- foo%}",
7376
[]tt{
@@ -88,21 +91,22 @@ func TestScannerBeginTagWithMinus(t *testing.T) {
8891
{ID: tagContents, Value: "baz aaa"},
8992
{ID: text, Value: " awer {- % aa= %}"},
9093
})
91-
testScannerSuccess(t, "{%-foo baz aaa%} awer {-{%- aa= xxx%}",
94+
testScannerSuccess(t, "\n\n {%- foo baz aaa -%} awer {-{%- aa= xxx%}",
9295
[]tt{
96+
{ID: text, Value: "\n\n"},
9397
{ID: tagName, Value: "foo"},
9498
{ID: tagContents, Value: "baz aaa"},
95-
{ID: text, Value: " awer {-"},
99+
{ID: text, Value: "awer {-"},
96100
{ID: tagName, Value: "aa="},
97101
{ID: tagContents, Value: "xxx"},
98102
})
99-
testScannerSuccess(t, "{%-foo%}{%bar%} \n {%baz%}",
103+
testScannerSuccess(t, "{%-foo%}{%bar%} \n {%-baz%}",
100104
[]tt{
101105
{ID: tagName, Value: "foo"},
102106
{ID: tagContents, Value: ""},
103107
{ID: tagName, Value: "bar"},
104108
{ID: tagContents, Value: ""},
105-
{ID: text, Value: " \n "},
109+
{ID: text, Value: " \n"},
106110
{ID: tagName, Value: "baz"},
107111
{ID: tagContents, Value: ""},
108112
})

testdata/templates/integration.qtpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ It should contains all the quicktemplate stuff.
106106

107107
Strip leading and trailing space:
108108
{%- for i := 0; i < 4; i++ -%}
109-
x += {%d i %}
109+
x += {%d i -%} + {%-d i+1 %}
110110
{%- endfor -%}
111111
end
112112

testdata/templates/integration.qtpl.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/templates/integration.qtpl.out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ It should contains all the quicktemplate stuff.
162162

163163
Strip leading and trailing space:
164164
{%- for i := 0; i < 4; i++ -%}
165-
x += {%d i %}
165+
x += {%d i -%} + {%-d i+1 %}
166166
{%- endfor -%}
167167
end
168168

@@ -195,10 +195,10 @@ type integrationPage struct {
195195

196196

197197
Strip leading and trailing space:
198-
x += 0
199-
x += 1
200-
x += 2
201-
x += 3
198+
x += 0+1
199+
x += 1+2
200+
x += 2+3
201+
x += 3+4
202202
end
203203

204204
tail of the func

0 commit comments

Comments
 (0)