Skip to content

Commit

Permalink
Mustachio: One tweak to whitespace trimming (#2555)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins committed Mar 3, 2021
1 parent 217c584 commit b3ffca1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/src/mustachio/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ class MustachioParser {
}
} else {
assert(result.type == _TagParseResultType.parsedTag);
trimTextRight();
if (result.node is Section) {
// Trim the right off of the preceding text node, only if a Section
// was just parsed. For other tags, like Variables or Partials,
// the whitespace may be important, as the tag itself will be
// rendered as text.
trimTextRight();
}
addTextNode(textStartIndex, textEndIndex);
children.add(result.node);
textStartIndex = _index;
Expand Down
22 changes: 21 additions & 1 deletion test/mustachio/renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ void main() {
expect(renderBar(bar, barTemplate), equals('Text Partial hello'));
});

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

test('Parser does not remove whitespace preceding a non-Section tag',
() async {
var fooTemplateFile = getFile('/project/foo.mustache')
..writeAsStringSync('''
Hello
{{ #b1 }}
{{ s1 }}
{{ /b1 }}
''');
var fooTemplate = await Template.parse(fooTemplateFile);
var foo = Foo()
..b1 = true
..s1 = 'World';
expect(renderFoo(foo, fooTemplate), equals('''
Hello
World
'''));
});

test('Renderer throws when it cannot resolve a variable key', () async {
var fooTemplateFile = getFile('/project/foo.mustache')
..writeAsStringSync('Text {{s2}}');
Expand Down

0 comments on commit b3ffca1

Please sign in to comment.