| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="../../../resources/js-test-pre.js"></script> |
| </head> |
| <body> |
| <p id="description"></p> |
| <div id="console"></div> |
| <script> |
| description('Tests for .valueAsNumber with <input type=date>.'); |
| |
| var input = document.createElement('input'); |
| input.type = 'date'; |
| |
| function valueAsNumberFor(stringValue) { |
| input.value = stringValue; |
| return input.valueAsNumber; |
| } |
| |
| function setValueAsNumberAndGetValue(year, month, day) { |
| var date = new Date(); |
| date.setTime(Date.UTC(year, month, day)); |
| if (year < 100) |
| date.setUTCFullYear(year); |
| input.valueAsNumber = date.getTime(); |
| return input.value; |
| } |
| |
| shouldBe('valueAsNumberFor("")', 'Number.NaN'); |
| shouldBe('valueAsNumberFor("1969-12-31")', 'Date.UTC(1969, 11, 31)'); |
| shouldBe('valueAsNumberFor("1970-01-01")', 'Date.UTC(1970, 0, 1)'); |
| shouldBe('valueAsNumberFor("2009-12-22")', 'Date.UTC(2009, 11, 22)'); |
| |
| shouldBe('setValueAsNumberAndGetValue(1969, 11, 1)', '"1969-12-01"'); |
| shouldBe('setValueAsNumberAndGetValue(1970, 0, 1)', '"1970-01-01"'); |
| shouldBe('setValueAsNumberAndGetValue(2009, 11, 31)', '"2009-12-31"'); |
| shouldBe('setValueAsNumberAndGetValue(10000, 0, 1)', '"10000-01-01"'); |
| |
| shouldBe('setValueAsNumberAndGetValue(-1, 0, 1)', '""'); |
| shouldBe('setValueAsNumberAndGetValue(0, 11, 31)', '""'); |
| shouldBe('setValueAsNumberAndGetValue(1, 0, 1)', '"0001-01-01"'); |
| shouldBe('setValueAsNumberAndGetValue(275760, 8, 13)', '"275760-09-13"'); |
| |
| debug('Tests to set invalid values to valueAsNumber:'); |
| shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-01-01"'); |
| shouldThrowErrorName('input.valueAsNumber = "foo"', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = NaN', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = Number.NaN', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = Infinity', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = Number.POSITIVE_INFINITY', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = Number.NEGATIVE_INFINITY', 'NotSupportedError'); |
| shouldThrowErrorName('input.valueAsNumber = Date.UTC(275760, 8, 14)', 'NotSupportedError'); |
| </script> |
| <script src="../../../resources/js-test-post.js"></script> |
| </body> |
| </html> |