From 2568ce71863f1ead9308aa2c35c58d31801905fa Mon Sep 17 00:00:00 2001 From: Rafael Xavier de Souza Date: Mon, 20 Feb 2017 22:10:33 -0300 Subject: [PATCH] Date: Parse literals, e.g., separators Fixes #683 --- src/date/tokenizer.js | 2 +- test/functional/date/parse-date.js | 4 ++-- test/unit.js | 3 +++ test/unit/date/parse.js | 7 +++++++ test/unit/util/regexp/escape.js | 18 ++++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/unit/util/regexp/escape.js diff --git a/src/date/tokenizer.js b/src/date/tokenizer.js index cceb9706b..26d37f996 100644 --- a/src/date/tokenizer.js +++ b/src/date/tokenizer.js @@ -374,7 +374,7 @@ return function( value, numberParser, properties ) { default: token.type = "literal"; - tokenRe = /./; + tokenRe = new RegExp( regexpEscape( current ) ); } if ( !tokenRe ) { diff --git a/test/functional/date/parse-date.js b/test/functional/date/parse-date.js index 6244f1dd4..a7ca0d60d 100644 --- a/test/functional/date/parse-date.js +++ b/test/functional/date/parse-date.js @@ -122,10 +122,10 @@ QUnit.test( "should parse time presets", function( assert ) { date.setSeconds( 7 ); date = startOf( date, "second" ); assertParseDate( assert, "5:35:07 PM", { time: "medium" }, date ); - assertParseDate( assert, "٥،٣٥،٠٧ م", { time: "medium" }, date, ar ); + assertParseDate( assert, "٥:٣٥:٠٧ م", { time: "medium" }, date, ar ); date = startOf( date, "minute" ); assertParseDate( assert, "5:35 PM", { time: "short" }, date ); - assertParseDate( assert, "٥،٣٥ م", { time: "short" }, date, ar ); + assertParseDate( assert, "٥:٣٥ م", { time: "short" }, date, ar ); }); QUnit.test( "should parse date presets", function( assert ) { diff --git a/test/unit.js b/test/unit.js index c9dd9174f..50953cb64 100644 --- a/test/unit.js +++ b/test/unit.js @@ -1,6 +1,9 @@ require([ "qunit", + // util + "./unit/util/regexp/escape", + // core "./unit/core", "./unit/core/locale", diff --git a/test/unit/date/parse.js b/test/unit/date/parse.js index 549e55d7a..fce72c9bc 100644 --- a/test/unit/date/parse.js +++ b/test/unit/date/parse.js @@ -569,4 +569,11 @@ QUnit.test( "should parse literal (')", function( assert ) { assertParse( assert, "09 o'clock AM", "hh 'o''clock' a", cldr, date ); }); +QUnit.test( "should parse invalid literal as null", function( assert ) { + assertParse( assert, "2-20-2017", "M/d/y", cldr, null ); + assertParse( assert, "2a20a2017", "M/d/y", cldr, null ); + assertParse( assert, "2/20/2017", "M-d-y", cldr, null ); + assertParse( assert, "2/20/2017x5xAM", "M/d/y h a", cldr, null ); +}); + }); diff --git a/test/unit/util/regexp/escape.js b/test/unit/util/regexp/escape.js new file mode 100644 index 000000000..ba6763711 --- /dev/null +++ b/test/unit/util/regexp/escape.js @@ -0,0 +1,18 @@ +define([ + "src/util/regexp/escape", +], function( regexpEscape ) { + +QUnit.module( "Util regexp escape" ); + +QUnit.test( "it shouldn't escape not-reserved characters", function( assert ) { + assert.equal( regexpEscape( "foo" ), "foo" ); +}); + +QUnit.test( "it should escape reserved characters", function( assert ) { + assert.equal( + regexpEscape( ".*+?^=!:${}()|\[\]\/\\" ), + "\\.\\*\\+\\?\\^\\=\\!\\:\\$\\{\\}\\(\\)\\|\\[\\]\\/\\\\" + ); +}); + +});