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

Commit e73a853

Browse files
committed
fix(input): fix step validation for input[number][ng-range-input]
Related to 9a8b8aa and #15257. Fixes the issue discussed in 9a8b8aa#commitcomment-19108436. Fixes #15257 Closes #15264
1 parent 9bda994 commit e73a853

File tree

2 files changed

+135
-8
lines changed

2 files changed

+135
-8
lines changed

src/ng/directive/input.js

+52-3
Original file line numberDiff line numberDiff line change
@@ -1546,13 +1546,62 @@ function parseNumberAttrVal(val) {
15461546
return !isNumberNaN(val) ? val : undefined;
15471547
}
15481548

1549+
function isNumberInteger(num) {
1550+
// See http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript#14794066
1551+
// (minus the assumption that `num` is a number)
1552+
1553+
// eslint-disable-next-line no-bitwise
1554+
return (num | 0) === num;
1555+
}
1556+
1557+
function countDecimals(num) {
1558+
var numString = num.toString();
1559+
var decimalSymbolIndex = numString.indexOf('.');
1560+
1561+
if (decimalSymbolIndex === -1) {
1562+
if (-1 < num && num < 1) {
1563+
// It may be in the exponential notation format (`1e-X`)
1564+
var match = /e-(\d+)$/.exec(numString);
1565+
1566+
if (match) {
1567+
return Number(match[1]);
1568+
}
1569+
}
1570+
1571+
return 0;
1572+
}
1573+
1574+
return numString.length - decimalSymbolIndex - 1;
1575+
}
1576+
1577+
function isValidForStep(viewValue, stepBase, step) {
1578+
// At this point `stepBase` and `step` are expected to be non-NaN values
1579+
// and `viewValue` is expected to be a valid stringified number.
1580+
var value = Number(viewValue);
1581+
1582+
// Due to limitations in Floating Point Arithmetic (e.g. `0.3 - 0.2 !== 0.1` or
1583+
// `0.5 % 0.1 !== 0`), we need to convert all numbers to integers.
1584+
if (!isNumberInteger(value) || !isNumberInteger(stepBase) || !isNumberInteger(step)) {
1585+
var decimalCount = Math.max(countDecimals(value), countDecimals(stepBase), countDecimals(step));
1586+
var multiplier = Math.pow(10, decimalCount);
1587+
1588+
value = value * multiplier;
1589+
stepBase = stepBase * multiplier;
1590+
step = step * multiplier;
1591+
}
1592+
1593+
return (value - stepBase) % step === 0;
1594+
}
1595+
15491596
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15501597
badInputChecker(scope, element, attr, ctrl);
15511598
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
15521599
numberFormatterParser(ctrl);
15531600

1601+
var minVal;
1602+
var maxVal;
1603+
15541604
if (isDefined(attr.min) || attr.ngMin) {
1555-
var minVal;
15561605
ctrl.$validators.min = function(value) {
15571606
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
15581607
};
@@ -1565,7 +1614,6 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15651614
}
15661615

15671616
if (isDefined(attr.max) || attr.ngMax) {
1568-
var maxVal;
15691617
ctrl.$validators.max = function(value) {
15701618
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;
15711619
};
@@ -1637,7 +1685,8 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16371685
} :
16381686
// ngStep doesn't set the setp attr, so the browser doesn't adjust the input value as setting step would
16391687
function stepValidator(modelValue, viewValue) {
1640-
return ctrl.$isEmpty(viewValue) || isUndefined(stepVal) || viewValue % stepVal === 0;
1688+
return ctrl.$isEmpty(viewValue) || isUndefined(stepVal) ||
1689+
isValidForStep(viewValue, minVal || 0, stepVal);
16411690
};
16421691

16431692
setInitialValueAndObserver('step', stepChange);

test/ng/directive/inputSpec.js

+83-5
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,6 @@ describe('input', function() {
27922792
});
27932793

27942794
describe('range', function() {
2795-
27962795
var scope;
27972796

27982797
var rangeTestEl = angular.element('<input type="range">');
@@ -2859,7 +2858,6 @@ describe('input', function() {
28592858
expect(scope.age).toBe(50);
28602859
expect(inputElm).toBeValid();
28612860
});
2862-
28632861
} else {
28642862

28652863
it('should reset the model if view is invalid', function() {
@@ -3249,16 +3247,16 @@ describe('input', function() {
32493247
expect(scope.value).toBe(40);
32503248
});
32513249
});
3252-
32533250
}
32543251

32553252

32563253
describe('step', function() {
32573254

32583255
if (supportsRange) {
32593256
// Browsers that implement range will never allow you to set a value that doesn't match the step value
3260-
// However, currently only Firefox fully inplements the spec when setting the value after the step value changes.
3257+
// However, currently only Firefox fully implements the spec when setting the value after the step value changes.
32613258
// Other browsers fail in various edge cases, which is why they are not tested here.
3259+
32623260
it('should round the input value to the nearest step on user input', function() {
32633261
var inputElm = compileRangeInput('ng-model="value" name="alias" step="5"');
32643262

@@ -3321,8 +3319,8 @@ describe('input', function() {
33213319
expect(scope.value).toBe(10);
33223320
expect(scope.form.alias.$error.step).toBeFalsy();
33233321
});
3324-
33253322
} else {
3323+
33263324
it('should validate if "range" is not implemented', function() {
33273325
scope.step = 10;
33283326
scope.value = 20;
@@ -3395,6 +3393,86 @@ describe('input', function() {
33953393
expect(inputElm.val()).toBe('10');
33963394
expect(scope.form.alias.$error.step).toBeFalsy();
33973395
});
3396+
3397+
it('should use the correct "step base" when `[min]` is specified', function() {
3398+
$rootScope.min = 5;
3399+
$rootScope.step = 10;
3400+
$rootScope.value = 10;
3401+
var inputElm = compileRangeInput('ng-model="value" min="{{min}}" step="{{step}}"');
3402+
var ngModel = inputElm.controller('ngModel');
3403+
3404+
expect(inputElm.val()).toBe('10');
3405+
expect(inputElm).toBeInvalid();
3406+
expect(ngModel.$error.step).toBe(true);
3407+
expect($rootScope.value).toBeUndefined();
3408+
3409+
helper.changeInputValueTo('15');
3410+
expect(inputElm).toBeValid();
3411+
expect($rootScope.value).toBe(15);
3412+
3413+
$rootScope.$apply('step = 3');
3414+
expect(inputElm.val()).toBe('15');
3415+
expect(inputElm).toBeInvalid();
3416+
expect(ngModel.$error.step).toBe(true);
3417+
expect($rootScope.value).toBeUndefined();
3418+
3419+
helper.changeInputValueTo('8');
3420+
expect(inputElm).toBeValid();
3421+
expect($rootScope.value).toBe(8);
3422+
3423+
$rootScope.$apply('min = 10; step = 20; value = 30');
3424+
expect(inputElm.val()).toBe('30');
3425+
expect(inputElm).toBeValid();
3426+
expect($rootScope.value).toBe(30);
3427+
3428+
$rootScope.$apply('min = 5');
3429+
expect(inputElm.val()).toBe('30');
3430+
expect(inputElm).toBeInvalid();
3431+
expect(ngModel.$error.step).toBe(true);
3432+
expect($rootScope.value).toBeUndefined();
3433+
3434+
$rootScope.$apply('step = 0.00000001');
3435+
expect(inputElm.val()).toBe('30');
3436+
expect(inputElm).toBeValid();
3437+
expect($rootScope.value).toBe(30);
3438+
3439+
// 0.3 - 0.2 === 0.09999999999999998
3440+
$rootScope.$apply('min = 0.2; step = 0.09999999999999998; value = 0.3');
3441+
expect(inputElm.val()).toBe('0.3');
3442+
expect(inputElm).toBeInvalid();
3443+
expect(ngModel.$error.step).toBe(true);
3444+
expect($rootScope.value).toBeUndefined();
3445+
});
3446+
3447+
it('should correctly validate even in cases where the JS floating point arithmetic fails',
3448+
function() {
3449+
var inputElm = compileRangeInput('ng-model="value" step="0.1"');
3450+
var ngModel = inputElm.controller('ngModel');
3451+
3452+
expect(inputElm.val()).toBe('');
3453+
expect(inputElm).toBeValid();
3454+
expect($rootScope.value).toBeUndefined();
3455+
3456+
helper.changeInputValueTo('0.3');
3457+
expect(inputElm).toBeValid();
3458+
expect($rootScope.value).toBe(0.3);
3459+
3460+
helper.changeInputValueTo('2.9999999999999996');
3461+
expect(inputElm).toBeInvalid();
3462+
expect(ngModel.$error.step).toBe(true);
3463+
expect($rootScope.value).toBeUndefined();
3464+
3465+
// 0.5 % 0.1 === 0.09999999999999998
3466+
helper.changeInputValueTo('0.5');
3467+
expect(inputElm).toBeValid();
3468+
expect($rootScope.value).toBe(0.5);
3469+
3470+
// 3.5 % 0.1 === 0.09999999999999981
3471+
helper.changeInputValueTo('3.5');
3472+
expect(inputElm).toBeValid();
3473+
expect($rootScope.value).toBe(3.5);
3474+
}
3475+
);
33983476
}
33993477
});
34003478

0 commit comments

Comments
 (0)