Browse Source

Inline tag compilation

tags/v1.0.0
Michael Jackson 12 years ago
parent
commit
7301707703
1 changed files with 27 additions and 27 deletions
  1. +27
    -27
      mustache.js

+ 27
- 27
mustache.js View File

@@ -61,16 +61,6 @@
}); });
} }


function escapeTags(tags) {
if (!isArray(tags) || tags.length !== 2)
throw new Error('Invalid tags: ' + tags);

return [
new RegExp(escapeRegExp(tags[0]) + "\\s*"),
new RegExp("\\s*" + escapeRegExp(tags[1]))
];
}

var whiteRe = /\s*/; var whiteRe = /\s*/;
var spaceRe = /\s+/; var spaceRe = /\s+/;
var equalsRe = /\s*=/; var equalsRe = /\s*=/;
@@ -103,14 +93,6 @@
if (!template) if (!template)
return []; return [];


tags = tags || mustache.tags;

if (typeof tags === 'string')
tags = tags.split(spaceRe);

var tagRes = escapeTags(tags);
var scanner = new Scanner(template);

var sections = []; // Stack to hold section tokens var sections = []; // Stack to hold section tokens
var tokens = []; // Buffer to hold the tokens var tokens = []; // Buffer to hold the tokens
var spaces = []; // Indices of whitespace tokens on the current line var spaces = []; // Indices of whitespace tokens on the current line
@@ -131,12 +113,30 @@
nonSpace = false; nonSpace = false;
} }


var openingTagRe, closingTagRe, closingCurlyRe;
function compileTags(tags) {
if (typeof tags === 'string')
tags = tags.split(spaceRe, 2);

if (!isArray(tags) || tags.length !== 2)
throw new Error('Invalid tags: ' + tags);

openingTagRe = new RegExp(escapeRegExp(tags[0]) + '\\s*');
closingTagRe = new RegExp('\\s*' + escapeRegExp(tags[1]));
closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tags[1]));
}

compileTags(tags || mustache.tags);

var scanner = new Scanner(template);

var start, type, value, chr, token, openSection; var start, type, value, chr, token, openSection;
while (!scanner.eos()) { while (!scanner.eos()) {
start = scanner.pos; start = scanner.pos;


// Match any text between tags. // Match any text between tags.
value = scanner.scanUntil(tagRes[0]);
value = scanner.scanUntil(openingTagRe);

if (value) { if (value) {
for (var i = 0, valueLength = value.length; i < valueLength; ++i) { for (var i = 0, valueLength = value.length; i < valueLength; ++i) {
chr = value.charAt(i); chr = value.charAt(i);
@@ -147,7 +147,7 @@
nonSpace = true; nonSpace = true;
} }


tokens.push(['text', chr, start, start + 1]);
tokens.push([ 'text', chr, start, start + 1 ]);
start += 1; start += 1;


// Check for whitespace on the current line. // Check for whitespace on the current line.
@@ -157,7 +157,7 @@
} }


// Match the opening tag. // Match the opening tag.
if (!scanner.scan(tagRes[0]))
if (!scanner.scan(openingTagRe))
break; break;


hasTag = true; hasTag = true;
@@ -170,18 +170,18 @@
if (type === '=') { if (type === '=') {
value = scanner.scanUntil(equalsRe); value = scanner.scanUntil(equalsRe);
scanner.scan(equalsRe); scanner.scan(equalsRe);
scanner.scanUntil(tagRes[1]);
scanner.scanUntil(closingTagRe);
} else if (type === '{') { } else if (type === '{') {
value = scanner.scanUntil(new RegExp('\\s*' + escapeRegExp('}' + tags[1])));
value = scanner.scanUntil(closingCurlyRe);
scanner.scan(curlyRe); scanner.scan(curlyRe);
scanner.scanUntil(tagRes[1]);
scanner.scanUntil(closingTagRe);
type = '&'; type = '&';
} else { } else {
value = scanner.scanUntil(tagRes[1]);
value = scanner.scanUntil(closingTagRe);
} }


// Match the closing tag. // Match the closing tag.
if (!scanner.scan(tagRes[1]))
if (!scanner.scan(closingTagRe))
throw new Error('Unclosed tag at ' + scanner.pos); throw new Error('Unclosed tag at ' + scanner.pos);


token = [ type, value, start, scanner.pos ]; token = [ type, value, start, scanner.pos ];
@@ -202,7 +202,7 @@
nonSpace = true; nonSpace = true;
} else if (type === '=') { } else if (type === '=') {
// Set the tags for the next time around. // Set the tags for the next time around.
tagRes = escapeTags(tags = value.split(spaceRe));
compileTags(value);
} }
} }




Loading…
Cancel
Save