Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Add support for escaping characters with the date filter #432

Merged
merged 2 commits into from
Mar 7, 2014
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
5 changes: 4 additions & 1 deletion lib/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ exports.date = function (input, format, offset, abbr) {

for (i; i < l; i += 1) {
cur = format.charAt(i);
if (dateFormatter.hasOwnProperty(cur)) {
if (cur === '\\') {
i += 1;
out += (i < l) ? format.charAt(i) : cur;
} else if (dateFormatter.hasOwnProperty(cur)) {
out += dateFormatter[cur](date, offset, abbr);
} else {
out += cur;
Expand Down
7 changes: 6 additions & 1 deletion tests/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ var n = new Swig(),
{ c: 'v|date("S")', v: makeDate(420, 2011, 8, 13), e: 'th' },
{ c: 'v|date("S")', v: makeDate(420, 2011, 8, 21), e: 'st' },
{ c: 'v|date("S")', v: makeDate(420, 2011, 8, 22), e: 'nd' },
{ c: 'v|date("S")', v: makeDate(420, 2011, 8, 23), e: 'rd' }
{ c: 'v|date("S")', v: makeDate(420, 2011, 8, 23), e: 'rd' },

// Escape character
{ c: 'v|date("\\D")', v: d, e: 'D' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double-slashes are necessary here to keep the TokenParser happy when performing the tests, and are not necessary in typical use. These tests are modelled after the test styling in the addslashes checks.

{ c: 'v|date("\\t\\e\\s\\t")', v: d, e: 'test' },
{ c: 'v|date("\\\\D")', v: d, e: '\\Tue' }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to add a more "real-world" example, and it failed tests...

      { c: 'v|date("jS \o\f F")', v: makeDate(420, 2012, 5, 4), e: '4th of July' }

@ecaron: Do you want to fix?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I may have done this wrong... need the extra escapes. Will re-try later tonight...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just going to say that - you're right that they need extra escapes because the first round of slashes gets lost converting the tests to code (just like in the addslashes example.)

],
'default': [
{ c: 'v|default("tacos")', v: 'foo', e: 'foo' },
Expand Down