|
1 | | -/*! Dust - Asynchronous Templating - v2.4.2 |
| 1 | +/*! Dust - Asynchronous Templating - v2.5.0 |
2 | 2 | * http://linkedin.github.io/dustjs/ |
3 | 3 | * Copyright (c) 2014 Aleksander Williams; Released under the MIT License */ |
4 | 4 | (function(root) { |
|
16 | 16 |
|
17 | 17 | dust.debugLevel = NONE; |
18 | 18 |
|
| 19 | + dust.config = { |
| 20 | + whitespace: false, |
| 21 | + }; |
| 22 | + |
| 23 | + // Directive aliases to minify code |
| 24 | + dust._aliases = { |
| 25 | + "write": "w", |
| 26 | + "end": "e", |
| 27 | + "map": "m", |
| 28 | + "render": "r", |
| 29 | + "reference": "f", |
| 30 | + "section": "s", |
| 31 | + "exists": "x", |
| 32 | + "notexists": "nx", |
| 33 | + "block": "b", |
| 34 | + "partial": "p", |
| 35 | + "helper": "h" |
| 36 | + }; |
| 37 | + |
19 | 38 | // Try to find the console in global scope |
20 | 39 | if (root && root.console && root.console.log) { |
21 | 40 | loggerContext = root.console; |
|
297 | 316 | Context.prototype._get = function(cur, down) { |
298 | 317 | var ctx = this.stack, |
299 | 318 | i = 1, |
300 | | - value, first, len, ctxThis; |
| 319 | + value, first, len, ctxThis, fn; |
301 | 320 | first = down[0]; |
302 | 321 | len = down.length; |
303 | 322 |
|
|
342 | 361 |
|
343 | 362 | // Return the ctx or a function wrapping the application of the context. |
344 | 363 | if (typeof ctx === 'function') { |
345 | | - return function() { |
| 364 | + fn = function() { |
346 | 365 | try { |
347 | 366 | return ctx.apply(ctxThis, arguments); |
348 | 367 | } catch (err) { |
349 | 368 | dust.log(err, ERROR); |
350 | 369 | throw err; |
351 | 370 | } |
352 | 371 | }; |
| 372 | + fn.__dustBody = !!ctx.__dustBody; |
| 373 | + return fn; |
353 | 374 | } else { |
354 | 375 | if (ctx === undefined) { |
355 | 376 | dust.log('Cannot find the value for reference [{' + down.join('.') + '}] in template [' + this.getTemplateName() + ']'); |
|
499 | 520 | this.events = {}; |
500 | 521 | } |
501 | 522 | if (!this.events[type]) { |
502 | | - dust.log('Event type [' + type + '] does not exist. Using just the specified callback.', WARN); |
503 | 523 | if(callback) { |
504 | 524 | this.events[type] = callback; |
505 | 525 | } else { |
|
612 | 632 |
|
613 | 633 | Chunk.prototype.section = function(elem, context, bodies, params) { |
614 | 634 | // anonymous functions |
615 | | - if (typeof elem === 'function') { |
| 635 | + if (typeof elem === 'function' && !elem.__dustBody) { |
616 | 636 | try { |
617 | 637 | elem = elem.apply(context.current(), [this, context, bodies, params]); |
618 | 638 | } catch(e) { |
|
797 | 817 | return this; |
798 | 818 | }; |
799 | 819 |
|
| 820 | + // Chunk aliases |
| 821 | + for(var f in Chunk.prototype) { |
| 822 | + if(dust._aliases[f]) { |
| 823 | + Chunk.prototype[dust._aliases[f]] = Chunk.prototype[f]; |
| 824 | + } |
| 825 | + } |
| 826 | + |
800 | 827 | function Tap(head, tail) { |
801 | 828 | this.head = head; |
802 | 829 | this.tail = tail; |
|
816 | 843 | return value; |
817 | 844 | }; |
818 | 845 |
|
819 | | - var HCHARS = new RegExp(/[&<>\"\']/), |
| 846 | + var HCHARS = /[&<>"']/, |
820 | 847 | AMP = /&/g, |
821 | 848 | LT = /</g, |
822 | 849 | GT = />/g, |
|
3546 | 3573 | var compiler = {}, |
3547 | 3574 | isArray = dust.isArray; |
3548 | 3575 |
|
3549 | | - |
| 3576 | + |
3550 | 3577 | compiler.compile = function(source, name) { |
3551 | 3578 | // the name parameter is optional. |
3552 | 3579 | // this can happen for templates that are rendered immediately (renderSource which calls compileFn) or |
|
3556 | 3583 | if (!name && name !== null) { |
3557 | 3584 | throw new Error('Template name parameter cannot be undefined when calling dust.compile'); |
3558 | 3585 | } |
3559 | | - |
| 3586 | + |
3560 | 3587 | try { |
3561 | 3588 | var ast = filterAST(parse(source)); |
3562 | 3589 | return compile(ast, name); |
|
3583 | 3610 | body: compactBuffers, |
3584 | 3611 | buffer: noop, |
3585 | 3612 | special: convertSpecial, |
3586 | | - format: nullify, // TODO: convert format |
| 3613 | + format: format, |
3587 | 3614 | reference: visit, |
3588 | 3615 | '#': visit, |
3589 | 3616 | '?': visit, |
|
3640 | 3667 | for (i=1, len=node.length; i<len; i++) { |
3641 | 3668 | res = compiler.filterNode(context, node[i]); |
3642 | 3669 | if (res) { |
3643 | | - if (res[0] === 'buffer') { |
| 3670 | + if (res[0] === 'buffer' || res[0] === 'format') { |
3644 | 3671 | if (memo) { |
3645 | | - memo[1] += res[1]; |
| 3672 | + memo[0] = (res[0] === 'buffer') ? 'buffer' : memo[0]; |
| 3673 | + memo[1] += res.slice(1, -2).join(''); |
3646 | 3674 | } else { |
3647 | 3675 | memo = res; |
3648 | 3676 | out.push(res); |
|
3665 | 3693 | }; |
3666 | 3694 |
|
3667 | 3695 | function convertSpecial(context, node) { |
3668 | | - return ['buffer', specialChars[node[1]]]; |
| 3696 | + return ['buffer', specialChars[node[1]], node[2], node[3]]; |
3669 | 3697 | } |
3670 | 3698 |
|
3671 | 3699 | function noop(context, node) { |
|
3674 | 3702 |
|
3675 | 3703 | function nullify(){} |
3676 | 3704 |
|
| 3705 | + function format(context, node) { |
| 3706 | + return dust.config.whitespace ? node : null; |
| 3707 | + } |
| 3708 | + |
3677 | 3709 | function compile(ast, name) { |
3678 | 3710 | var context = { |
3679 | 3711 | name: name, |
|
3716 | 3748 |
|
3717 | 3749 | for (i=0, len=bodies.length; i<len; i++) { |
3718 | 3750 | out[i] = 'function body_' + i + '(chk,ctx){' + |
3719 | | - blx + 'return chk' + bodies[i] + ';}'; |
| 3751 | + blx + 'return chk' + bodies[i] + ';}body_' + i + '.__dustBody=!0;'; |
3720 | 3752 | } |
3721 | 3753 | return out.join(''); |
3722 | 3754 | } |
|
3743 | 3775 | }, |
3744 | 3776 |
|
3745 | 3777 | buffer: function(context, node) { |
3746 | | - return '.write(' + escape(node[1]) + ')'; |
| 3778 | + return '.w(' + escape(node[1]) + ')'; |
3747 | 3779 | }, |
3748 | 3780 |
|
3749 | 3781 | format: function(context, node) { |
3750 | | - return '.write(' + escape(node[1] + node[2]) + ')'; |
| 3782 | + return '.w(' + escape(node[1] + node[2]) + ')'; |
3751 | 3783 | }, |
3752 | 3784 |
|
3753 | 3785 | reference: function(context, node) { |
3754 | | - return '.reference(' + compiler.compileNode(context, node[1]) + |
| 3786 | + return '.f(' + compiler.compileNode(context, node[1]) + |
3755 | 3787 | ',ctx,' + compiler.compileNode(context, node[2]) + ')'; |
3756 | 3788 | }, |
3757 | 3789 |
|
|
3798 | 3830 | }, |
3799 | 3831 |
|
3800 | 3832 | '@': function(context, node) { |
3801 | | - return '.helper(' + |
| 3833 | + return '.h(' + |
3802 | 3834 | escape(node[1].text) + |
3803 | 3835 | ',' + compiler.compileNode(context, node[2]) + ',' + |
3804 | 3836 | compiler.compileNode(context, node[4]) + ',' + |
|
3838 | 3870 | }, |
3839 | 3871 |
|
3840 | 3872 | partial: function(context, node) { |
3841 | | - return '.partial(' + |
| 3873 | + return '.p(' + |
3842 | 3874 | compiler.compileNode(context, node[1]) + |
3843 | 3875 | ',' + compiler.compileNode(context, node[2]) + |
3844 | 3876 | ',' + compiler.compileNode(context, node[3]) + ')'; |
|
3907 | 3939 | return escape(node[1]); |
3908 | 3940 | }, |
3909 | 3941 | raw: function(context, node) { |
3910 | | - return ".write(" + escape(node[1]) + ")"; |
| 3942 | + return ".w(" + escape(node[1]) + ")"; |
3911 | 3943 | } |
3912 | 3944 | }; |
3913 | 3945 |
|
3914 | 3946 | function compileSection(context, node, cmd) { |
3915 | | - return '.' + cmd + '(' + |
| 3947 | + return '.' + (dust._aliases[cmd] || cmd) + '(' + |
3916 | 3948 | compiler.compileNode(context, node[1]) + |
3917 | 3949 | ',' + compiler.compileNode(context, node[2]) + ',' + |
3918 | 3950 | compiler.compileNode(context, node[4]) + ',' + |
|
3946 | 3978 | dust.pragmas = compiler.pragmas; |
3947 | 3979 | dust.compileNode = compiler.compileNode; |
3948 | 3980 | dust.nodes = compiler.nodes; |
3949 | | - |
| 3981 | + |
3950 | 3982 | return compiler; |
3951 | 3983 |
|
3952 | 3984 | })); |
3953 | | - |
|
0 commit comments