summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklinDM <mrmineshafter17@gmail.com>2023-05-13 18:15:13 +0800
committerFranklinDM <mrmineshafter17@gmail.com>2023-05-13 19:22:42 +0800
commite56b7b826d6f4f8cb1fc4bcd06fce7786540784c (patch)
tree69410ab7e1073814ee95c5cd62df39f54289d796
parent19650c6f8a55e1cfed6acd9bcf415409569f0945 (diff)
downloaduxp-e56b7b826d6f4f8cb1fc4bcd06fce7786540784c.tar.gz
Issue #1765 - Part 2: Implement calc() parsing inside rgb/a() and the non-hue component of hsl/a()
This also adds a new helper method for checking the type of tokens that will be parsed after the current token, which is useful for determining the unit of values inside calc() functions. Partially based on https://github.com/roytam1/UXP/commit/8a0897d23f5b51526f8a2c6ef63cb26968e6b985
-rw-r--r--layout/style/nsCSSParser.cpp70
1 files changed, 63 insertions, 7 deletions
diff --git a/layout/style/nsCSSParser.cpp b/layout/style/nsCSSParser.cpp
index 244abf36ae..14a935c321 100644
--- a/layout/style/nsCSSParser.cpp
+++ b/layout/style/nsCSSParser.cpp
@@ -676,6 +676,11 @@ protected:
bool SkipAtRule(bool aInsideBlock);
bool SkipDeclaration(bool aCheckForBraces);
+ // Returns true when the target token type is found, and false for the
+ // end of declaration, start of !important flag, end of declaration
+ // block, or EOF.
+ bool LookForTokenType(nsCSSTokenType aType);
+
void PushGroup(css::GroupRule* aRule);
void PopGroup();
@@ -5419,6 +5424,33 @@ CSSParserImpl::SkipDeclaration(bool aCheckForBraces)
return true;
}
+bool
+CSSParserImpl::LookForTokenType(nsCSSTokenType aType) {
+ bool rv = false;
+ CSSParserInputState stateBeforeValue;
+ SaveInputState(stateBeforeValue);
+
+ const char16_t stopChars[] = { ';', '!', '}', 0 };
+ nsDependentString stopSymbolChars(stopChars);
+ while (GetToken(true)) {
+ // The current function has percentage values.
+ if (mToken.mType == eCSSToken_Percentage) {
+ rv = true;
+ break;
+ }
+ // Stop looking if we're at the end of the declaration, encountered an
+ // !important flag, or at the end of the declaration block.
+ if (mToken.mType == eCSSToken_Symbol &&
+ stopSymbolChars.FindChar(mToken.mSymbol) != -1) {
+ rv = false;
+ break;
+ }
+ }
+
+ RestoreSavedInputState(stateBeforeValue);
+ return rv;
+}
+
void
CSSParserImpl::SkipRuleSet(bool aInsideBraces)
{
@@ -7000,7 +7032,15 @@ CSSParserImpl::ParseColor(nsCSSValue& aValue)
if (GetToken(true)) {
UngetToken();
}
- if (mToken.mType == eCSSToken_Number) { // <number>
+
+ bool isNumber = mToken.mType == eCSSToken_Number;
+
+ // Check first if we have percentage values inside the function.
+ if (mToken.mType == eCSSToken_Function) {
+ isNumber = !LookForTokenType(eCSSToken_Percentage);
+ }
+
+ if (isNumber) { // <number>
uint8_t r, g, b, a;
if (ParseRGBColor(r, g, b, a)) {
@@ -7107,14 +7147,22 @@ CSSParserImpl::ParseColorComponent(uint8_t& aComponent, Maybe<char> aSeparator)
return false;
}
- if (mToken.mType != eCSSToken_Number) {
+ float value;
+ if (mToken.mType == eCSSToken_Number) {
+ value = mToken.mNumber;
+ } else if (IsCalcFunctionToken(mToken)) {
+ nsCSSValue aValue;
+ if (!ParseCalc(aValue, VARIANT_LPN | VARIANT_CALC)) {
+ return false;
+ }
+ ReduceNumberCalcOps ops;
+ value = mozilla::css::ComputeCalc(aValue, ops);
+ } else {
REPORT_UNEXPECTED_TOKEN(PEExpectedNumber);
UngetToken();
return false;
}
- float value = mToken.mNumber;
-
if (aSeparator && !ExpectSymbol(*aSeparator, true)) {
REPORT_UNEXPECTED_TOKEN_CHAR(PEColorComponentBadTerm, *aSeparator);
return false;
@@ -7135,14 +7183,22 @@ CSSParserImpl::ParseColorComponent(float& aComponent, Maybe<char> aSeparator)
return false;
}
- if (mToken.mType != eCSSToken_Percentage) {
+ float value;
+ if (mToken.mType == eCSSToken_Percentage) {
+ value = mToken.mNumber;
+ } else if (IsCalcFunctionToken(mToken)) {
+ nsCSSValue aValue;
+ if (!ParseCalc(aValue, VARIANT_LPN | VARIANT_CALC)) {
+ return false;
+ }
+ ReduceNumberCalcOps ops;
+ value = mozilla::css::ComputeCalc(aValue, ops);
+ } else {
REPORT_UNEXPECTED_TOKEN(PEExpectedPercent);
UngetToken();
return false;
}
- float value = mToken.mNumber;
-
if (aSeparator && !ExpectSymbol(*aSeparator, true)) {
REPORT_UNEXPECTED_TOKEN_CHAR(PEColorComponentBadTerm, *aSeparator);
return false;