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

Commit 90034fd

Browse files
committed
don't assume min = 0 / max = 100 default for non-support browsers
test that support browsers enforce default min = 0 / max = 100
1 parent f93b09a commit 90034fd

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

src/ng/directive/input.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1547,9 +1547,9 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15471547
numberFormatterParser(ctrl);
15481548
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
15491549

1550-
var minVal = 0,
1551-
maxVal = 100,
1552-
supportsRange = ctrl.$$hasNativeValidators && element[0].type === 'range',
1550+
var supportsRange = ctrl.$$hasNativeValidators && element[0].type === 'range',
1551+
minVal = supportsRange ? 0 : undefined,
1552+
maxVal = supportsRange ? 100 : undefined,
15531553
validity = element[0].validity,
15541554
minAttrType = isDefined(attr.ngMin) ? 'ngMin' : isDefined(attr.min) ? 'min' : false,
15551555
maxAttrType = isDefined(attr.ngMax) ? 'ngMax' : isDefined(attr.max) ? 'max' : false;
@@ -1594,7 +1594,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15941594
}
15951595

15961596
function setInitialValueAndObserver(attrType, attrName, changeFn) {
1597-
var initialAttrValue;
1597+
var initialAttrValue;
15981598
if (attrType === attrName) {
15991599
initialAttrValue = attr[attrName];
16001600
// Set the actual element attribute so that the browser can adjust the value based on
@@ -1612,7 +1612,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16121612
if (isDefined(val) && !isNumber(val)) {
16131613
val = parseFloat(val);
16141614
}
1615-
minVal = isNumber(val) && !isNaN(val) ? val : 0;
1615+
minVal = isNumber(val) && !isNaN(val) ? val : undefined;
16161616
// ignore changes before model is initialized
16171617
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
16181618
return;
@@ -1622,8 +1622,8 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16221622
var elVal = element.val();
16231623
// IE11 doesn't set the el val correctly if the minVal is greater than the element value
16241624
if (minVal > elVal) {
1625-
element.val(minVal);
16261625
elVal = minVal;
1626+
element.val(elVal);
16271627
}
16281628
ctrl.$setViewValue(elVal);
16291629
} else {
@@ -1636,19 +1636,19 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16361636
if (isDefined(val) && !isNumber(val)) {
16371637
val = parseFloat(val);
16381638
}
1639-
maxVal = isNumber(val) && !isNaN(val) ? val : 100;
1639+
maxVal = isNumber(val) && !isNaN(val) ? val : undefined;
16401640
// ignore changes before model is initialized
16411641
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
16421642
return;
16431643
}
16441644

16451645
if (supportsRange && maxAttrType === 'max') {
16461646
var elVal = element.val();
1647-
// IE11 doesn't set the el val correctly if the maxVal is greater than the element value
1647+
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
16481648
if (maxVal < elVal) {
1649-
element.val(maxVal);
16501649
// IE11 and Chrome don't set the value to the minVal when max < min
1651-
elVal = maxVal < minVal ? minVal : maxVal;
1650+
elVal = Math.max(minVal, maxVal);
1651+
element.val(elVal);
16521652
}
16531653
ctrl.$setViewValue(elVal);
16541654
} else {

test/ng/directive/inputSpec.js

+84-5
Original file line numberDiff line numberDiff line change
@@ -2976,6 +2976,22 @@ describe('input', function() {
29762976
expect(scope.form.alias.$error.min).toBeFalsy();
29772977
});
29782978

2979+
it('should set the model to the min val if it is less than the min val', function() {
2980+
scope.value = -10;
2981+
// Default min is 0
2982+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="{{min}}" />');
2983+
2984+
expect(inputElm).toBeValid();
2985+
expect(inputElm.val()).toBe('0');
2986+
expect(scope.value).toBe(0);
2987+
2988+
scope.$apply('value = 5; min = 10');
2989+
2990+
expect(inputElm).toBeValid();
2991+
expect(inputElm.val()).toBe('10');
2992+
expect(scope.value).toBe(10);
2993+
});
2994+
29792995
it('should adjust the element and model value when the min value changes on-the-fly', function() {
29802996
scope.min = 10;
29812997
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="{{min}}" />');
@@ -3009,8 +3025,9 @@ describe('input', function() {
30093025
});
30103026

30113027
} else {
3028+
// input[type=range] will become type=text in browsers that don't support it
3029+
30123030
it('should validate if "range" is not implemented', function() {
3013-
// This will become type=text in browsers that don't support it
30143031
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="10" />');
30153032

30163033
helper.changeInputValueTo('5');
@@ -3024,6 +3041,34 @@ describe('input', function() {
30243041
expect(scope.form.alias.$error.min).toBeFalsy();
30253042
});
30263043

3044+
it('should not assume a min val of 0 if the min interpolates to a non-number', function() {
3045+
scope.value = -10;
3046+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="{{min}}" />');
3047+
3048+
expect(inputElm).toBeValid();
3049+
expect(inputElm.val()).toBe('-10');
3050+
expect(scope.value).toBe(-10);
3051+
expect(scope.form.alias.$error.min).toBeFalsy();
3052+
3053+
helper.changeInputValueTo('-5');
3054+
expect(inputElm).toBeValid();
3055+
expect(inputElm.val()).toBe('-5');
3056+
expect(scope.value).toBe(-5);
3057+
expect(scope.form.alias.$error.min).toBeFalsy();
3058+
3059+
scope.$apply('max = "null"');
3060+
expect(inputElm).toBeValid();
3061+
expect(inputElm.val()).toBe('-5');
3062+
expect(scope.value).toBe(-5);
3063+
expect(scope.form.alias.$error.max).toBeFalsy();
3064+
3065+
scope.$apply('max = "asdf"');
3066+
expect(inputElm).toBeValid();
3067+
expect(inputElm.val()).toBe('-5');
3068+
expect(scope.value).toBe(-5);
3069+
expect(scope.form.alias.$error.max).toBeFalsy();
3070+
});
3071+
30273072
it('should validate even if the min value changes on-the-fly', function() {
30283073
scope.min = 10;
30293074
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="{{min}}" />');
@@ -3118,7 +3163,6 @@ describe('input', function() {
31183163
expect(scope.form.alias.$error.max).toBeFalsy();
31193164
});
31203165

3121-
// Browsers that implement range will never allow you to set the value > max value
31223166
it('should validate', function() {
31233167
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="10" />');
31243168

@@ -3133,9 +3177,16 @@ describe('input', function() {
31333177
expect(scope.form.alias.$error.max).toBeFalsy();
31343178
});
31353179

3136-
it('should set the model to the max val if it is more than the max val', function() {
3137-
scope.value = 90;
3138-
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="10" />');
3180+
it('should set the model to the max val if it is greater than the max val', function() {
3181+
scope.value = 110;
3182+
// Default max is 100
3183+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="{{max}}" />');
3184+
3185+
expect(inputElm).toBeValid();
3186+
expect(inputElm.val()).toBe('100');
3187+
expect(scope.value).toBe(100);
3188+
3189+
scope.$apply('value = 90; max = 10');
31393190

31403191
expect(inputElm).toBeValid();
31413192
expect(inputElm.val()).toBe('10');
@@ -3189,6 +3240,34 @@ describe('input', function() {
31893240
expect(scope.form.alias.$error.max).toBeFalsy();
31903241
});
31913242

3243+
it('should not assume a max val of 100 if the max attribute interpolates to a non-number', function() {
3244+
scope.value = 120;
3245+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="{{max}}" />');
3246+
3247+
expect(inputElm).toBeValid();
3248+
expect(inputElm.val()).toBe('120');
3249+
expect(scope.value).toBe(120);
3250+
expect(scope.form.alias.$error.max).toBeFalsy();
3251+
3252+
helper.changeInputValueTo('140');
3253+
expect(inputElm).toBeValid();
3254+
expect(inputElm.val()).toBe('140');
3255+
expect(scope.value).toBe(140);
3256+
expect(scope.form.alias.$error.max).toBeFalsy();
3257+
3258+
scope.$apply('max = null');
3259+
expect(inputElm).toBeValid();
3260+
expect(inputElm.val()).toBe('140');
3261+
expect(scope.value).toBe(140);
3262+
expect(scope.form.alias.$error.max).toBeFalsy();
3263+
3264+
scope.$apply('max = "asdf"');
3265+
expect(inputElm).toBeValid();
3266+
expect(inputElm.val()).toBe('140');
3267+
expect(scope.value).toBe(140);
3268+
expect(scope.form.alias.$error.max).toBeFalsy();
3269+
});
3270+
31923271
it('should validate even if the max value changes on-the-fly', function() {
31933272
scope.max = 10;
31943273
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" max="{{max}}" />');

0 commit comments

Comments
 (0)