From 85440b01dcbb8ac4005c41949fe6700c554c1940 Mon Sep 17 00:00:00 2001 From: Mounir Lamouri Date: Mon, 26 Nov 2012 11:35:26 +0000 Subject: [PATCH] Bug 813936 - Update nsHTMLInputElement::GetStepBase() to return the default value if there is no min. r=smaug --- .../html/content/src/nsHTMLInputElement.cpp | 13 +++++ .../test/forms/test_step_attribute.html | 40 ++++++++++++---- .../test/forms/test_stepup_stepdown.html | 48 ++++++++----------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/content/html/content/src/nsHTMLInputElement.cpp b/content/html/content/src/nsHTMLInputElement.cpp index 5fa870ffd92..85dfbc9149f 100644 --- a/content/html/content/src/nsHTMLInputElement.cpp +++ b/content/html/content/src/nsHTMLInputElement.cpp @@ -1204,6 +1204,19 @@ nsHTMLInputElement::GetStepBase() const { double stepBase = GetMinAsDouble(); + // If @min is not a double, we should use defaultValue. + if (MOZ_DOUBLE_IS_NaN(stepBase)) { + nsAutoString stringValue; + GetAttr(kNameSpaceID_None, nsGkAtoms::value, stringValue); + + nsresult ec; + stepBase = stringValue.ToDouble(&ec); + + if (NS_FAILED(ec)) { + stepBase = MOZ_DOUBLE_NaN(); + } + } + return MOZ_DOUBLE_IS_NaN(stepBase) ? kDefaultStepBase : stepBase; } diff --git a/content/html/content/test/forms/test_step_attribute.html b/content/html/content/test/forms/test_step_attribute.html index 5e1ffe66350..c368a71679e 100644 --- a/content/html/content/test/forms/test_step_attribute.html +++ b/content/html/content/test/forms/test_step_attribute.html @@ -45,8 +45,11 @@ var types = [ [ 'button', false ], ]; -var input = document.createElement("input"); -document.getElementById('content').appendChild(input); +function getFreshElement(type) { + var elmt = document.createElement('input'); + elmt.type = type; + return elmt; +} function checkValidity(aElement, aValidity, aApply, aData) { @@ -74,7 +77,7 @@ function checkValidity(aElement, aValidity, aApply, aData) SimpleTest.waitForExplicitFinish(); SpecialPowers.pushPrefEnv({'set': [["dom.experimental_forms", true]]}, function() { for each (var data in types) { - input.type = data[0]; + var input = getFreshElement(data[0]); var apply = data[1]; if (data[2]) { @@ -227,10 +230,35 @@ for each (var data in types) { input.max = '10'; input.value = '-9'; checkValidity(input, false, apply, {low: -10, high: -8}); + + // If there is a value defined but no min, the step base is the value. + input = getFreshElement(data[0]); + input.setAttribute('value', '1'); + input.step = 2; + checkValidity(input, true, apply); + + input.value = 3; + checkValidity(input, true, apply); + + input.value = 2; + checkValidity(input, false, apply, {low: 1, high: 3}); + + // Should also work with defaultValue. + input = getFreshElement(data[0]); + input.defaultValue = 1; + input.step = 2; + checkValidity(input, true, apply); + + input.value = 3; + checkValidity(input, true, apply); + + input.value = 2; + checkValidity(input, false, apply, {low: 1, high: 3}); } if (input.type == 'number') { // Check that when the higher value is higher than max, we don't show it. + input = getFreshElement(data[0]); input.step = '2'; input.min = '1'; input.max = '10.9'; @@ -240,12 +268,6 @@ for each (var data in types) { "The nearest valid value is 9.", "The validation message should not include the higher value."); } - - // Cleaning up, - input.removeAttribute('step'); - input.removeAttribute('max'); - input.removeAttribute('min'); - input.value = ''; } SimpleTest.finish(); diff --git a/content/html/content/test/forms/test_stepup_stepdown.html b/content/html/content/test/forms/test_stepup_stepdown.html index 00b3b6439be..19f3b809104 100644 --- a/content/html/content/test/forms/test_stepup_stepdown.html +++ b/content/html/content/test/forms/test_stepup_stepdown.html @@ -124,8 +124,6 @@ function checkStepDownForNumber() [ '1', null, null, null, 1.1, '0', false ], // With step values. [ '1', '0.5', null, null, null, '0.5', false ], - [ null, '0.5', null, null, null, '0', false ], - [ null, '0.5', null, null, null, '-0.5', false ], [ '1', '0.25', null, null, 4, '0', false ], // step = 0 isn't allowed (-> step = 1). [ '1', '0', null, null, null, '0', false ], @@ -147,21 +145,21 @@ function checkStepDownForNumber() [ '1', null, null, '-10', null, '-10', false ], [ '1', null, null, '1', null, '0', false ], [ '5', null, null, '3', '3', '2', false ], - [ '5', '2', null, '3', '2', '2', false ], + [ '5', '2', '-6', '3', '2', '2', false ], [ '-3', '5', '-10', '-3', null, '-5', false ], // Step mismatch. [ '1', '2', '-2', null, null, '0', false ], [ '3', '2', '-2', null, null, '2', false ], [ '3', '2', '-2', null, '2', '0', false ], [ '3', '2', '-2', null, '-2', '6', false ], - [ '1', '2', null, null, null, '0', false ], + [ '1', '2', '-6', null, null, '0', false ], [ '1', '2', '-2', null, null, '0', false ], - [ '1', '3', null, null, null, '0', false ], - [ '2', '3', null, null, null, '0', false ], + [ '1', '3', '-6', null, null, '0', false ], + [ '2', '3', '-6', null, null, '0', false ], [ '2', '3', '1', null, null, '1', false ], [ '5', '3', '1', null, null, '4', false ], - [ '3', '2', null, null, null, '2', false ], - [ '5', '2', null, null, null, '4', false ], + [ '3', '2', '-6', null, null, '2', false ], + [ '5', '2', '-6', null, null, '4', false ], [ '6', '2', '1', null, null, '5', false ], [ '8', '3', '1', null, null, '7', false ], [ '9', '2', '-10', null, null, '8', false ], @@ -178,13 +176,16 @@ function checkStepDownForNumber() [ '0', 'ANY', null, null, 1, null, true ], [ '0', 'AnY', null, null, 1, null, true ], [ '0', 'aNy', null, null, 1, null, true ], + // With @value = step base. + [ '1', '2', null, null, null, '-1', false ], ]; - var element = document.createElement("input"); - element.type = 'number'; for each (var data in testData) { + var element = document.createElement("input"); + element.type = 'number'; + if (data[0] != null) { - element.value = data[0]; + element.setAttribute('value', data[0]); } if (data[1] != null) { @@ -216,10 +217,6 @@ function checkStepDownForNumber() } finally { is(exceptionCaught, data[6], "exception status should be " + data[6]); } - - element.removeAttribute('step'); - element.removeAttribute('min'); - element.removeAttribute('max'); } } @@ -240,8 +237,6 @@ function checkStepUpForNumber() [ '1', null, null, null, 1.1, '2', false ], // With step values. [ '1', '0.5', null, null, null, '1.5', false ], - [ null, '0.5', null, null, null, '2', false ], - [ null, '0.5', null, null, null, '2.5', false ], [ '1', '0.25', null, null, 4, '2', false ], // step = 0 isn't allowed (-> step = 1). [ '1', '0', null, null, null, '2', false ], @@ -269,7 +264,7 @@ function checkStepUpForNumber() [ '1', '2', '0', null, null, '2', false ], [ '1', '2', '0', null, '2', '4', false ], [ '8', '2', null, '9', null, '8', false ], - [ '-3', '2', null, null, null, '-2', false ], + [ '-3', '2', '-6', null, null, '-2', false ], [ '9', '3', '-10', null, null, '11', false ], [ '7', '3', '-10', null, null, '8', false ], [ '7', '3', '5', null, null, '8', false ], @@ -282,8 +277,8 @@ function checkStepUpForNumber() [ '-9', '3', '-8', '-1', '5', '-2', false ], [ '-9', '3', '8', '15', '15', '14', false ], [ '-1', '3', '-1', '4', '3', '2', false ], - [ '-3', '2', null, '-2', null, '-2', false ], - [ '-3', '2', null, '-1', null, '-2', false ], + [ '-3', '2', '-6', '-2', null, '-2', false ], + [ '-3', '2', '-6', '-1', null, '-2', false ], // value = "" (NaN). [ '', null, null, null, null, '', false ], // With step = 'any'. @@ -291,13 +286,16 @@ function checkStepUpForNumber() [ '0', 'ANY', null, null, 1, null, true ], [ '0', 'AnY', null, null, 1, null, true ], [ '0', 'aNy', null, null, 1, null, true ], + // With @value = step base. + [ '1', '2', null, null, null, '3', false ], ]; - var element = document.createElement("input"); - element.type = 'number'; for each (var data in testData) { + var element = document.createElement("input"); + element.type = 'number'; + if (data[0] != null) { - element.value = data[0]; + element.setAttribute('value', data[0]); } if (data[1] != null) { @@ -329,10 +327,6 @@ function checkStepUpForNumber() } finally { is(exceptionCaught, data[6], "exception status should be " + data[6]); } - - element.removeAttribute('step'); - element.removeAttribute('min'); - element.removeAttribute('max'); } }