зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1367669 - [PaymentRequest API] Revise the way to validate a decimal monetary value. r=baku
This commit is contained in:
Родитель
aebf20ee04
Коммит
b89ef45eed
|
@ -75,14 +75,34 @@ PaymentRequest::IsValidNumber(const nsAString& aItem,
|
|||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg)
|
||||
{
|
||||
nsAutoString aValue(aStr);
|
||||
nsresult error = NS_OK;
|
||||
aValue.ToFloat(&error);
|
||||
nsresult error = NS_ERROR_FAILURE;
|
||||
|
||||
if (!aStr.IsEmpty()) {
|
||||
nsAutoString aValue(aStr);
|
||||
|
||||
// If the beginning character is '-', we will check the second one.
|
||||
int beginningIndex = (aValue.First() == '-') ? 1 : 0;
|
||||
|
||||
// Ensure
|
||||
// - the beginning character is a digit in [0-9], and
|
||||
// - the last character is not '.'
|
||||
// to follow spec:
|
||||
// https://w3c.github.io/browser-payment-api/#dfn-valid-decimal-monetary-value
|
||||
//
|
||||
// For example, ".1" is not valid for '.' is not in [0-9],
|
||||
// and " 0.1" either for beginning with ' '
|
||||
if (aValue.Last() != '.' &&
|
||||
aValue.CharAt(beginningIndex) >= '0' &&
|
||||
aValue.CharAt(beginningIndex) <= '9') {
|
||||
aValue.ToFloat(&error);
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_FAILED(error)) {
|
||||
aErrorMsg.AssignLiteral("The amount.value of \"");
|
||||
aErrorMsg.Append(aItem);
|
||||
aErrorMsg.AppendLiteral("\"(");
|
||||
aErrorMsg.Append(aValue);
|
||||
aErrorMsg.Append(aStr);
|
||||
aErrorMsg.AppendLiteral(") must be a valid decimal monetary value.");
|
||||
return false;
|
||||
}
|
||||
|
@ -90,19 +110,30 @@ PaymentRequest::IsValidNumber(const nsAString& aItem,
|
|||
}
|
||||
|
||||
bool
|
||||
PaymentRequest::IsPositiveNumber(const nsAString& aItem,
|
||||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg)
|
||||
PaymentRequest::IsNonNegativeNumber(const nsAString& aItem,
|
||||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg)
|
||||
{
|
||||
nsAutoString aValue(aStr);
|
||||
nsresult error = NS_OK;
|
||||
float value = aValue.ToFloat(&error);
|
||||
if (NS_FAILED(error) || value < 0) {
|
||||
nsresult error = NS_ERROR_FAILURE;
|
||||
|
||||
if (!aStr.IsEmpty()) {
|
||||
nsAutoString aValue(aStr);
|
||||
// Ensure
|
||||
// - the beginning character is a digit in [0-9], and
|
||||
// - the last character is not '.'
|
||||
if (aValue.Last() != '.' &&
|
||||
aValue.First() >= '0' &&
|
||||
aValue.First() <= '9') {
|
||||
aValue.ToFloat(&error);
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_FAILED(error)) {
|
||||
aErrorMsg.AssignLiteral("The amount.value of \"");
|
||||
aErrorMsg.Append(aItem);
|
||||
aErrorMsg.AppendLiteral("\"(");
|
||||
aErrorMsg.Append(aValue);
|
||||
aErrorMsg.AppendLiteral(") must be a valid and positive decimal monetary value.");
|
||||
aErrorMsg.Append(aStr);
|
||||
aErrorMsg.AppendLiteral(") must be a valid and non-negative decimal monetaryvalue.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -112,8 +143,8 @@ bool
|
|||
PaymentRequest::IsValidDetailsInit(const PaymentDetailsInit& aDetails, nsAString& aErrorMsg)
|
||||
{
|
||||
// Check the amount.value of detail.total
|
||||
if (!IsPositiveNumber(NS_LITERAL_STRING("details.total"),
|
||||
aDetails.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||
if (!IsNonNegativeNumber(NS_LITERAL_STRING("details.total"),
|
||||
aDetails.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -149,8 +180,8 @@ PaymentRequest::IsValidDetailsBase(const PaymentDetailsBase& aDetails, nsAString
|
|||
if (aDetails.mModifiers.WasPassed()) {
|
||||
const Sequence<PaymentDetailsModifier>& modifiers = aDetails.mModifiers.Value();
|
||||
for (const PaymentDetailsModifier& modifier : modifiers) {
|
||||
if (!IsPositiveNumber(NS_LITERAL_STRING("details.modifiers.total"),
|
||||
modifier.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||
if (!IsNonNegativeNumber(NS_LITERAL_STRING("details.modifiers.total"),
|
||||
modifier.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||
return false;
|
||||
}
|
||||
if (modifier.mAdditionalDisplayItems.WasPassed()) {
|
||||
|
|
|
@ -43,9 +43,9 @@ public:
|
|||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg);
|
||||
static bool
|
||||
IsPositiveNumber(const nsAString& aItem,
|
||||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg);
|
||||
IsNonNegativeNumber(const nsAString& aItem,
|
||||
const nsAString& aStr,
|
||||
nsAString& aErrorMsg);
|
||||
|
||||
static bool
|
||||
IsValidDetailsInit(const PaymentDetailsInit& aDetails,
|
||||
|
|
Загрузка…
Ссылка в новой задаче