|
372 | 372 | tags = tags || exports.tags;
|
373 | 373 |
|
374 | 374 | 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(', ')); |
378 | 376 |
|
379 | 377 | var tagRes = escapeTags(tags);
|
380 | 378 | var scanner = new Scanner(template);
|
|
403 | 401 | var start, type, value, chr, token;
|
404 | 402 | while (!scanner.eos()) {
|
405 | 403 | start = scanner.pos;
|
406 |
| - value = scanner.scanUntil(tagRes[0]); |
407 | 404 |
|
| 405 | + // Match any text between tags. |
| 406 | + value = scanner.scanUntil(tagRes[0]); |
408 | 407 | if (value) {
|
409 | 408 | for (var i = 0, len = value.length; i < len; ++i) {
|
410 | 409 | chr = value.charAt(i);
|
|
423 | 422 | }
|
424 | 423 | }
|
425 | 424 |
|
426 |
| - start = scanner.pos; |
427 |
| - |
428 | 425 | // Match the opening tag.
|
429 | 426 | if (!scanner.scan(tagRes[0])) break;
|
430 | 427 | hasTag = true;
|
431 | 428 |
|
| 429 | + // Get the tag type. |
432 | 430 | type = scanner.scan(tagRe) || 'name';
|
433 |
| - |
434 |
| - // Skip any whitespace between tag and value. |
435 | 431 | scanner.scan(whiteRe);
|
436 | 432 |
|
437 |
| - // Extract the tag value. |
438 |
| - if (type === "=") { |
| 433 | + // Get the tag value. |
| 434 | + if (type === '=') { |
439 | 435 | value = scanner.scanUntil(eqRe);
|
440 | 436 | scanner.scan(eqRe);
|
441 | 437 | 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]))); |
445 | 440 | scanner.scan(curlyRe);
|
446 | 441 | scanner.scanUntil(tagRes[1]);
|
447 |
| - type = "&"; |
| 442 | + type = '&'; |
448 | 443 | } else {
|
449 | 444 | value = scanner.scanUntil(tagRes[1]);
|
450 | 445 | }
|
451 | 446 |
|
452 | 447 | // 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); |
469 | 449 |
|
470 | 450 | token = [type, value, start, scanner.pos];
|
471 | 451 | tokens.push(token);
|
472 | 452 |
|
473 | 453 | if (type === '#' || type === '^') {
|
474 | 454 | 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 === '&') { |
476 | 461 | nonSpace = true;
|
477 |
| - } else if (type === "=") { |
| 462 | + } else if (type === '=') { |
478 | 463 | // Set the tags for the next time around.
|
479 | 464 | 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(', ')); |
485 | 466 | tagRes = escapeTags(tags);
|
486 | 467 | }
|
487 | 468 | }
|
488 | 469 |
|
489 | 470 | // 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); |
494 | 473 |
|
495 | 474 | tokens = squashTokens(tokens);
|
496 | 475 |
|
|
0 commit comments