Skip to content

Commit b3ffca1

Browse files
authored
Mustachio: One tweak to whitespace trimming (#2555)
1 parent 217c584 commit b3ffca1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/src/mustachio/parser.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ class MustachioParser {
115115
}
116116
} else {
117117
assert(result.type == _TagParseResultType.parsedTag);
118-
trimTextRight();
118+
if (result.node is Section) {
119+
// Trim the right off of the preceding text node, only if a Section
120+
// was just parsed. For other tags, like Variables or Partials,
121+
// the whitespace may be important, as the tag itself will be
122+
// rendered as text.
123+
trimTextRight();
124+
}
119125
addTextNode(textStartIndex, textEndIndex);
120126
children.add(result.node);
121127
textStartIndex = _index;

test/mustachio/renderer_test.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ void main() {
385385
expect(renderBar(bar, barTemplate), equals('Text Partial hello'));
386386
});
387387

388-
test('Parser removes whitespace preceding a tag on its own line', () async {
388+
test('Parser removes whitespace preceding a Section tag on its own line',
389+
() async {
389390
var fooTemplateFile = getFile('/project/foo.mustache')
390391
..writeAsStringSync('''
391392
<ol>
@@ -405,6 +406,25 @@ void main() {
405406
'''));
406407
});
407408

409+
test('Parser does not remove whitespace preceding a non-Section tag',
410+
() async {
411+
var fooTemplateFile = getFile('/project/foo.mustache')
412+
..writeAsStringSync('''
413+
Hello
414+
{{ #b1 }}
415+
{{ s1 }}
416+
{{ /b1 }}
417+
''');
418+
var fooTemplate = await Template.parse(fooTemplateFile);
419+
var foo = Foo()
420+
..b1 = true
421+
..s1 = 'World';
422+
expect(renderFoo(foo, fooTemplate), equals('''
423+
Hello
424+
World
425+
'''));
426+
});
427+
408428
test('Renderer throws when it cannot resolve a variable key', () async {
409429
var fooTemplateFile = getFile('/project/foo.mustache')
410430
..writeAsStringSync('Text {{s2}}');

0 commit comments

Comments
 (0)