Skip to content

Commit b6d6bda

Browse files
committed
Strings matched by the encoding regular expression, but not found in the encoding map are now removed from the output.
Added single quotes to the default characters to encode: ' => &blueimp#39; This allows safely using single quotes for attributes in HTML documents, e.g. for embedding JSON, which requires double quotes for its own properties and string values.
1 parent a557f70 commit b6d6bda

File tree

9 files changed

+29
-31
lines changed

9 files changed

+29
-31
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,18 @@ result = tmpl("tmpl-demo", data); // Loads and parses the template again
155155
```
156156

157157
### Output encoding
158-
The method **tmpl.encode** is used to escape HTML special characters in template output:
158+
The method **tmpl.encode** is used to escape HTML special characters in the template output:
159159

160160
```js
161-
var output = tmpl.encode("<>&\"\x00"); // Renders "&lt;&gt;&amp;&quot;"
161+
var output = tmpl.encode("<>&\"'\x00"); // Renders "&lt;&gt;&amp;&quot;&#39;"
162162
```
163163

164-
**tmpl.encode** makes use of the regular expression **tmpl.encReg** and the encoding map **tmpl.encMap** to match and replace special characters, which can be modified to change the behavior of the output encoding:
164+
**tmpl.encode** makes use of the regular expression **tmpl.encReg** and the encoding map **tmpl.encMap** to match and replace special characters, which can be modified to change the behavior of the output encoding.
165+
Strings matched by the regular expression, but not found in the encoding map are removed from the output. This allows for example to automatically trim input values (removing whitespace from the start and end of the string):
165166

166167
```js
167-
// Add single quotes to the encoding rules:
168-
tmpl.encReg = /[<>&"'\x00]/g;
169-
tmpl.encMap["'"] = "&#39;";
170-
171-
var output = tmpl.encode("<>&\"'\x00"); // Renders "&lt;&gt;&amp;&quot;&#39;"
168+
tmpl.encReg = /(^\s+)|(\s+$)|[<>&"'\x00]/g;
169+
var output = tmpl.encode(" Banana! "); // Renders "Banana" (without whitespace)
172170
```
173171

174172
### Local helper variables

compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
/*
3-
* JavaScript Templates Compiler 2.0
3+
* JavaScript Templates Compiler 2.1.0
44
* https://github.com/blueimp/JavaScript-Templates
55
*
66
* Copyright 2011, Sebastian Tschan

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE HTML>
22
<!--
33
/*
4-
* JavaScript Templates Demo 2.0
4+
* JavaScript Templates Demo 2.1.0
55
* https://github.com/blueimp/JavaScript-Templates
66
*
77
* Copyright 2011, Sebastian Tschan

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-tmpl",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"title": "JavaScript Templates",
55
"description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.",
66
"keywords": [

runtime.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Runtime 2.0
2+
* JavaScript Templates Runtime 2.1.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -20,19 +20,19 @@
2020
};
2121
};
2222
tmpl.cache = {};
23-
tmpl.encReg = /[<>&"\x00]/g;
23+
tmpl.encReg = /[<>&"'\x00]/g;
2424
tmpl.encMap = {
25-
"<": "&lt;",
26-
">": "&gt;",
27-
"&": "&amp;",
28-
"\"": "&quot;",
29-
"\x00": ""
25+
"<" : "&lt;",
26+
">" : "&gt;",
27+
"&" : "&amp;",
28+
"\"" : "&quot;",
29+
"'" : "&#39;"
3030
};
3131
tmpl.encode = function (s) {
3232
return String(s || "").replace(
3333
tmpl.encReg,
3434
function (c) {
35-
return tmpl.encMap[c];
35+
return tmpl.encMap[c] || "";
3636
}
3737
);
3838
};

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE HTML>
22
<!--
33
/*
4-
* JavaScript Templates Test 2.0
4+
* JavaScript Templates Test 2.1.0
55
* https://github.com/blueimp/JavaScript-Templates
66
*
77
* Copyright 2011, Sebastian Tschan

test/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Test 2.0
2+
* JavaScript Templates Test 2.1.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -31,7 +31,7 @@
3131
nullValue: null,
3232
falseValue: false,
3333
zeroValue: 0,
34-
special: '<>&"\x00',
34+
special: '<>&"\'\x00',
3535
list: [1, 2, 3, 4, 5],
3636
func: function () {
3737
return this.value;
@@ -84,14 +84,14 @@
8484
it('Escape HTML special characters with {%=o.prop%}', function () {
8585
expect(
8686
tmpl('{%=o.special%}', data),
87-
'&lt;&gt;&amp;&quot;'
87+
'&lt;&gt;&amp;&quot;&#39;'
8888
);
8989
});
9090

9191
it('Allow HTML special characters with {%#o.prop%}', function () {
9292
expect(
9393
tmpl('{%#o.special%}', data),
94-
'<>&"\x00'
94+
'<>&"\'\x00'
9595
);
9696
});
9797

@@ -176,14 +176,14 @@
176176
it('Escape HTML special characters with print(data)', function () {
177177
expect(
178178
tmpl('{% print(o.special); %}', data),
179-
'&lt;&gt;&amp;&quot;'
179+
'&lt;&gt;&amp;&quot;&#39;'
180180
);
181181
});
182182

183183
it('Allow HTML special characters with print(data, true)', function () {
184184
expect(
185185
tmpl('{% print(o.special, true); %}', data),
186-
'<>&"\x00'
186+
'<>&"\'\x00'
187187
);
188188
});
189189

tmpl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates 2.0
2+
* JavaScript Templates 2.1.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -57,19 +57,19 @@
5757
return "_s+='";
5858
}
5959
};
60-
tmpl.encReg = /[<>&"\x00]/g;
60+
tmpl.encReg = /[<>&"'\x00]/g;
6161
tmpl.encMap = {
6262
"<" : "&lt;",
6363
">" : "&gt;",
6464
"&" : "&amp;",
6565
"\"" : "&quot;",
66-
"\x00": ""
66+
"'" : "&#39;"
6767
};
6868
tmpl.encode = function (s) {
6969
return String(s || "").replace(
7070
tmpl.encReg,
7171
function (c) {
72-
return tmpl.encMap[c];
72+
return tmpl.encMap[c] || "";
7373
}
7474
);
7575
};

tmpl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)