diff options
Diffstat (limited to 'dom/base')
-rw-r--r-- | dom/base/nsAttrValue.cpp | 34 | ||||
-rw-r--r-- | dom/base/nsAttrValue.h | 13 | ||||
-rw-r--r-- | dom/base/nsContentUtils.cpp | 1 | ||||
-rw-r--r-- | dom/base/nsContentUtils.h | 4 |
4 files changed, 51 insertions, 1 deletions
diff --git a/dom/base/nsAttrValue.cpp b/dom/base/nsAttrValue.cpp index ebddcb7ed..2418fb501 100644 --- a/dom/base/nsAttrValue.cpp +++ b/dom/base/nsAttrValue.cpp @@ -1526,6 +1526,40 @@ nsAttrValue::ParseIntWithFallback(const nsAString& aString, int32_t aDefault, SetIntValueAndType(val, eInteger, nonStrict ? &aString : nullptr); } +void +nsAttrValue::ParseClampedNonNegativeInt(const nsAString& aString, + int32_t aDefault, int32_t aMin, + int32_t aMax) +{ + ResetIfSet(); + + nsContentUtils::ParseHTMLIntegerResultFlags result; + int32_t val = nsContentUtils::ParseHTMLInteger(aString, &result); + bool nonStrict = (result & nsContentUtils::eParseHTMLInteger_IsPercent) || + (result & nsContentUtils::eParseHTMLInteger_NonStandard) || + (result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput); + + if (result & nsContentUtils::eParseHTMLInteger_ErrorOverflow) { + if (result & nsContentUtils::eParseHTMLInteger_Negative) { + val = aDefault; + } else { + val = aMax; + } + nonStrict = true; + } else if ((result & nsContentUtils::eParseHTMLInteger_Error) || val < 0) { + val = aDefault; + nonStrict = true; + } else if (val < aMin) { + val = aMin; + nonStrict = true; + } else if (val > aMax) { + val = aMax; + nonStrict = true; + } + + SetIntValueAndType(val, eInteger, nonStrict ? &aString : nullptr); +} + bool nsAttrValue::ParseNonNegativeIntValue(const nsAString& aString) { diff --git a/dom/base/nsAttrValue.h b/dom/base/nsAttrValue.h index 655e4ca61..23f61a614 100644 --- a/dom/base/nsAttrValue.h +++ b/dom/base/nsAttrValue.h @@ -362,6 +362,19 @@ public: */ bool ParseNonNegativeIntValue(const nsAString& aString); + /** + * Parse a string value into a clamped non-negative integer. + * This method follows the rules for parsing non-negative integer from: + * https://html.spec.whatwg.org/multipage/infrastructure.html#clamped-to-the-range + * + * @param aString the string to parse + * @param aDefault value to return for negative or invalid values + * @param aMin minimum value + * @param aMax maximum value + */ + void ParseClampedNonNegativeInt(const nsAString& aString, int32_t aDefault, + int32_t aMin, int32_t aMax); + /** * Parse a string value into a positive integer. * This method follows the rules for parsing non-negative integer from: diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 800f40fa1..6a9904bf9 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -1080,6 +1080,7 @@ nsContentUtils::ParseHTMLInteger(const nsAString& aValue, int sign = 1; if (*iter == char16_t('-')) { sign = -1; + result |= eParseHTMLInteger_Negative; ++iter; } else if (*iter == char16_t('+')) { result |= eParseHTMLInteger_NonStandard; diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 606d67de9..ffbd15e78 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -444,7 +444,9 @@ public: // Set if one or more error flags were set. eParseHTMLInteger_Error = 1 << 3, eParseHTMLInteger_ErrorNoValue = 1 << 4, - eParseHTMLInteger_ErrorOverflow = 1 << 5 + eParseHTMLInteger_ErrorOverflow = 1 << 5, + // Use this flag to detect the difference between overflow and underflow + eParseHTMLInteger_Negative = 1 << 6, }; static int32_t ParseHTMLInteger(const nsAString& aValue, ParseHTMLIntegerResultFlags *aResult); |