@@ -48,6 +48,7 @@ class MustachioParser {
48
48
List <MustachioNode > _parseBlock ({String /*?*/ sectionKey}) {
49
49
var children = < MustachioNode > [];
50
50
var textStartIndex = _index;
51
+ var textEndIndex = _index;
51
52
52
53
void addTextNode (int startIndex, int endIndex) {
53
54
if (endIndex > startIndex) {
@@ -56,13 +57,40 @@ class MustachioParser {
56
57
}
57
58
}
58
59
60
+ /// Trims [textEndIndex] if it marks the end of a blank line.
61
+ ///
62
+ /// [textEndIndex] is reset back to the newline immediately preceding any
63
+ /// whitespace preceding [textEndIndex] .
64
+ void trimTextRight () {
65
+ var newEndIndex = textEndIndex;
66
+ while (true ) {
67
+ if (newEndIndex == textStartIndex) {
68
+ // We walked all the way to [textStartIndex] without finding a
69
+ // newline; for example in `{{a}} {{b}}` we don't want to trim the
70
+ // singular space.
71
+ return ;
72
+ }
73
+ var ch = template.codeUnitAt (newEndIndex - 1 );
74
+ if (ch == $space || ch == $tab) {
75
+ newEndIndex-- ;
76
+ } else if (ch == $cr || ch == $lf) {
77
+ textEndIndex = newEndIndex - 1 ;
78
+ return ;
79
+ } else {
80
+ // We walked back to some other character; [textEndIndex] does not
81
+ // mark the end of a blank line.
82
+ return ;
83
+ }
84
+ }
85
+ }
86
+
59
87
while (true ) {
60
88
if (_nextAtEnd) {
61
89
addTextNode (textStartIndex, _templateLength);
62
90
break ;
63
91
}
64
92
if (_thisChar == $lbrace && _nextChar == $lbrace) {
65
- var textEndIndex = _index;
93
+ textEndIndex = _index;
66
94
_index += 2 ;
67
95
var result = _parseTag ();
68
96
if (result == _TagParseResult .endOfFile) {
@@ -77,6 +105,7 @@ class MustachioParser {
77
105
continue ;
78
106
} else if (result.type == _TagParseResultType .parsedEndTag) {
79
107
if (sectionKey != null && sectionKey == result.endTagKey) {
108
+ trimTextRight ();
80
109
addTextNode (textStartIndex, textEndIndex);
81
110
break ;
82
111
} else {
@@ -85,6 +114,8 @@ class MustachioParser {
85
114
continue ;
86
115
}
87
116
} else {
117
+ assert (result.type == _TagParseResultType .parsedTag);
118
+ trimTextRight ();
88
119
addTextNode (textStartIndex, textEndIndex);
89
120
children.add (result.node);
90
121
textStartIndex = _index;
0 commit comments