Skip to content

Commit

Permalink
Merge pull request #34 from iamleppert/escape-single-quote
Browse files Browse the repository at this point in the history
Escape a single quote apostrophe in escapeHtml
  • Loading branch information
vybs committed May 21, 2012
2 parents 76e825e + a1caed0 commit a204699
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,19 @@ Tap.prototype.go = function(value) {
return value;
};

var HCHARS = new RegExp(/[&<>\"]/),
var HCHARS = new RegExp(/[&<>\"\']/),
AMP = /&/g,
LT = /</g,
GT = />/g,
QUOT = /\"/g;
QUOT = /\"/g,
SQUOT = /\'/g;

dust.escapeHtml = function(s) {
if (typeof s === "string") {
if (!HCHARS.test(s)) {
return s;
}
return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;');
return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;').replace(SQUOT, '&#39;');
}
return s;
};
Expand Down
4 changes: 2 additions & 2 deletions test/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ exports.dustExamples = [
name: "escaped",
source: "{safe|s}{~n}{unsafe}",
context: { safe: "<script>alert('Hello!')</script>", unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert(&#39;Goodbye!&#39;)&lt;/script&gt;"
},
{
name: "escape_pragma",
source: "{%esc:s}\n {unsafe}{~n}\n {%esc:h}\n {unsafe}\n {/esc}\n{/esc}",
context: { unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert(&#39;Goodbye!&#39;)&lt;/script&gt;"
},
{
name: "else_block",
Expand Down
4 changes: 2 additions & 2 deletions test/jasmine-test/spec/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ var dustExamples = [
name: "escaped",
source: "{safe|s}{~n}{unsafe}",
context: { safe: "<script>alert('Hello!')</script>", unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;",
expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert(&#39;Goodbye!&#39;)&lt;/script&gt;",
message: "should test escaped characters"
},
{
name: "escape_pragma",
source: "{%esc:s}\n {unsafe}{~n}\n {%esc:h}\n {unsafe}\n {/esc}\n{/esc}",
context: { unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;",
expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert(&#39;Goodbye!&#39;)&lt;/script&gt;",
message: "should test escape_pragma"
},
{
Expand Down

0 comments on commit a204699

Please sign in to comment.