Skip to content

fix($urlMatcherFactory): early binding of array handler bypasses type resolution #1572

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ Type.prototype.$asArray = function(mode, isSearch) {
return new ArrayType(this, mode);

function ArrayType(type, mode) {
function bindTo(thisObj, callback) {
function bindTo(thisObj, type, callbackName) {
return function() {
return callback.apply(thisObj, arguments);
return type[callbackName].apply(thisObj, arguments);
};
}

Expand Down Expand Up @@ -537,10 +537,10 @@ Type.prototype.$asArray = function(mode, isSearch) {
};
}

this.encode = arrayHandler(bindTo(this, type.encode));
this.decode = arrayHandler(bindTo(this, type.decode));
this.is = arrayHandler(bindTo(this, type.is), true);
this.equals = arrayEqualsHandler(bindTo(this, type.equals));
this.encode = arrayHandler(bindTo(this, type, 'encode'));
this.decode = arrayHandler(bindTo(this, type, 'decode'));
this.is = arrayHandler(bindTo(this, type, 'is'), true);
this.equals = arrayEqualsHandler(bindTo(this, type, 'equals'));
this.pattern = type.pattern;
this.$arrayMode = mode;
}
Expand Down
25 changes: 25 additions & 0 deletions test/urlMatcherFactorySpec.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,31 @@ describe("UrlMatcher", function () {
});
});

describe("urlMatcherFactoryProvider", function () {

describe(".type()", function () {

var m;
beforeEach(module('ui.router.util', function($urlMatcherFactoryProvider) {
$urlMatcherFactoryProvider.type("myType", {}, function() {
return {
decode: function() {
return 'decoded';
}
};
});
m = new UrlMatcher("/test?{foo:myType}");
}));

it("should handle arrays properly with config-time custom type definitions", inject(function ($stateParams) {
expect(m.exec("/test", {foo: '1'})).toEqual({ foo: 'decoded' });
expect(m.exec("/test", {foo: ['1', '2']})).toEqual({ foo: ['decoded', 'decoded'] });
}));

});

});

describe("urlMatcherFactory", function () {

var $umf;
Expand Down