Fix #24: renderString shrinks multiple newlines to just one
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 25a662a..879f844 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
* Remove MustacheFormatException
* Allow templates to specify default delimiters. Thanks to Joris Hermans.
+* Fix #24: renderString shrinks multiple newlines to just one (Thanks to John Ryan for the repro).
## 0.2.4
diff --git a/lib/src/parser.dart b/lib/src/parser.dart
index 779afaa..10f8bd7 100644
--- a/lib/src/parser.dart
+++ b/lib/src/parser.dart
@@ -199,7 +199,7 @@
// Handle standalone tags and indented partials.
//
// A "standalone tag" in the spec is a tag one a line where the line only
- // contains whitespace. During rendering the whitespace is ommitted.
+ // contains whitespace. During rendering the whitespace is omitted.
// Standalone partials also indent their content to match the tag during
// rendering.
@@ -208,6 +208,10 @@
//
// Where lineEnd can also mean start/end of the source.
void _parseLine() {
+ // If first token is a newline append it.
+ var t = _peek();
+ if (t != null && t.type == TokenType.lineEnd) _appendTextToken(t);
+
// Continue parsing standalone lines until we find one than isn't a
// standalone line.
while (_peek() != null) {
diff --git a/test/parser_test.dart b/test/parser_test.dart
index 9600042..eb1231e 100644
--- a/test/parser_test.dart
+++ b/test/parser_test.dart
@@ -208,6 +208,29 @@
expectNodes(nodes[0].children, [new TextNode('def\n', 8, 12)]);
});
+ test('parse variable newline', () {
+ var source = 'abc\n\n{{foo}}def';
+ var parser = new Parser(source, 'foo', '{{ }}', lenient: false);
+ var nodes = parser.parse();
+ expectNodes(nodes, [
+ new TextNode('abc\n\n', 0, 5),
+ new VariableNode('foo', 5, 12, escape: true),
+ new TextNode('def', 12, 15)
+ ]);
+ });
+
+ test('parse section standalone tag whitespace v2', () {
+ var source = 'abc\n\n{{#foo}}\ndef\n{{/foo}}\nghi';
+ var parser = new Parser(source, 'foo', '{{ }}', lenient: false);
+ var nodes = parser.parse();
+ expectNodes(nodes, [
+ new TextNode('abc\n\n', 0, 5),
+ new SectionNode('foo', 5, 13, '{{ }}'),
+ new TextNode('ghi', 27, 30)
+ ]);
+ expectNodes(nodes[1].children, [new TextNode('def\n', 14, 18)]);
+ });
+
test('parse whitespace', () {
var source = 'abc\n ';
var parser = new Parser(source, 'foo', '{{ }}', lenient: false);