Parcourir la source

Beef up entry points for bad input checks.

tags/0.5.0-vsc
unknown il y a 16 ans
Parent
révision
12d2fbb755
3 fichiers modifiés avec 38 ajouts et 5 suppressions
  1. +20
    -5
      mustache.js
  2. +9
    -0
      test/unit.compiler.js
  3. +9
    -0
      test/unit.interpreter.js

+ 20
- 5
mustache.js Voir le fichier

@@ -770,6 +770,11 @@ var Mustache = function() {
Turns a template and view into HTML Turns a template and view into HTML
*/ */
to_html: function(template, view, partials, send_func) { to_html: function(template, view, partials, send_func) {
if (!template) { return ''; }
partials = partials || {};
view = view || {};
var o = send_func ? undefined : []; var o = send_func ? undefined : [];
var s = send_func || function(output) { o.push(output); }; var s = send_func || function(output) { o.push(output); };
@@ -780,11 +785,20 @@ var Mustache = function() {
return o.join(''); return o.join('');
} }
}, },
/*
Compiles a template into an equivalent JS function for faster
repeated execution.
*/
compile: function(template, partials) { compile: function(template, partials) {
if (!template) { return function() { return '' }; }
var p = {}; var p = {};
for (var key in partials) {
if (partials.hasOwnProperty(key)) {
p[key] = partials[key];
if (partials) {
for (var key in partials) {
if (partials.hasOwnProperty(key)) {
p[key] = partials[key];
}
} }
} }
@@ -795,9 +809,10 @@ var Mustache = function() {
renderer.render(template, {}, p); renderer.render(template, {}, p);


return function(view, send_func) { return function(view, send_func) {
var o = send_func ? undefined : [];
var s = send_func || function(output) { o.push(output); };
view = view || {};
var o = send_func ? undefined : [];
var s = send_func || function(output) { o.push(output); };
for (var i=0,n=commands.length; i<n; ++i) { for (var i=0,n=commands.length; i<n; ++i) {
commands[i]([view], s); commands[i]([view], s);


+ 9
- 0
test/unit.compiler.js Voir le fichier

@@ -14,6 +14,15 @@ module('Compiler', {
} }
}); });


test("Argument validation", function() {
expect(4);
equals(Mustache.to_html(undefined), '', 'No parameters');
equals(Mustache.to_html('{{hi}}'), '', ' No View or Partials');
equals(Mustache.to_html('{{hi}}', {hi:'Hi.'}), 'Hi.', 'No Partials');
equals(Mustache.to_html('{{>hi}}', undefined, {hi:'{{p}}'}), '', 'Partial but no view');
});

test("Parser", function() { test("Parser", function() {
expect(4); expect(4);




+ 9
- 0
test/unit.interpreter.js Voir le fichier

@@ -1,5 +1,14 @@
module('Interpreter'); module('Interpreter');


test("Argument validation", function() {
expect(4);
equals(Mustache.to_html(undefined), '', 'No parameters');
equals(Mustache.to_html('{{hi}}'), '', ' No View or Partials');
equals(Mustache.to_html('{{hi}}', {hi:'Hi.'}), 'Hi.', 'No Partials');
equals(Mustache.to_html('{{>hi}}', undefined, {hi:'{{p}}'}), '', 'Partial but no view');
});

test("Parser", function() { test("Parser", function() {
expect(3); expect(3);




Chargement…
Annuler
Enregistrer