4 <script src=
"../../../resources/js-test.js"></script>
7 <p id=
"description"></p>
8 <div id=
"console"></div>
10 description('Tests for .valueAsNumber with <input type=range>.');
12 var input
= document
.createElement('input');
14 document
.body
.appendChild(input
);
16 function valueAsNumberFor(stringValue
) {
17 input
.value
= stringValue
;
18 return input
.valueAsNumber
;
21 function setValueAsNumberAndGetValue(num
) {
22 input
.valueAsNumber
= num
;
26 // The default values for type=range: min=0 max=100 step=1
27 // Values are modifies for these restrictions.
28 shouldBe('valueAsNumberFor("0")', '0');
29 shouldBe('valueAsNumberFor("10")', '10');
30 shouldBe('valueAsNumberFor("01")', '1');
31 shouldBe('valueAsNumberFor("-0")', '0');
32 shouldBe('valueAsNumberFor("-1.2")', '0');
33 shouldBe('valueAsNumberFor("0.2")', '0');
34 shouldBe('valueAsNumberFor(".2")', '0');
35 shouldBe('valueAsNumberFor("1.2E10")', '100');
36 shouldBe('valueAsNumberFor("1.2E-10")', '0');
37 shouldBe('valueAsNumberFor("1.2E+10")', '100');
38 shouldBe('valueAsNumberFor("123456789012345678901234567890123456789")', '100');
39 shouldBe('valueAsNumberFor("0.12345678901234567890123456789012345678901234567890")', '0');
41 debug('valueAsNumber for invalid string values:');
42 shouldBe('valueAsNumberFor("")', '50');
43 shouldBe('valueAsNumberFor("abc")', '50');
44 shouldBe('valueAsNumberFor("0xff")', '50');
45 shouldBe('valueAsNumberFor("+1")', '50');
46 shouldBe('valueAsNumberFor(" 10")', '50');
47 shouldBe('valueAsNumberFor("10 ")', '50');
48 shouldBe('valueAsNumberFor("1E")', '50');
49 shouldBe('valueAsNumberFor("NaN")', '50');
50 shouldBe('valueAsNumberFor("nan")', '50');
51 shouldBe('valueAsNumberFor("Inf")', '50');
52 shouldBe('valueAsNumberFor("inf")', '50');
53 shouldBe('valueAsNumberFor("Infinity")', '50');
54 shouldBe('valueAsNumberFor("infinity")', '50');
56 debug('Too huge exponent to support');
57 shouldBe('valueAsNumberFor("1.2E65535")', '50');
59 debug('Tests for the valueAsNumber setter:');
60 shouldBeEqualToString('setValueAsNumberAndGetValue(0)', '0');
61 shouldBeEqualToString('setValueAsNumberAndGetValue(10)', '10');
62 shouldBeEqualToString('setValueAsNumberAndGetValue(01)', '1');
63 shouldBeEqualToString('setValueAsNumberAndGetValue(-0)', '0');
64 shouldBeEqualToString('setValueAsNumberAndGetValue(-1.2)', '0');
65 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e10)', '100');
66 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e-10)', '0');
67 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2345678901234567e+38)', '100');
69 debug('Tests to set invalid values to valueAsNumber:');
70 shouldBeEqualToString('setValueAsNumberAndGetValue(null)', '0');
71 shouldBeEqualToString('setValueAsNumberAndGetValue(0)', '0');
73 // if .valueAsNumber is passed a NaN, the value is set as the empty string. Which resets the range value to its default.
74 var minValue
= Number(input
.min
) || 0;
75 var maxValue
= Number(input
.max
) || 100;
76 var defaultValue
= String(minValue
> maxValue
? minValue
: (minValue
+ (maxValue
- minValue
) / 2.0));
77 shouldBeEqualToString('setValueAsNumberAndGetValue("foo")', defaultValue
);
78 shouldBeEqualToString('setValueAsNumberAndGetValue(NaN)', defaultValue
);
79 shouldBeEqualToString('setValueAsNumberAndGetValue(Number.NaN)', defaultValue
);
80 shouldThrow('setValueAsNumberAndGetValue(Infinity)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
81 shouldThrow('setValueAsNumberAndGetValue(Number.POSITIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
82 shouldThrow('setValueAsNumberAndGetValue(Number.NEGATIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');