Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ", ', and ` to the list of chars that need HTML escaping. #68

Merged
merged 3 commits into from
May 26, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ Handlebars.SafeString.prototype.toString = function() {
(function() {
var escape = {
"<": "&lt;",
">": "&gt;"
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};

var badChars = /&(?!\w+;)|[<>]/g;
var possible = /[&<>]/;
var badChars = /&(?!\w+;)|[<>"'`]/g;
var possible = /[&<>"'`]/;

var escapeChar = function(chr) {
return escape[chr] || "&amp;"
Expand Down
12 changes: 6 additions & 6 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ test("escaping expressions", function() {
shouldCompileTo("{{{awesome}}}", {awesome: "&\"\\<>"}, '&\"\\<>',
"expressions with 3 handlebars aren't escaped");

shouldCompileTo("{{awesome}}", {awesome: "&\"\\<>"}, '&amp;\"\\&lt;&gt;',
"by default expressions should be escaped");

shouldCompileTo("{{&awesome}}", {awesome: "&\"\\<>"}, '&\"\\<>',
"expressions with {{& handlebars aren't escaped");

shouldCompileTo("{{awesome}}", {awesome: "&\"'`\\<>"}, '&amp;&quot;&#x27;&#x60;\\&lt;&gt;',
"by default expressions should be escaped");

});

test("functions returning safestrings shouldn't be escaped", function() {
Expand Down Expand Up @@ -370,7 +370,7 @@ test("block helper inverted sections", function() {
// so we should see the output of both
shouldCompileTo(string, hash, "<ul><li>Alan</li><li>Yehuda</li></ul>", "an inverse wrapper is passed in as a new context");
shouldCompileTo(string, empty, "<p><em>Nobody's here</em></p>", "an inverse wrapper can be optionally called");
shouldCompileTo(messageString, rootMessage, "<p>Nobody's here</p>", "the context of an inverse is the parent of the block");
shouldCompileTo(messageString, rootMessage, "<p>Nobody&#x27;s here</p>", "the context of an inverse is the parent of the block");
});

module("fallback hash");
Expand Down Expand Up @@ -448,14 +448,14 @@ test("using a quote in the middle of a parameter raises an error", function() {
});

test("escaping a String is possible", function(){
var string = 'Message: {{hello "\\"world\\""}}';
var string = 'Message: {{{hello "\\"world\\""}}}';
var hash = {}
var fallback = {hello: function(param) { return "Hello " + param; }}
shouldCompileTo(string, [hash, fallback], "Message: Hello \"world\"", "template with an escaped String literal");
});

test("it works with ' marks", function() {
var string = 'Message: {{hello "Alan\'s world"}}';
var string = 'Message: {{{hello "Alan\'s world"}}}';
var hash = {}
var fallback = {hello: function(param) { return "Hello " + param; }}
shouldCompileTo(string, [hash, fallback], "Message: Hello Alan's world", "template with a ' mark");
Expand Down