diff --git a/mustache.js b/mustache.js index 5ffdfe6..f8990a8 100644 --- a/mustache.js +++ b/mustache.js @@ -552,8 +552,12 @@ Writer.prototype.unescapedValue = function unescapedValue (token, context) { var value = context.lookup(token[1]); - if (value != null) + if (value != null) { + if (mustache.sanitizeUnescaped) { + return mustache.sanitizeUnescaped(value); + } return value; + } }; Writer.prototype.escapedValue = function escapedValue (token, context) { @@ -621,6 +625,12 @@ // See https://github.com/janl/mustache.js/issues/244 mustache.escape = escapeHtml; + // Export the sanitizing function for unescaped values. + mustache.sanitizeUnescaped = null; + mustache.setUnescapedSanitizier = function setUnescapedSanitizier (sanitizeUnescaped) { + mustache.sanitizeUnescaped = sanitizeUnescaped; + }; + // Export these mainly for testing, but also for advanced usage. mustache.Scanner = Scanner; mustache.Context = Context; diff --git a/test/sanitize-test.js b/test/sanitize-test.js new file mode 100644 index 0000000..98c812b --- /dev/null +++ b/test/sanitize-test.js @@ -0,0 +1,18 @@ +require('./helper'); + +var renderHelper = require('./render-helper'); + +var tests = renderHelper.getTests(); + +describe('Mustache.sanitizeUnescaped', function () { + beforeEach(function () { + Mustache.setUnescapedSanitizier(function(value) { + return value.toUpperCase(); + }); + }); + + it('requires template to be a string', function () { + assert.equal(Mustache.render('{{{value}}}', {value: 'abc'}), + 'ABC'); + }); +});