Skip to content

Commit 35a1544

Browse files
committed
Preserve whitespace in templates.
1 parent 7ecde98 commit 35a1544

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,23 @@ The template contents are matched and replaced using the regular expression **tm
227227
To use different tags for the template syntax, override **tmpl.regexp** with a modified regular expression, by exchanging all occurrences of "**\\{%**" and "**%\\}**", e.g. with "**\\[%**" and "**%\\]**":
228228

229229
```js
230-
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;
230+
tmpl.regexp = /([\s'\\])(?![^%]*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;
231+
```
232+
233+
By default, the plugin preserves whitespace (newlines, carriage returns, tabs and spaces). To strip unnecessary whitespace, you can override the **tmpl.func** function, e.g. with the following code:
234+
235+
```js
236+
var originalFunc = tmpl.func;
237+
tmpl.func = function (s, p1, p2, p3, p4, p5, offset, str) {
238+
if (p1 && /\s/.test(p1)) {
239+
if (!offset || /\s/.test(str.charAt(offset - 1)) ||
240+
/^\s+$/g.test(str.slice(offset))) {
241+
return '';
242+
}
243+
return ' ';
244+
}
245+
return originalFunc.apply(tmpl, arguments);
246+
};
231247
```
232248

233249
## Templates syntax

test/test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
if (typeof require !== 'undefined') {
1818
// Override the template loading method:
1919
tmpl.load = function (id) {
20-
return require('fs').readFileSync('./test/' + id + '.html', 'utf8');
20+
return require('fs')
21+
.readFileSync('./test/' + id + '.html', 'utf8');
2122
};
2223
}
2324

@@ -157,6 +158,17 @@
157158
);
158159
});
159160

161+
it('Preserve whitespace', function () {
162+
expect(
163+
tmpl(
164+
'\n\r\t{%=o.value%} \n\r\t{%=o.value%} ',
165+
data
166+
)
167+
).to.be(
168+
'\n\r\tvalue \n\r\tvalue '
169+
);
170+
});
171+
160172
});
161173

162174
describe('Evaluation', function () {
@@ -230,7 +242,7 @@
230242
'{% for (var i=0; i<o.list.length; i++) {' +
231243
'include("template", {value: o.list[i]});} %}',
232244
data
233-
)
245+
).replace(/[\r\n]/g, '')
234246
).to.be(
235247
'12345'
236248
);

tmpl.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,35 @@
3434
tmpl.load = function (id) {
3535
return document.getElementById(id).innerHTML;
3636
};
37-
tmpl.regexp = /(\s+)|('|\\)(?![^%]*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g;
38-
tmpl.func = function (s, p1, p2, p3, p4, p5, p6, o, str) {
39-
if (p1) { // whitespace
40-
return o && o + s.length !== str.length ? " " : "";
37+
tmpl.regexp = /([\s'\\])(?![^%]*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g;
38+
tmpl.func = function (s, p1, p2, p3, p4, p5) {
39+
if (p1) { // whitespace, quote and backspace in interpolation context
40+
return {
41+
"\n": "\\n",
42+
"\r": "\\r",
43+
"\t": "\\t",
44+
" " : " "
45+
}[s] || "\\" + s;
4146
}
42-
if (p2) { // single quote or backslash
43-
return "\\" + s;
44-
}
45-
if (p3) { // interpolation: {%=prop%}, or unescaped: {%#prop%}
46-
if (p3 === "=") {
47-
return "'+_e(" + p4 + ")+'";
47+
if (p2) { // interpolation: {%=prop%}, or unescaped: {%#prop%}
48+
if (p2 === "=") {
49+
return "'+_e(" + p3 + ")+'";
4850
}
49-
return "'+(" + p4 + "||'')+'";
51+
return "'+(" + p3 + "||'')+'";
5052
}
51-
if (p5) { // evaluation start tag: {%
53+
if (p4) { // evaluation start tag: {%
5254
return "';";
5355
}
54-
if (p6) { // evaluation end tag: %}
56+
if (p5) { // evaluation end tag: %}
5557
return "_s+='";
5658
}
5759
};
5860
tmpl.encReg = /[<>&"\x00]/g;
5961
tmpl.encMap = {
60-
"<": "&lt;",
61-
">": "&gt;",
62-
"&": "&amp;",
63-
"\"": "&quot;",
62+
"<" : "&lt;",
63+
">" : "&gt;",
64+
"&" : "&amp;",
65+
"\"" : "&quot;",
6466
"\x00": ""
6567
};
6668
tmpl.encode = function (s) {

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)