@@ -20,8 +20,13 @@ function $RouteProvider(){
20
20
*
21
21
* @param {string } path Route path (matched against `$location.path`). If `$location.path`
22
22
* contains redundant trailing slash or is missing one, the route will still match and the
23
- * `$location.path` will be updated to add or drop the trailing slash to exacly match the
23
+ * `$location.path` will be updated to add or drop the trailing slash to exactly match the
24
24
* route definition.
25
+ *
26
+ * `path` can contain named groups starting with a colon (`:name`). All characters up to the
27
+ * next slash are matched and stored in `$routeParams` under the given `name` when the route
28
+ * matches.
29
+ *
25
30
* @param {Object } route Mapping information to be assigned to `$route.current` on route
26
31
* match.
27
32
*
@@ -286,8 +291,7 @@ function $RouteProvider(){
286
291
* instance of the Controller.
287
292
*/
288
293
289
- var matcher = switchRouteMatcher ,
290
- forceReload = false ,
294
+ var forceReload = false ,
291
295
$route = {
292
296
routes : routes ,
293
297
@@ -315,21 +319,36 @@ function $RouteProvider(){
315
319
316
320
/////////////////////////////////////////////////////
317
321
322
+ /**
323
+ * @param on {string} current url
324
+ * @param when {string} route when template to match the url against
325
+ * @return {?Object }
326
+ */
318
327
function switchRouteMatcher ( on , when ) {
319
328
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
320
329
// regex only once and then reuse it
321
- var regex = '^' + when . replace ( / ( [ \. \\ \( \) \^ \$ ] ) / g, "\\$1" ) + '$' ,
330
+
331
+ // Escape regexp special characters.
332
+ when = '^' + when . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, "\\$&" ) + '$' ;
333
+ var regex = '' ,
322
334
params = [ ] ,
323
335
dst = { } ;
324
- forEach ( when . split ( / [ ^ \w : ] / ) , function ( param ) {
325
- if ( param && param . charAt ( 0 ) === ':' ) {
326
- var paramRegExp = new RegExp ( param + "([\\W])" ) ;
327
- if ( regex . match ( paramRegExp ) ) {
328
- regex = regex . replace ( paramRegExp , "([^\\/]*)$1" ) ;
329
- params . push ( param . substr ( 1 ) ) ;
330
- }
331
- }
332
- } ) ;
336
+
337
+ var re = / : ( \w + ) / g,
338
+ paramMatch ,
339
+ lastMatchedIndex = 0 ;
340
+
341
+ while ( ( paramMatch = re . exec ( when ) ) !== null ) {
342
+ // Find each :param in `when` and replace it with a capturing group.
343
+ // Append all other sections of when unchanged.
344
+ regex += when . slice ( lastMatchedIndex , paramMatch . index ) ;
345
+ regex += '([^\\/]*)' ;
346
+ params . push ( paramMatch [ 1 ] ) ;
347
+ lastMatchedIndex = re . lastIndex ;
348
+ }
349
+ // Append trailing path part.
350
+ regex += when . substr ( lastMatchedIndex ) ;
351
+
333
352
var match = on . match ( new RegExp ( regex ) ) ;
334
353
if ( match ) {
335
354
forEach ( params , function ( name , index ) {
@@ -418,7 +437,7 @@ function $RouteProvider(){
418
437
// Match a route
419
438
var params , match ;
420
439
forEach ( routes , function ( route , path ) {
421
- if ( ! match && ( params = matcher ( $location . path ( ) , path ) ) ) {
440
+ if ( ! match && ( params = switchRouteMatcher ( $location . path ( ) , path ) ) ) {
422
441
match = inherit ( route , {
423
442
params : extend ( { } , $location . search ( ) , params ) ,
424
443
pathParams : params } ) ;
0 commit comments