From e320cc3dd2fbaf7629498fa342d12e4130d322c4 Mon Sep 17 00:00:00 2001 From: Eric Ferraiuolo Date: Mon, 12 Aug 2013 14:10:45 -0400 Subject: [PATCH 1/2] Normalize NPCG in a regex route's matches to empty strings for undefined Fixes #1076 --- src/app/js/router.js | 7 ++++++- src/app/tests/unit/assets/router-test.js | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/app/js/router.js b/src/app/js/router.js index 3b919675b38..9e66727c3ca 100644 --- a/src/app/js/router.js +++ b/src/app/js/router.js @@ -670,7 +670,12 @@ Y.Router = Y.extend(Router, Y.Base, { // Decode each of the path matches so that the any URL-encoded // path segments are decoded in the `req.params` object. matches = YArray.map(route.regex.exec(path) || [], function (match) { - return match && decode(match); + // Decode matches, or coerce `undefined` matches to an empty + // string to match expectations of working with `req.params` + // in the content of route dispatching, and normalize + // browser differences in their handling of regexp NPCGs: + // https://github.com/yui/yui3/issues/1076 + return (match && decode(match)) || ''; }); // Use named keys for parameter names if the route path contains diff --git a/src/app/tests/unit/assets/router-test.js b/src/app/tests/unit/assets/router-test.js index d739e201b5f..2f9e38c2026 100644 --- a/src/app/tests/unit/assets/router-test.js +++ b/src/app/tests/unit/assets/router-test.js @@ -993,14 +993,14 @@ routerSuite.add(new Y.Test.Case({ calls += 1; Assert.isArray(req.params); - ArrayAssert.itemsAreSame(['/fnord/quux', 'fnord', 'fnord', undefined, 'quux'], req.params); + ArrayAssert.itemsAreSame(['/fnord/quux', 'fnord', 'fnord', '', 'quux'], req.params); }); router.route(/^\/((blorp)|(blerf))\/(quux)$/, function (req) { calls += 1; Assert.isArray(req.params); - ArrayAssert.itemsAreSame(['/blerf/quux', 'blerf', undefined, 'blerf', 'quux'], req.params); + ArrayAssert.itemsAreSame(['/blerf/quux', 'blerf', '', 'blerf', 'quux'], req.params); }); router._dispatch('/foo/one/two', {}); From 8ca52d6d396c4984fa36f10e8e2f45e8d46bb891 Mon Sep 17 00:00:00 2001 From: Eric Ferraiuolo Date: Mon, 12 Aug 2013 14:31:33 -0400 Subject: [PATCH 2/2] Update Router's HISTORY for coercing NPCG `undefined`s to empty strings --- src/app/HISTORY.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/app/HISTORY.md b/src/app/HISTORY.md index 6ae936bd86e..43345faf171 100644 --- a/src/app/HISTORY.md +++ b/src/app/HISTORY.md @@ -13,13 +13,16 @@ App Framework Change History ### Router * Fixed issue with trying to URL-decode matching path segments that are - `undefined`. Routes defined as Regexps (instead of strings) can contain an + `undefined`. Routes defined as regexps (instead of strings) can contain an arbitrary number of captures; when executing the regex during dispatching, its array of matches can contain `undefined` values. Router will now check that a - match is a truthy value before trying to URL-decode it. + match is a truthy value before trying to URL-decode it, and coerce `undefined` + values to empty strings. ([#964][], [#1076][]) +[#964]: https://github.com/yui/yui3/issues/964 [#1004]: https://github.com/yui/yui3/issues/1004 +[#1076]: https://github.com/yui/yui3/issues/1076 3.11.0