Browse files

Don't double escape output

Benchmarking this change shows a very slight performance hit:
Winner: regular
Compared with next highest (negative lookahead), it's:
9.5% faster
1.11 times as fast
0.04 order(s) of magnitude faster

closes gh-5
  • Loading branch information...
1 parent 5c5f702 commit 1ac8334b7f81309f5c4f1e2ff3592b75d145f358 @paularmstrong committed Sep 27, 2011
Showing with 3 additions and 2 deletions.
  1. +1 −1 lib/helpers.js
  2. +2 −1 tests/helpers.test.js
View
2 lib/helpers.js
@@ -135,7 +135,7 @@ exports.escape = function (variable, context) {
exports.escaper = function (input) {
if (typeof input === 'string') {
- return input.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
+ return input.replace(/&(?!amp;|lt;|gt;|quot;|#39;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
} else {
return input;
}
View
3 tests/helpers.test.js
@@ -61,6 +61,7 @@ exports.isValidBlockName = function (test) {
};
exports.escaper = function (test) {
- test.strictEqual(helpers.escaper('hi<foo> & "bar" \'baz\''), 'hi&lt;foo&gt; &amp; &quot;bar&quot; &#39;baz&#39;');
+ test.strictEqual(helpers.escaper('hi<foo> & "bar" \'baz\''), 'hi&lt;foo&gt; &amp; &quot;bar&quot; &#39;baz&#39;', 'escape input');
+ test.strictEqual(helpers.escaper('hi&lt;foo&gt; &amp; &quot;bar&quot; &#39;baz&#39;'), 'hi&lt;foo&gt; &amp; &quot;bar&quot; &#39;baz&#39;', 'input is not double-escaped');
test.done();
};

0 comments on commit 1ac8334

Please sign in to comment.