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

Normalize NPCG in a regex route's matches to empty strings for undefined #1077

Merged
merged 2 commits into from
Aug 13, 2013
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
7 changes: 5 additions & 2 deletions src/app/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/app/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/app/tests/unit/assets/router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {});
Expand Down