Bug 1893532 - Improve error message of HTMLInputElement.stepUp/stepDown. r=dom-core,sefeng

Differential Revision: https://phabricator.services.mozilla.com/D208671
This commit is contained in:
Emilio Cobos Álvarez
2024-05-01 00:11:33 +00:00
parent 373de30e6a
commit 4a29b211db
4 changed files with 35 additions and 53 deletions

View File

@@ -1986,18 +1986,21 @@ Decimal HTMLInputElement::GetStepBase() const {
return kDefaultStepBase;
}
nsresult HTMLInputElement::GetValueIfStepped(int32_t aStep,
StepCallerType aCallerType,
Decimal* aNextStep) {
Decimal HTMLInputElement::GetValueIfStepped(int32_t aStep,
StepCallerType aCallerType,
ErrorResult& aRv) {
constexpr auto kNaN = Decimal::nan();
if (!DoStepDownStepUpApply()) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
aRv.ThrowInvalidStateError("Step doesn't apply to this input type");
return kNaN;
}
Decimal stepBase = GetStepBase();
Decimal step = GetStep();
if (step == kStepAny) {
if (aCallerType != CALLED_FOR_USER_EVENT) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
if (aCallerType != StepCallerType::ForUserEvent) {
aRv.ThrowInvalidStateError("Can't step an input with step=\"any\"");
return kNaN;
}
// Allow the spin buttons and up/down arrow keys to do something sensible:
step = GetDefaultStep();
@@ -2015,7 +2018,7 @@ nsresult HTMLInputElement::GetValueIfStepped(int32_t aStep,
// adjustment to align maximum on a step, or else (if we adjusted
// maximum) there is no valid step between minimum and the unadjusted
// maximum.
return NS_OK;
return kNaN;
}
}
}
@@ -2062,25 +2065,20 @@ nsresult HTMLInputElement::GetValueIfStepped(int32_t aStep,
(aStep < 0 && value > valueBeforeStepping))) {
// We don't want step-up to effectively step down, or step-down to
// effectively step up, so return;
return NS_OK;
return kNaN;
}
*aNextStep = value;
return NS_OK;
return value;
}
nsresult HTMLInputElement::ApplyStep(int32_t aStep) {
Decimal nextStep = Decimal::nan(); // unchanged if value will not change
nsresult rv = GetValueIfStepped(aStep, CALLED_FOR_SCRIPT, &nextStep);
if (NS_SUCCEEDED(rv) && nextStep.isFinite()) {
// We know we're not a file input, so the caller type does not matter; just
// pass "not system" to be safe.
SetValue(nextStep, CallerType::NonSystem);
void HTMLInputElement::ApplyStep(int32_t aStep, ErrorResult& aRv) {
Decimal nextStep = GetValueIfStepped(aStep, StepCallerType::ForScript, aRv);
if (aRv.Failed() || !nextStep.isFinite()) {
return;
}
return rv;
// We know we're not a file input, so the caller type does not matter; just
// pass "not system" to be safe.
SetValue(nextStep, CallerType::NonSystem);
}
bool HTMLInputElement::IsDateTimeInputType(FormControlType aType) {
@@ -3520,11 +3518,9 @@ void HTMLInputElement::StepNumberControlForUserEvent(int32_t aDirection) {
}
}
Decimal newValue = Decimal::nan(); // unchanged if value will not change
nsresult rv = GetValueIfStepped(aDirection, CALLED_FOR_USER_EVENT, &newValue);
if (NS_FAILED(rv) || !newValue.isFinite()) {
Decimal newValue = GetValueIfStepped(aDirection, StepCallerType::ForUserEvent,
IgnoreErrors());
if (!newValue.isFinite()) {
return; // value should not or will not change
}