From 421d3a5b89e26552cfa2c4165cfc5e429dfbef4e Mon Sep 17 00:00:00 2001 From: Larry Davis Date: Fri, 14 Sep 2012 16:31:54 -0700 Subject: [PATCH 1/3] Use getNamespaceDeclaration to declare namespace and JSON.stringify to escape template names --- tasks/jst.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tasks/jst.js b/tasks/jst.js index eaa4a76..c2ee048 100644 --- a/tasks/jst.js +++ b/tasks/jst.js @@ -11,8 +11,7 @@ module.exports = function(grunt) { "use strict"; var _ = require("underscore"); - - var escapeQuote = function(name) { return name.replace("'","\\'"); }; + var helpers = require('grunt-contrib-lib').init(grunt); // filename conversion for templates var defaultProcessName = function(name) { return name; }; @@ -32,7 +31,7 @@ module.exports = function(grunt) { var compiled, srcFiles, src, filename; var output = []; - var namespace = "this['" + options.namespace + "']"; + var nsInfo = helpers.getNamespaceDeclaration(options.namespace); this.files.forEach(function(files) { srcFiles = grunt.file.expandFiles(files.src); @@ -46,12 +45,12 @@ module.exports = function(grunt) { grunt.fail.warn("JST failed to compile."); } - filename = escapeQuote(processName(file)); - output.push(namespace+"['"+filename+"'] = "+compiled+";"); + filename = processName(file); + output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";"); }); if(output.length > 0) { - output.unshift(namespace + " = " + namespace + " || {};"); + output.unshift(nsInfo.declaration); grunt.file.write(files.dest, output.join("\n\n")); grunt.log.writeln("File '" + files.dest + "' created."); } From 5def75a42fe43e4b91be170c87d3372377751f67 Mon Sep 17 00:00:00 2001 From: Larry Davis Date: Fri, 14 Sep 2012 16:32:44 -0700 Subject: [PATCH 2/3] Tests for getNamespaceDeclaration, moved expected test results into test/expected --- grunt.js | 20 ++++++++++++++++++-- test/expected/jst.js | 11 +++++++++++ test/expected/ns_nested.js | 13 +++++++++++++ test/expected/uglyfile.js | 17 +++++++++++++++++ "test/fixtures/it\"s-a-bad-filename.html" | 1 + test/jst_test.js | 14 +++++++++++--- 6 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 test/expected/jst.js create mode 100644 test/expected/ns_nested.js create mode 100644 test/expected/uglyfile.js create mode 100644 "test/fixtures/it\"s-a-bad-filename.html" diff --git a/grunt.js b/grunt.js index e4adda3..aa97cf3 100644 --- a/grunt.js +++ b/grunt.js @@ -42,12 +42,28 @@ module.exports = function(grunt) { jst: { compile: { files: { - "tmp/jst.js": ["test/fixtures/*.html"] + "tmp/jst.js": ["test/fixtures/template.html"] } }, uglyfile: { files: { - "tmp/uglyfile.js": ["test/fixtures/*bad*"] + "tmp/uglyfile.js": ["test/fixtures/*bad-filename*"] + } + }, + ns_nested: { + options: { + namespace: "MyApp.JST.Main" + }, + files: { + "tmp/ns_nested.js": ["test/fixtures/template.html"] + } + }, + ns_nested_this: { + options: { + namespace: "this.MyApp.JST.Main" + }, + files: { + "tmp/ns_nested_this.js": ["test/fixtures/template.html"] } } }, diff --git a/test/expected/jst.js b/test/expected/jst.js new file mode 100644 index 0000000..cfa694d --- /dev/null +++ b/test/expected/jst.js @@ -0,0 +1,11 @@ +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/template.html"] = function(obj){ +var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')}; +with(obj||{}){ +__p+=''+ +( title )+ +''; +} +return __p; +}; \ No newline at end of file diff --git a/test/expected/ns_nested.js b/test/expected/ns_nested.js new file mode 100644 index 0000000..9ecbd92 --- /dev/null +++ b/test/expected/ns_nested.js @@ -0,0 +1,13 @@ +this["MyApp"] = this["MyApp"] || {}; +this["MyApp"]["JST"] = this["MyApp"]["JST"] || {}; +this["MyApp"]["JST"]["Main"] = this["MyApp"]["JST"]["Main"] || {}; + +this["MyApp"]["JST"]["Main"]["test/fixtures/template.html"] = function(obj){ +var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')}; +with(obj||{}){ +__p+=''+ +( title )+ +''; +} +return __p; +}; \ No newline at end of file diff --git a/test/expected/uglyfile.js b/test/expected/uglyfile.js new file mode 100644 index 0000000..044ab97 --- /dev/null +++ b/test/expected/uglyfile.js @@ -0,0 +1,17 @@ +this["JST"] = this["JST"] || {}; + +this["JST"]["test/fixtures/it\"s-a-bad-filename.html"] = function(obj){ +var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')}; +with(obj||{}){ +__p+='never name your file like this.'; +} +return __p; +}; + +this["JST"]["test/fixtures/it's-a-bad-filename.html"] = function(obj){ +var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')}; +with(obj||{}){ +__p+='never name your file like this.'; +} +return __p; +}; \ No newline at end of file diff --git "a/test/fixtures/it\"s-a-bad-filename.html" "b/test/fixtures/it\"s-a-bad-filename.html" new file mode 100644 index 0000000..6f82484 --- /dev/null +++ "b/test/fixtures/it\"s-a-bad-filename.html" @@ -0,0 +1 @@ +never name your file like this. \ No newline at end of file diff --git a/test/jst_test.js b/test/jst_test.js index 1a3d7c1..4b78829 100644 --- a/test/jst_test.js +++ b/test/jst_test.js @@ -6,16 +6,24 @@ exports['jst'] = { var expect, result; - test.expect(2); + test.expect(4); - expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};\n\nthis[\'JST\'][\'test/fixtures/template.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'\'+\n( title )+\n\'\';\n}\nreturn __p;\n};'; + expect = grunt.file.read("test/expected/jst.js"); result = grunt.file.read("tmp/jst.js"); test.equal(expect, result, "should compile underscore templates into JST"); - expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};'; + expect = grunt.file.read("test/expected/uglyfile.js"); result = grunt.file.read("tmp/uglyfile.js"); test.equal(expect, result, "should escape single quotes in filenames"); + expect = grunt.file.read("test/expected/ns_nested.js"); + result = grunt.file.read("tmp/ns_nested.js"); + test.equal(expect, result, "should define parts of nested namespaces"); + + expect = grunt.file.read("test/expected/ns_nested.js"); // same as previous test + result = grunt.file.read("tmp/ns_nested_this.js"); + test.equal(expect, result, "should define parts of nested namespaces, ignoring this."); + test.done(); } }; \ No newline at end of file From e1dcb8d3d5e5f5c2f48508faa09da4899d6e3033 Mon Sep 17 00:00:00 2001 From: Larry Davis Date: Fri, 14 Sep 2012 16:36:52 -0700 Subject: [PATCH 3/3] Added detailed information and example for namespace option --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b23f153..2c27e3e 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,14 @@ This controls how this task (and its helpers) operate and should contain key:val ##### namespace ```string``` -The namespace in which the resulting JST templates are assigned to. +The namespace, in dot notation, in which the resulting JST templates are assigned to. The example below assigns templates to the `MyApp.Templates` namespace: + +Example: +``` javascript +options: { + namespace: 'MyApp.Templates' +} +``` ##### processName ```function```