Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(input): format numeric string model values correctly #6684

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
3 changes: 3 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,9 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}

ctrl.$formatters.push(function(value) {
if (isString(value) && !isNaN(value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isNaN might not be the best choice here, but I think it's probably ok

value = Number(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we pin the radix to 10? without it it's easy to run into incorrect parsing issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Number is more or less equivalent to parseFloat() when the parameter is a string, I'm not sure it's actually possible to get it to parse an octal or binary number from a string. Using parseFloat() would work just as well, too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as far as I can tell, there's no way to set a radix for an html5 number input type, so supporting this would be a step beyond what html5 input types actually do... I don't necessarily see that as a bad thing, however it's complicated, and users don't really have a way to ask for a specific radix (and if they did, and tried to enter a hex number, a browser with HTML5 constraint validation would call it invalid anyways)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Number("010") => 10, you can't really do octal notation this way...

Interestingly, v8 will translate 0xFF into 255, hmm, so it does most of what parseFloat() will do, but it won't stop if it encounters a character that can't be tokenized as a number (so Number("10abc") => NaN vs parseFloat("10abc") => 10)

}
return validate(ctrl, 'number', ctrl.$isEmpty(value) || isNumber(value), value);
});
}
Expand Down
48 changes: 48 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,54 @@ describe('input', function() {
});


it('should translate numeric base-10 strings in model to numbers', function() {
compileInput('<input type="number" ng-model="age" />');

scope.$apply(function() {
scope.age = '10';
});

expect(scope.age).toBe('10');
expect(inputElm.val()).toBe('10');
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a test case for octal number...



it('should translate numeric base-16 strings in model to numbers', function() {
compileInput('<input type="number" ng-model="age" />');

scope.$apply(function() {
scope.age = '0xFf';
});

expect(scope.age).toBe('0xFf');
expect(inputElm.val()).toBe('255');
});


it('should translate scientific notation from numeric strings in model to numbers', function() {
compileInput('<input type="number" ng-model="age" />');

scope.$apply(function() {
scope.age = '0.1e6';
});

expect(scope.age).toBe('0.1e6');
expect(inputElm.val()).toBe('100000');
});


it('should remove leading zeros from numeric strings in model', function() {
compileInput('<input type="number" ng-model="age" />');

scope.$apply(function() {
scope.age = '010';
});

expect(scope.age).toBe('010');
expect(inputElm.val()).toBe('10');
});


describe('min', function() {

it('should validate', function() {
Expand Down