[Pkg-javascript-commits] [leaflet] 146/301: Allow double quotes in template strings fixes #2120
Jonas Smedegaard
js at moszumanska.debian.org
Mon Jan 27 22:22:47 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository leaflet.
commit a7f2d1975f23b3dddd9f012d5d0cf401f3996125
Author: Jan Pieter Waagmeester <jieter at jieter.nl>
Date: Wed Oct 23 15:14:59 2013 +0200
Allow double quotes in template strings fixes #2120
---
spec/suites/core/UtilSpec.js | 13 +++++++++++--
src/core/Util.js | 1 +
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/spec/suites/core/UtilSpec.js b/spec/suites/core/UtilSpec.js
index f26befe..d956422 100644
--- a/spec/suites/core/UtilSpec.js
+++ b/spec/suites/core/UtilSpec.js
@@ -194,25 +194,27 @@ describe('Util', function() {
var str = L.Util.template(tpl, {
foo: 'Vlad',
bar: 'Dave',
- baz:function(o){
+ baz: function (o) {
return o.bar;
}
});
expect(str).to.eql('Hello Vlad and Dave!');
});
+
it('check the cache', function () {
var tpl = 'Hello {foo} and {baz }!';
var str = L.Util._templateCache[tpl]({
foo: 'ladies',
- baz: function() {
+ baz: function () {
return 'gentlemen';
}
});
expect(str).to.eql('Hello ladies and gentlemen!');
});
+
it('evaluates templates with a function', function () {
var tpl = L.Util.compileTemplate('Hello { foo } and { bar}!',{});
@@ -228,10 +230,17 @@ describe('Util', function() {
expect(str1).to.eql('Hello Vlad and Dave!');
expect(str2).to.eql('Hello {Calvin} and {Simon}!');
});
+
it('does not modify text without a token variable', function () {
expect(L.Util.template('foo', {})).to.eql('foo');
});
+ it('supports templates with double quotes', function () {
+ expect(L.Util.template('He said: "{foo}"!', {
+ foo: 'Hello'
+ })).to.eql('He said: "Hello"!');
+ });
+
it('throws when a template token is not given', function () {
expect(function () {
L.Util.template(tpl, {foo: 'bar'});
diff --git a/src/core/Util.js b/src/core/Util.js
index ca13965..5e67b17 100644
--- a/src/core/Util.js
+++ b/src/core/Util.js
@@ -107,6 +107,7 @@ L.Util = {
compileTemplate: function (str, data) {
// based on https://gist.github.com/padolsey/6008842
+ str = str.replace(/"/g, '\\\"');
str = str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
return '" + o["' + key + '"]' + (typeof data[key] === 'function' ? '(o)' : '') + ' + "';
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet.git
More information about the Pkg-javascript-commits
mailing list