|
1 |
| -/*! dustjs-linkedin - v2.7.1 |
| 1 | +/*! dustjs-linkedin - v2.7.2 |
2 | 2 | * http://dustjs.com/
|
3 | 3 | * Copyright (c) 2015 Aleksander Williams; Released under the MIT License */
|
4 | 4 | (function (root, factory) {
|
5 |
| - /*global define*/ |
6 | 5 | if (typeof define === 'function' && define.amd && define.amd.dust === true) {
|
7 | 6 | define('dust.core', [], factory);
|
8 | 7 | } else if (typeof exports === 'object') {
|
|
12 | 11 | }
|
13 | 12 | }(this, function() {
|
14 | 13 | var dust = {
|
15 |
| - "version": "2.7.1" |
| 14 | + "version": "2.7.2" |
16 | 15 | },
|
17 | 16 | NONE = 'NONE', ERROR = 'ERROR', WARN = 'WARN', INFO = 'INFO', DEBUG = 'DEBUG',
|
18 | 17 | EMPTY_FUNC = function() {};
|
|
266 | 265 | */
|
267 | 266 | dust.isStreamable = function(elem) {
|
268 | 267 | return elem &&
|
269 |
| - typeof elem.on === 'function'; |
| 268 | + typeof elem.on === 'function' && |
| 269 | + typeof elem.pipe === 'function'; |
270 | 270 | };
|
271 | 271 |
|
272 | 272 | // apply the filter chain and return the output string
|
273 |
| - dust.filter = function(string, auto, filters) { |
274 |
| - var i, len, name; |
| 273 | + dust.filter = function(string, auto, filters, context) { |
| 274 | + var i, len, name, filter; |
275 | 275 | if (filters) {
|
276 | 276 | for (i = 0, len = filters.length; i < len; i++) {
|
277 | 277 | name = filters[i];
|
| 278 | + if (!name.length) { |
| 279 | + continue; |
| 280 | + } |
| 281 | + filter = dust.filters[name]; |
278 | 282 | if (name === 's') {
|
279 | 283 | auto = null;
|
280 |
| - } |
281 |
| - else if (typeof dust.filters[name] === 'function') { |
282 |
| - string = dust.filters[name](string); |
283 |
| - } |
284 |
| - else { |
| 284 | + } else if (typeof filter === 'function') { |
| 285 | + string = filter(string, context); |
| 286 | + } else { |
285 | 287 | dust.log('Invalid filter `' + name + '`', WARN);
|
286 | 288 | }
|
287 | 289 | }
|
288 | 290 | }
|
289 | 291 | // by default always apply the h filter, unless asked to unescape with |s
|
290 | 292 | if (auto) {
|
291 |
| - string = dust.filters[auto](string); |
| 293 | + string = dust.filters[auto](string, context); |
292 | 294 | }
|
293 | 295 | return string;
|
294 | 296 | };
|
|
757 | 759 | } else if (dust.isStreamable(elem)) {
|
758 | 760 | return this.stream(elem, context, null, auto, filters);
|
759 | 761 | } else if (!dust.isEmpty(elem)) {
|
760 |
| - return this.write(dust.filter(elem, auto, filters)); |
| 762 | + return this.write(dust.filter(elem, auto, filters, context)); |
761 | 763 | } else {
|
762 | 764 | return this;
|
763 | 765 | }
|
|
783 | 785 | }
|
784 | 786 | }
|
785 | 787 |
|
| 788 | + if (dust.isEmptyObject(bodies)) { |
| 789 | + // No bodies to render, and we've already invoked any function that was available in |
| 790 | + // hopes of returning a Chunk. |
| 791 | + return chunk; |
| 792 | + } |
| 793 | + |
786 | 794 | if (!dust.isEmptyObject(params)) {
|
787 | 795 | context = context.push(params);
|
788 | 796 | }
|
|
902 | 910 | }
|
903 | 911 | };
|
904 | 912 |
|
905 |
| - Chunk.prototype.helper = function(name, context, bodies, params) { |
| 913 | + Chunk.prototype.helper = function(name, context, bodies, params, auto) { |
906 | 914 | var chunk = this,
|
| 915 | + filters = params.filters, |
907 | 916 | ret;
|
| 917 | + |
| 918 | + // Pre-2.7.1 compat: if auto is undefined, it's an old template. Automatically escape |
| 919 | + if (auto === undefined) { |
| 920 | + auto = 'h'; |
| 921 | + } |
| 922 | + |
908 | 923 | // handle invalid helpers, similar to invalid filters
|
909 | 924 | if(dust.helpers[name]) {
|
910 | 925 | try {
|
911 | 926 | ret = dust.helpers[name](chunk, context, bodies, params);
|
912 |
| - if (dust.isThenable(ret)) { |
913 |
| - return this.await(ret, context, bodies); |
| 927 | + if (ret instanceof Chunk) { |
| 928 | + return ret; |
| 929 | + } |
| 930 | + if(typeof filters === 'string') { |
| 931 | + filters = filters.split('|'); |
| 932 | + } |
| 933 | + if (!dust.isEmptyObject(bodies)) { |
| 934 | + return chunk.section(ret, context, bodies, params); |
914 | 935 | }
|
915 |
| - return ret; |
| 936 | + // Helpers act slightly differently from functions in context in that they will act as |
| 937 | + // a reference if they are self-closing (due to grammar limitations) |
| 938 | + // In the Chunk.await function we check to make sure bodies is null before acting as a reference |
| 939 | + return chunk.reference(ret, context, auto, filters); |
916 | 940 | } catch(err) {
|
917 | 941 | dust.log('Error in helper `' + name + '`: ' + err.message, ERROR);
|
918 | 942 | return chunk.setError(err);
|
|
928 | 952 | * @param thenable {Thenable} the target thenable to await
|
929 | 953 | * @param context {Context} context to use to render the deferred chunk
|
930 | 954 | * @param bodies {Object} must contain a "body", may contain an "error"
|
| 955 | + * @param auto {String} automatically apply this filter if the Thenable is a reference |
| 956 | + * @param filters {Array} apply these filters if the Thenable is a reference |
931 | 957 | * @return {Chunk}
|
932 | 958 | */
|
933 | 959 | Chunk.prototype.await = function(thenable, context, bodies, auto, filters) {
|
934 |
| - var body = bodies && bodies.block, |
935 |
| - errorBody = bodies && bodies.error; |
936 | 960 | return this.map(function(chunk) {
|
937 | 961 | thenable.then(function(data) {
|
938 |
| - if(body) { |
939 |
| - chunk.render(body, context.push(data)).end(); |
| 962 | + if (bodies) { |
| 963 | + chunk = chunk.section(data, context, bodies); |
940 | 964 | } else {
|
941 |
| - chunk.reference(data, context, auto, filters).end(); |
| 965 | + // Actually a reference. Self-closing sections don't render |
| 966 | + chunk = chunk.reference(data, context, auto, filters); |
942 | 967 | }
|
| 968 | + chunk.end(); |
943 | 969 | }, function(err) {
|
| 970 | + var errorBody = bodies && bodies.error; |
944 | 971 | if(errorBody) {
|
945 | 972 | chunk.render(errorBody, context.push(err)).end();
|
946 | 973 | } else {
|
947 |
| - dust.log('Unhandled promise rejection in `' + context.getTemplateName() + '`'); |
| 974 | + dust.log('Unhandled promise rejection in `' + context.getTemplateName() + '`', INFO); |
948 | 975 | chunk.end();
|
949 | 976 | }
|
950 | 977 | });
|
|
976 | 1003 | chunk = chunk.map(function(chunk) {
|
977 | 1004 | chunk.render(body, context.push(thunk)).end();
|
978 | 1005 | });
|
979 |
| - } else { |
980 |
| - // Don't fork, just write into the master async chunk |
| 1006 | + } else if(!bodies) { |
| 1007 | + // When actually a reference, don't fork, just write into the master async chunk |
981 | 1008 | chunk = chunk.reference(thunk, context, auto, filters);
|
982 | 1009 | }
|
983 | 1010 | })
|
|
988 | 1015 | if(errorBody) {
|
989 | 1016 | chunk.render(errorBody, context.push(err));
|
990 | 1017 | } else {
|
991 |
| - dust.log('Unhandled stream error in `' + context.getTemplateName() + '`'); |
| 1018 | + dust.log('Unhandled stream error in `' + context.getTemplateName() + '`', INFO); |
992 | 1019 | }
|
993 | 1020 | if(!ended) {
|
994 | 1021 | ended = true;
|
|
0 commit comments