Skip to content

Commit 5cd7f0c

Browse files
committed
Upgrade mustache.js to 4.1.0
1 parent 77b1a9a commit 5cd7f0c

File tree

6 files changed

+65
-31
lines changed

6 files changed

+65
-31
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
mustache-js-rails (4.0.1)
4+
mustache-js-rails (4.1.0)
55
railties (>= 3.1)
66

77
GEM

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and [mustache jQuery integration](https://github.com/jonnyreeves/jquery-Mustache
99

1010
Integrated versions are:
1111

12-
* mustache.js - <b id="mustache-js-version">4.0.1</b>
12+
* mustache.js - <b id="mustache-js-version">4.1.0</b>
1313
* jQuery mustache - <b id="jquery-mustache-js-version">0.2.8</b>
1414

1515
### Installation

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env rake
22
require 'bundler/gem_tasks'
3+
require 'json'
34

45
task :update do
56
Dir['*.gem'].each{ |f| FileUtils.rm(f) }
@@ -21,7 +22,7 @@ task :update do
2122
FileUtils.cp('jquery.mustache.js/jquery.mustache.js', js_dir)
2223

2324
puts 'Updating version...'
24-
mustache_js_version = File.read('mustache.js/mustache.js.nuspec').match(/<version>([\.\d]+)<\/version>/)[1]
25+
mustache_js_version = JSON.parse(File.read('mustache.js/package.json')).fetch('version')
2526
jquery_mustache_js_version = File.read('jquery.mustache.js/jquery.mustache.js').match(/jQuery Mustache - v([\.\d]+)/)[1]
2627
puts "Current mustache.js version is: #{mustache_js_version}"
2728
puts "Current jquery.mustache.js version is: #{jquery_mustache_js_version}"

lib/mustache-js-rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MustacheJsRails
2-
VERSION = "4.0.1"
2+
VERSION = "4.1.0"
33
end

vendor/assets/javascripts/mustache.js

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,25 @@
536536
* also be a function that is used to load partial templates on the fly
537537
* that takes a single argument: the name of the partial.
538538
*
539-
* If the optional `tags` argument is given here it must be an array with two
539+
* If the optional `config` argument is given here, then it should be an
540+
* object with a `tags` attribute or an `escape` attribute or both.
541+
* If an array is passed, then it will be interpreted the same way as
542+
* a `tags` attribute on a `config` object.
543+
*
544+
* The `tags` attribute of a `config` object must be an array with two
540545
* string values: the opening and closing tags used in the template (e.g.
541546
* [ "<%", "%>" ]). The default is to mustache.tags.
547+
*
548+
* The `escape` attribute of a `config` object must be a function which
549+
* accepts a string as input and outputs a safely escaped string.
550+
* If an `escape` function is not provided, then an HTML-safe string
551+
* escaping function is used as the default.
542552
*/
543-
Writer.prototype.render = function render (template, view, partials, tags) {
553+
Writer.prototype.render = function render (template, view, partials, config) {
554+
var tags = this.getConfigTags(config);
544555
var tokens = this.parse(template, tags);
545556
var context = (view instanceof Context) ? view : new Context(view, undefined);
546-
return this.renderTokens(tokens, context, partials, template, tags);
557+
return this.renderTokens(tokens, context, partials, template, config);
547558
};
548559

549560
/**
@@ -555,7 +566,7 @@
555566
* If the template doesn't use higher-order sections, this argument may
556567
* be omitted.
557568
*/
558-
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) {
569+
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, config) {
559570
var buffer = '';
560571

561572
var token, symbol, value;
@@ -564,11 +575,11 @@
564575
token = tokens[i];
565576
symbol = token[0];
566577

567-
if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
568-
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
569-
else if (symbol === '>') value = this.renderPartial(token, context, partials, tags);
578+
if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate, config);
579+
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate, config);
580+
else if (symbol === '>') value = this.renderPartial(token, context, partials, config);
570581
else if (symbol === '&') value = this.unescapedValue(token, context);
571-
else if (symbol === 'name') value = this.escapedValue(token, context);
582+
else if (symbol === 'name') value = this.escapedValue(token, context, config);
572583
else if (symbol === 'text') value = this.rawValue(token);
573584

574585
if (value !== undefined)
@@ -578,25 +589,25 @@
578589
return buffer;
579590
};
580591

581-
Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
592+
Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate, config) {
582593
var self = this;
583594
var buffer = '';
584595
var value = context.lookup(token[1]);
585596

586597
// This function is used to render an arbitrary template
587598
// in the current context by higher-order sections.
588599
function subRender (template) {
589-
return self.render(template, context, partials);
600+
return self.render(template, context, partials, config);
590601
}
591602

592603
if (!value) return;
593604

594605
if (isArray(value)) {
595606
for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
596-
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
607+
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate, config);
597608
}
598609
} else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
599-
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
610+
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate, config);
600611
} else if (isFunction(value)) {
601612
if (typeof originalTemplate !== 'string')
602613
throw new Error('Cannot use higher-order sections without the original template');
@@ -607,18 +618,18 @@
607618
if (value != null)
608619
buffer += value;
609620
} else {
610-
buffer += this.renderTokens(token[4], context, partials, originalTemplate);
621+
buffer += this.renderTokens(token[4], context, partials, originalTemplate, config);
611622
}
612623
return buffer;
613624
};
614625

615-
Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
626+
Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate, config) {
616627
var value = context.lookup(token[1]);
617628

618629
// Use JavaScript's definition of falsy. Include empty arrays.
619630
// See https://github.com/janl/mustache.js/issues/186
620631
if (!value || (isArray(value) && value.length === 0))
621-
return this.renderTokens(token[4], context, partials, originalTemplate);
632+
return this.renderTokens(token[4], context, partials, originalTemplate, config);
622633
};
623634

624635
Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) {
@@ -632,8 +643,9 @@
632643
return partialByNl.join('\n');
633644
};
634645

635-
Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) {
646+
Writer.prototype.renderPartial = function renderPartial (token, context, partials, config) {
636647
if (!partials) return;
648+
var tags = this.getConfigTags(config);
637649

638650
var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
639651
if (value != null) {
@@ -644,7 +656,8 @@
644656
if (tagIndex == 0 && indentation) {
645657
indentedValue = this.indentPartial(value, indentation, lineHasNonSpace);
646658
}
647-
return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue, tags);
659+
var tokens = this.parse(indentedValue, tags);
660+
return this.renderTokens(tokens, context, partials, indentedValue, config);
648661
}
649662
};
650663

@@ -654,19 +667,41 @@
654667
return value;
655668
};
656669

657-
Writer.prototype.escapedValue = function escapedValue (token, context) {
670+
Writer.prototype.escapedValue = function escapedValue (token, context, config) {
671+
var escape = this.getConfigEscape(config) || mustache.escape;
658672
var value = context.lookup(token[1]);
659673
if (value != null)
660-
return typeof value === 'number' ? String(value) : mustache.escape(value);
674+
return (typeof value === 'number' && escape === mustache.escape) ? String(value) : escape(value);
661675
};
662676

663677
Writer.prototype.rawValue = function rawValue (token) {
664678
return token[1];
665679
};
666680

681+
Writer.prototype.getConfigTags = function getConfigTags (config) {
682+
if (isArray(config)) {
683+
return config;
684+
}
685+
else if (config && typeof config === 'object') {
686+
return config.tags;
687+
}
688+
else {
689+
return undefined;
690+
}
691+
};
692+
693+
Writer.prototype.getConfigEscape = function getConfigEscape (config) {
694+
if (config && typeof config === 'object' && !isArray(config)) {
695+
return config.escape;
696+
}
697+
else {
698+
return undefined;
699+
}
700+
};
701+
667702
var mustache = {
668703
name: 'mustache.js',
669-
version: '4.0.1',
704+
version: '4.1.0',
670705
tags: [ '{{', '}}' ],
671706
clearCache: undefined,
672707
escape: undefined,
@@ -711,19 +746,17 @@
711746
};
712747

713748
/**
714-
* Renders the `template` with the given `view` and `partials` using the
715-
* default writer. If the optional `tags` argument is given here it must be an
716-
* array with two string values: the opening and closing tags used in the
717-
* template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
749+
* Renders the `template` with the given `view`, `partials`, and `config`
750+
* using the default writer.
718751
*/
719-
mustache.render = function render (template, view, partials, tags) {
752+
mustache.render = function render (template, view, partials, config) {
720753
if (typeof template !== 'string') {
721754
throw new TypeError('Invalid template! Template should be a "string" ' +
722755
'but "' + typeStr(template) + '" was given as the first ' +
723756
'argument for mustache#render(template, view, partials)');
724757
}
725758

726-
return defaultWriter.render(template, view, partials, tags);
759+
return defaultWriter.render(template, view, partials, config);
727760
};
728761

729762
// Export the escaping function so that the user may override it.

0 commit comments

Comments
 (0)