Skip to content

Commit 4a19a46

Browse files
Iain MacNeillExpFront
Iain MacNeill
authored andcommitted
fix(urlMatcherFactory.js): change to allow url query param names to contain periods
This change modifies the searchPlaceholder regex to include the period character as valid for url query param names. This change also modifies the regex found in the addParameter function which validates the param name just prior to adding to the list of parameters. A new set of unit tests were added to urlMatcherFactorySpec to ensure this modification works correctly. This change resolves issue angular-ui#2395
1 parent bf94d47 commit 4a19a46

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/urlMatcherFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
8282
// \\. - a backslash escape
8383
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
8484
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
85-
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
85+
searchPlaceholder = /([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
8686
compiled = '^', last = 0, m,
8787
segments = this.segments = [],
8888
parentParams = parentMatcher ? parentMatcher.params : {},
@@ -92,7 +92,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
9292
function addParameter(id, type, config, location) {
9393
paramNames.push(id);
9494
if (parentParams[id]) return parentParams[id];
95-
if (!/^\w+(-+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
95+
if (!/^\w+([-.]+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
9696
if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
9797
params[id] = new $$UMFP.Param(id, type, config, location);
9898
return params[id];

test/urlMatcherFactorySpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ describe("UrlMatcher", function () {
8888
});
8989
});
9090

91+
describe("parameters containing periods", function() {
92+
it("should match if properly formatted", function() {
93+
var matcher = new UrlMatcher('/users/?from&to&with.periods&with.periods.also');
94+
var params = matcher.parameters();
95+
96+
expect(params.length).toBe(4);
97+
expect(params).toContain('from');
98+
expect(params).toContain('to');
99+
expect(params).toContain('with.periods');
100+
expect(params).toContain('with.periods.also');
101+
expect(matcher.parameters()).toEqual(['from','to','with.periods','with.periods.also']);
102+
});
103+
104+
it("should not match if invalid", function() {
105+
var err = "Invalid parameter name '.periods' in pattern '/users/?from&to&.periods'";
106+
expect(function() { new UrlMatcher('/users/?from&to&.periods'); }).toThrow(err);
107+
108+
err = "Invalid parameter name 'periods.' in pattern '/users/?from&to&periods.'";
109+
expect(function() { new UrlMatcher('/users/?from&to&periods.'); }).toThrow(err);
110+
});
111+
});
112+
91113
describe(".exec()", function() {
92114
it("should capture parameter values", function () {
93115
var m = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');

0 commit comments

Comments
 (0)