From e56b7b826d6f4f8cb1fc4bcd06fce7786540784c Mon Sep 17 00:00:00 2001 From: FranklinDM Date: Sat, 13 May 2023 18:15:13 +0800 Subject: 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 --- layout/style/nsCSSParser.cpp | 70 +++++++++++++++++++++++++++++++++++++++----- 1 file 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) { // + + 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) { // uint8_t r, g, b, a; if (ParseRGBColor(r, g, b, a)) { @@ -7107,14 +7147,22 @@ CSSParserImpl::ParseColorComponent(uint8_t& aComponent, Maybe 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 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; -- cgit v1.2.3