зеркало из 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,
|
const nsAString& aStr,
|
||||||
nsAString& aErrorMsg)
|
nsAString& aErrorMsg)
|
||||||
{
|
{
|
||||||
nsAutoString aValue(aStr);
|
nsresult error = NS_ERROR_FAILURE;
|
||||||
nsresult error = NS_OK;
|
|
||||||
aValue.ToFloat(&error);
|
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)) {
|
if (NS_FAILED(error)) {
|
||||||
aErrorMsg.AssignLiteral("The amount.value of \"");
|
aErrorMsg.AssignLiteral("The amount.value of \"");
|
||||||
aErrorMsg.Append(aItem);
|
aErrorMsg.Append(aItem);
|
||||||
aErrorMsg.AppendLiteral("\"(");
|
aErrorMsg.AppendLiteral("\"(");
|
||||||
aErrorMsg.Append(aValue);
|
aErrorMsg.Append(aStr);
|
||||||
aErrorMsg.AppendLiteral(") must be a valid decimal monetary value.");
|
aErrorMsg.AppendLiteral(") must be a valid decimal monetary value.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -90,19 +110,30 @@ PaymentRequest::IsValidNumber(const nsAString& aItem,
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
PaymentRequest::IsPositiveNumber(const nsAString& aItem,
|
PaymentRequest::IsNonNegativeNumber(const nsAString& aItem,
|
||||||
const nsAString& aStr,
|
const nsAString& aStr,
|
||||||
nsAString& aErrorMsg)
|
nsAString& aErrorMsg)
|
||||||
{
|
{
|
||||||
nsAutoString aValue(aStr);
|
nsresult error = NS_ERROR_FAILURE;
|
||||||
nsresult error = NS_OK;
|
|
||||||
float value = aValue.ToFloat(&error);
|
if (!aStr.IsEmpty()) {
|
||||||
if (NS_FAILED(error) || value < 0) {
|
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.AssignLiteral("The amount.value of \"");
|
||||||
aErrorMsg.Append(aItem);
|
aErrorMsg.Append(aItem);
|
||||||
aErrorMsg.AppendLiteral("\"(");
|
aErrorMsg.AppendLiteral("\"(");
|
||||||
aErrorMsg.Append(aValue);
|
aErrorMsg.Append(aStr);
|
||||||
aErrorMsg.AppendLiteral(") must be a valid and positive decimal monetary value.");
|
aErrorMsg.AppendLiteral(") must be a valid and non-negative decimal monetaryvalue.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -112,8 +143,8 @@ bool
|
||||||
PaymentRequest::IsValidDetailsInit(const PaymentDetailsInit& aDetails, nsAString& aErrorMsg)
|
PaymentRequest::IsValidDetailsInit(const PaymentDetailsInit& aDetails, nsAString& aErrorMsg)
|
||||||
{
|
{
|
||||||
// Check the amount.value of detail.total
|
// Check the amount.value of detail.total
|
||||||
if (!IsPositiveNumber(NS_LITERAL_STRING("details.total"),
|
if (!IsNonNegativeNumber(NS_LITERAL_STRING("details.total"),
|
||||||
aDetails.mTotal.mAmount.mValue, aErrorMsg)) {
|
aDetails.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +180,8 @@ PaymentRequest::IsValidDetailsBase(const PaymentDetailsBase& aDetails, nsAString
|
||||||
if (aDetails.mModifiers.WasPassed()) {
|
if (aDetails.mModifiers.WasPassed()) {
|
||||||
const Sequence<PaymentDetailsModifier>& modifiers = aDetails.mModifiers.Value();
|
const Sequence<PaymentDetailsModifier>& modifiers = aDetails.mModifiers.Value();
|
||||||
for (const PaymentDetailsModifier& modifier : modifiers) {
|
for (const PaymentDetailsModifier& modifier : modifiers) {
|
||||||
if (!IsPositiveNumber(NS_LITERAL_STRING("details.modifiers.total"),
|
if (!IsNonNegativeNumber(NS_LITERAL_STRING("details.modifiers.total"),
|
||||||
modifier.mTotal.mAmount.mValue, aErrorMsg)) {
|
modifier.mTotal.mAmount.mValue, aErrorMsg)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (modifier.mAdditionalDisplayItems.WasPassed()) {
|
if (modifier.mAdditionalDisplayItems.WasPassed()) {
|
||||||
|
|
|
@ -43,9 +43,9 @@ public:
|
||||||
const nsAString& aStr,
|
const nsAString& aStr,
|
||||||
nsAString& aErrorMsg);
|
nsAString& aErrorMsg);
|
||||||
static bool
|
static bool
|
||||||
IsPositiveNumber(const nsAString& aItem,
|
IsNonNegativeNumber(const nsAString& aItem,
|
||||||
const nsAString& aStr,
|
const nsAString& aStr,
|
||||||
nsAString& aErrorMsg);
|
nsAString& aErrorMsg);
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
IsValidDetailsInit(const PaymentDetailsInit& aDetails,
|
IsValidDetailsInit(const PaymentDetailsInit& aDetails,
|
||||||
|
|
Загрузка…
Ссылка в новой задаче