Skip to content

Commit d820d25

Browse files
yotammademphillipj
authored andcommitted
Fix indentation of inline partials (#716)
These changes fixes an indentation bug introduced when partials was made to also be indented like its parent template. When partials are rendered inline, it should not be indented with the amount of indentation that the line already has. Fixes #715 Refs #705
1 parent 13cde04 commit d820d25

File tree

4 files changed

+9025
-13
lines changed

4 files changed

+9025
-13
lines changed

mustache.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
function parseTemplate (template, tags) {
123123
if (!template)
124124
return [];
125-
125+
var lineHasNonSpace = false;
126126
var sections = []; // Stack to hold section tokens
127127
var tokens = []; // Buffer to hold the tokens
128128
var spaces = []; // Indices of whitespace tokens on the current line
@@ -175,10 +175,11 @@
175175

176176
if (isWhitespace(chr)) {
177177
spaces.push(tokens.length);
178-
if (!nonSpace)
179-
indentation += chr;
178+
indentation += chr;
180179
} else {
181180
nonSpace = true;
181+
lineHasNonSpace = true;
182+
indentation += ' ';
182183
}
183184

184185
tokens.push([ 'text', chr, start, start + 1 ]);
@@ -189,6 +190,7 @@
189190
stripSpace();
190191
indentation = '';
191192
tagIndex = 0;
193+
lineHasNonSpace = false;
192194
}
193195
}
194196
}
@@ -222,7 +224,7 @@
222224
throw new Error('Unclosed tag at ' + scanner.pos);
223225

224226
if (type == '>') {
225-
token = [ type, value, start, scanner.pos, indentation, tagIndex ];
227+
token = [ type, value, start, scanner.pos, indentation, tagIndex, lineHasNonSpace ];
226228
} else {
227229
token = [ type, value, start, scanner.pos ];
228230
}
@@ -610,11 +612,11 @@
610612
return this.renderTokens(token[4], context, partials, originalTemplate);
611613
};
612614

613-
Writer.prototype.indentPartial = function indentPartial (partial, indentation) {
615+
Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) {
614616
var filteredIndentation = indentation.replace(/[^ \t]/g, '');
615617
var partialByNl = partial.split('\n');
616618
for (var i = 0; i < partialByNl.length; i++) {
617-
if (partialByNl[i].length) {
619+
if (partialByNl[i].length && (i > 0 || !lineHasNonSpace)) {
618620
partialByNl[i] = filteredIndentation + partialByNl[i];
619621
}
620622
}
@@ -626,11 +628,12 @@
626628

627629
var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
628630
if (value != null) {
631+
var lineHasNonSpace = token[6];
629632
var tagIndex = token[5];
630633
var indentation = token[4];
631634
var indentedValue = value;
632635
if (tagIndex == 0 && indentation) {
633-
indentedValue = this.indentPartial(value, indentation);
636+
indentedValue = this.indentPartial(value, indentation, lineHasNonSpace);
634637
}
635638
return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue);
636639
}

0 commit comments

Comments
 (0)