|
122 | 122 | function parseTemplate (template, tags) {
|
123 | 123 | if (!template)
|
124 | 124 | return [];
|
125 |
| - |
| 125 | + var lineHasNonSpace = false; |
126 | 126 | var sections = []; // Stack to hold section tokens
|
127 | 127 | var tokens = []; // Buffer to hold the tokens
|
128 | 128 | var spaces = []; // Indices of whitespace tokens on the current line
|
|
175 | 175 |
|
176 | 176 | if (isWhitespace(chr)) {
|
177 | 177 | spaces.push(tokens.length);
|
178 |
| - if (!nonSpace) |
179 |
| - indentation += chr; |
| 178 | + indentation += chr; |
180 | 179 | } else {
|
181 | 180 | nonSpace = true;
|
| 181 | + lineHasNonSpace = true; |
| 182 | + indentation += ' '; |
182 | 183 | }
|
183 | 184 |
|
184 | 185 | tokens.push([ 'text', chr, start, start + 1 ]);
|
|
189 | 190 | stripSpace();
|
190 | 191 | indentation = '';
|
191 | 192 | tagIndex = 0;
|
| 193 | + lineHasNonSpace = false; |
192 | 194 | }
|
193 | 195 | }
|
194 | 196 | }
|
|
222 | 224 | throw new Error('Unclosed tag at ' + scanner.pos);
|
223 | 225 |
|
224 | 226 | if (type == '>') {
|
225 |
| - token = [ type, value, start, scanner.pos, indentation, tagIndex ]; |
| 227 | + token = [ type, value, start, scanner.pos, indentation, tagIndex, lineHasNonSpace ]; |
226 | 228 | } else {
|
227 | 229 | token = [ type, value, start, scanner.pos ];
|
228 | 230 | }
|
|
610 | 612 | return this.renderTokens(token[4], context, partials, originalTemplate);
|
611 | 613 | };
|
612 | 614 |
|
613 |
| - Writer.prototype.indentPartial = function indentPartial (partial, indentation) { |
| 615 | + Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) { |
614 | 616 | var filteredIndentation = indentation.replace(/[^ \t]/g, '');
|
615 | 617 | var partialByNl = partial.split('\n');
|
616 | 618 | for (var i = 0; i < partialByNl.length; i++) {
|
617 |
| - if (partialByNl[i].length) { |
| 619 | + if (partialByNl[i].length && (i > 0 || !lineHasNonSpace)) { |
618 | 620 | partialByNl[i] = filteredIndentation + partialByNl[i];
|
619 | 621 | }
|
620 | 622 | }
|
|
626 | 628 |
|
627 | 629 | var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
|
628 | 630 | if (value != null) {
|
| 631 | + var lineHasNonSpace = token[6]; |
629 | 632 | var tagIndex = token[5];
|
630 | 633 | var indentation = token[4];
|
631 | 634 | var indentedValue = value;
|
632 | 635 | if (tagIndex == 0 && indentation) {
|
633 |
| - indentedValue = this.indentPartial(value, indentation); |
| 636 | + indentedValue = this.indentPartial(value, indentation, lineHasNonSpace); |
634 | 637 | }
|
635 | 638 | return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue);
|
636 | 639 | }
|
|
0 commit comments