Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mustache specs - whitespace compliance #303

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 34 additions & 7 deletions mustache.js
Expand Up @@ -181,6 +181,7 @@
Writer.prototype.clearCache = function () {
this._cache = {};
this._partialCache = {};
this._partials = {};
};

Writer.prototype.compile = function (template, tags) {
Expand All @@ -194,18 +195,27 @@
return fn;
};

Writer.prototype.compilePartial = function (name, template, tags) {
Writer.prototype.compilePartial = function (name, template, tags, indentation) {
// A standalone partial should be indented with the whitespace before the
// partial on the same row. We keep track on instances of the same partial
// with different indentations by prefixing its name with the indentation in
// the partial cache.
var cacheName = (indentation || '') + name;
if (indentation)
template = template.replace(/^(?=.)/gm, indentation);
var fn = this.compile(template, tags);
this._partialCache[name] = fn;
this._partialCache[cacheName] = fn;
return fn;
};

Writer.prototype.getPartial = function (name) {
if (!(name in this._partialCache) && this._loadPartial) {
this.compilePartial(name, this._loadPartial(name));
Writer.prototype.getPartial = function (name, indentation) {
var cacheName = (indentation || '') + name;
if (!(cacheName in this._partialCache)) {
var partial = this._loadPartial ? this._loadPartial(name) : this._partials[name];
if (partial) this.compilePartial(name, partial, null, indentation);
}

return this._partialCache[name];
return this._partialCache[cacheName];
};

Writer.prototype.compileTokens = function (tokens, template) {
Expand All @@ -217,6 +227,7 @@
} else {
for (var name in partials) {
self.compilePartial(name, partials[name]);
self._partials[name] = partials[name];
}
}
}
Expand Down Expand Up @@ -277,7 +288,8 @@

break;
case '>':
value = writer.getPartial(tokenValue);
// indentation for standalone partial in token[4]
value = writer.getPartial(tokenValue, token[4]);
if (typeof value === 'function') buffer += value(context);
break;
case '&':
Expand Down Expand Up @@ -381,6 +393,7 @@
var sections = []; // Stack to hold section tokens
var tokens = []; // Buffer to hold the tokens
var spaces = []; // Indices of whitespace tokens on the current line
var whitespace = ''; // Whitespace characters at the same line before a tag
var hasTag = false; // Is there a {{tag}} on the current line?
var nonSpace = false; // Is there a non-space char on the current line?

Expand All @@ -397,6 +410,7 @@

hasTag = false;
nonSpace = false;
whitespace = '';
}

var start, type, value, chr, token;
Expand All @@ -411,6 +425,7 @@

if (isWhitespace(chr)) {
spaces.push(tokens.length);
whitespace += chr;
} else {
nonSpace = true;
}
Expand All @@ -423,6 +438,9 @@
}
}

// The following tag is not standalone
if (whitespace && nonSpace) whitespace = '';

// Match the opening tag.
if (!scanner.scan(tagRes[0])) break;
hasTag = true;
Expand All @@ -449,6 +467,10 @@
if (!scanner.scan(tagRes[1])) throw new Error('Unclosed tag at ' + scanner.pos);

token = [type, value, start, scanner.pos];

// Store whitespaces before standalone partials
if (type === '>' && whitespace) token[4] = whitespace;

tokens.push(token);

if (type === '#' || type === '^') {
Expand All @@ -466,7 +488,12 @@
if (tags.length !== 2) throw new Error('Invalid tags at ' + start + ': ' + tags.join(', '));
tagRes = escapeTags(tags);
}
whitespace = '';
}
// Standalone tags should not require a newline to follow them
// therefore we strip spaces from the last line if it is a standalone tag,
// even if it might not be followed by a newline
stripSpace();

// Make sure there are no open sections when we're done.
var openSection = sections.pop();
Expand Down
17 changes: 0 additions & 17 deletions test/mustache-spec-test.js
Expand Up @@ -5,23 +5,6 @@ var path = require('path');
var specsDir = path.join(__dirname, 'spec/specs');

var skipTests = {
comments: [
'Standalone Without Newline'
],
delimiters: [
'Standalone Without Newline'
],
inverted: [
'Standalone Without Newline'
],
partials: [
'Standalone Without Previous Line',
'Standalone Without Newline',
'Standalone Indentation'
],
sections: [
'Standalone Without Newline'
],
'~lambdas': [
'Interpolation',
'Interpolation - Expansion',
Expand Down