Skip to content

Commit f6496c8

Browse files
committed
Style tweaks, added comments
1 parent 3cdb300 commit f6496c8

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

mustache.js

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,7 @@
372372
tags = tags || exports.tags;
373373

374374
if (typeof tags === 'string') tags = tags.split(spaceRe);
375-
if (tags.length !== 2) {
376-
throw new Error('Invalid tags: ' + tags.join(', '));
377-
}
375+
if (tags.length !== 2) throw new Error('Invalid tags: ' + tags.join(', '));
378376

379377
var tagRes = escapeTags(tags);
380378
var scanner = new Scanner(template);
@@ -403,8 +401,9 @@
403401
var start, type, value, chr, token;
404402
while (!scanner.eos()) {
405403
start = scanner.pos;
406-
value = scanner.scanUntil(tagRes[0]);
407404

405+
// Match any text between tags.
406+
value = scanner.scanUntil(tagRes[0]);
408407
if (value) {
409408
for (var i = 0, len = value.length; i < len; ++i) {
410409
chr = value.charAt(i);
@@ -423,74 +422,54 @@
423422
}
424423
}
425424

426-
start = scanner.pos;
427-
428425
// Match the opening tag.
429426
if (!scanner.scan(tagRes[0])) break;
430427
hasTag = true;
431428

429+
// Get the tag type.
432430
type = scanner.scan(tagRe) || 'name';
433-
434-
// Skip any whitespace between tag and value.
435431
scanner.scan(whiteRe);
436432

437-
// Extract the tag value.
438-
if (type === "=") {
433+
// Get the tag value.
434+
if (type === '=') {
439435
value = scanner.scanUntil(eqRe);
440436
scanner.scan(eqRe);
441437
scanner.scanUntil(tagRes[1]);
442-
} else if (type === "{") {
443-
var closeRe = new RegExp("\\s*" + escapeRe("}" + tags[1]));
444-
value = scanner.scanUntil(closeRe);
438+
} else if (type === '{') {
439+
value = scanner.scanUntil(new RegExp('\\s*' + escapeRe('}' + tags[1])));
445440
scanner.scan(curlyRe);
446441
scanner.scanUntil(tagRes[1]);
447-
type = "&";
442+
type = '&';
448443
} else {
449444
value = scanner.scanUntil(tagRes[1]);
450445
}
451446

452447
// Match the closing tag.
453-
if (!scanner.scan(tagRes[1])) {
454-
throw new Error('Unclosed tag at ' + scanner.pos);
455-
}
456-
457-
// Check section nesting.
458-
if (type === '/') {
459-
if (sections.length === 0) {
460-
throw new Error('Unopened section "' + value + '" at ' + start);
461-
}
462-
463-
var section = sections.pop();
464-
465-
if (section[1] !== value) {
466-
throw new Error('Unclosed section "' + section[1] + '" at ' + start);
467-
}
468-
}
448+
if (!scanner.scan(tagRes[1])) throw new Error('Unclosed tag at ' + scanner.pos);
469449

470450
token = [type, value, start, scanner.pos];
471451
tokens.push(token);
472452

473453
if (type === '#' || type === '^') {
474454
sections.push(token);
475-
} else if (type === "name" || type === "{" || type === "&") {
455+
} else if (type === '/') {
456+
// Check section nesting.
457+
if (sections.length === 0) throw new Error('Unopened section "' + value + '" at ' + start);
458+
var openSection = sections.pop();
459+
if (openSection[1] !== value) throw new Error('Unclosed section "' + openSection[1] + '" at ' + start);
460+
} else if (type === 'name' || type === '{' || type === '&') {
476461
nonSpace = true;
477-
} else if (type === "=") {
462+
} else if (type === '=') {
478463
// Set the tags for the next time around.
479464
tags = value.split(spaceRe);
480-
481-
if (tags.length !== 2) {
482-
throw new Error('Invalid tags at ' + start + ': ' + tags.join(', '));
483-
}
484-
465+
if (tags.length !== 2) throw new Error('Invalid tags at ' + start + ': ' + tags.join(', '));
485466
tagRes = escapeTags(tags);
486467
}
487468
}
488469

489470
// Make sure there are no open sections when we're done.
490-
var section = sections.pop();
491-
if (section) {
492-
throw new Error('Unclosed section "' + section[1] + '" at ' + scanner.pos);
493-
}
471+
var openSection = sections.pop();
472+
if (openSection) throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos);
494473

495474
tokens = squashTokens(tokens);
496475

0 commit comments

Comments
 (0)