[webkit-changes] [WebKit/WebKit] c7dc51: Support late resolution of symbols in CSS calc

Sam Weinig noreply at github.com
Fri May 10 16:12:55 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: c7dc51fc9225e7ef06d5f956bb7a9837278d542c
      https://github.com/WebKit/WebKit/commit/c7dc51fc9225e7ef06d5f956bb7a9837278d542c
  Author: Sam Weinig <weinig at apple.com>
  Date:   2024-05-10 (Fri, 10 May 2024)

  Changed paths:
    M Source/WebCore/Headers.cmake
    M Source/WebCore/Sources.txt
    M Source/WebCore/WebCore.xcodeproj/project.pbxproj
    M Source/WebCore/css/CSSPrimitiveValue.cpp
    M Source/WebCore/css/calc/CSSCalcExpressionNode.cpp
    M Source/WebCore/css/calc/CSSCalcExpressionNode.h
    M Source/WebCore/css/calc/CSSCalcExpressionNodeParser.cpp
    M Source/WebCore/css/calc/CSSCalcExpressionNodeParser.h
    M Source/WebCore/css/calc/CSSCalcInvertNode.cpp
    M Source/WebCore/css/calc/CSSCalcInvertNode.h
    M Source/WebCore/css/calc/CSSCalcNegateNode.cpp
    M Source/WebCore/css/calc/CSSCalcNegateNode.h
    M Source/WebCore/css/calc/CSSCalcOperationNode.cpp
    M Source/WebCore/css/calc/CSSCalcOperationNode.h
    M Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.cpp
    M Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.h
    A Source/WebCore/css/calc/CSSCalcSymbolNode.cpp
    A Source/WebCore/css/calc/CSSCalcSymbolNode.h
    M Source/WebCore/css/calc/CSSCalcSymbolTable.cpp
    M Source/WebCore/css/calc/CSSCalcSymbolTable.h
    A Source/WebCore/css/calc/CSSCalcSymbolsAllowed.cpp
    A Source/WebCore/css/calc/CSSCalcSymbolsAllowed.h
    M Source/WebCore/css/calc/CSSCalcValue.cpp
    M Source/WebCore/css/calc/CSSCalcValue.h
    M Source/WebCore/css/parser/CSSCalcParser.cpp
    M Source/WebCore/css/parser/CSSCalcParser.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Angle.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+AngleDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+CSSPrimitiveValueResolver.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Color.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Integer.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+IntegerDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Length.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Length.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+LengthDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+MetaConsumer.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+MetaResolver.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+None.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+NoneDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Number.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+NumberDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Percent.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+PercentDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Primitives.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Primitives.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+RawResolver.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+RawResolver.h
    A Source/WebCore/css/parser/CSSPropertyParserConsumer+RawTypes.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+RawTypes.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Resolution.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+ResolutionDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Symbol.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+SymbolDefinitions.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Time.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+Time.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+TimeDefinitions.h
    A Source/WebCore/css/parser/CSSPropertyParserConsumer+UnevaluatedCalc.cpp
    A Source/WebCore/css/parser/CSSPropertyParserConsumer+UnevaluatedCalc.h
    M Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp
    M Source/WebCore/css/parser/CSSPropertyParserHelpers.h
    M Source/WebCore/css/typedom/CSSNumericValue.cpp
    M Source/WebCore/style/StyleResolveForFontRaw.cpp

  Log Message:
  -----------
  Support late resolution of symbols in CSS calc
https://bugs.webkit.org/show_bug.cgi?id=274001

Reviewed by Darin Adler.

This change makes it so that symbols passed to `calc()`, such as done
for relative colors, are kept as symbols until evaluation of the `calc()`
is performed. This will allow us to delay resolving the symbols in
relative colors in cases like an origin color of `currentColor`, where
we don't have the values to resolve the symbol until much later.

To make this possible, a new `CSSCalcExpressionNode` type is added,
`CSSCalcSymbolNode`, which represents the unresolved symbol. Additionally,
instead of passing the `CSSCalcSymbolTable` to the parser, a new
`CSSCalcSymbolsAllowed` table is passed there, and `CSSCalcSymbolTable`
is passed to the evaluation function `doubleValue()`.

Only a few small changes were needed to processing `calc()` to support
these unresolved symbols, primarily in `CSSCalcOperationNode` where
a few places were making assumptions about how child nodes would be
combined by simplification if they had the same types, but with symbols
that is not possible in all cases anymore.

* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
    - Add new files.

* Source/WebCore/css/CSSPrimitiveValue.cpp:
(WebCore::CSSPrimitiveValue::doubleValue const):
(WebCore::CSSPrimitiveValue::doubleValueDividingBy100IfPercentage const):
    - Pass empty symbol table to `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcExpressionNode.cpp:
    - Move virtual destructor out of line to avoid vtable duplication.

* Source/WebCore/css/calc/CSSCalcExpressionNode.h:
    - Adds new `CssCalcSymbol` type
    - Adds new `isResolvable()` pure virtual function, which returns false
      if the node, or any of its children, is an unresolved symbol.
    - Adds `CSSCalcSymbolTable` parameter to `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcExpressionNodeParser.cpp:
(WebCore::CSSCalcExpressionNodeParser::parseValue):
* Source/WebCore/css/calc/CSSCalcExpressionNodeParser.h:
(WebCore::CSSCalcExpressionNodeParser::CSSCalcExpressionNodeParser):
    - Utilize CSSCalcSymbolsAllowed to create symbol nodes for symbols
      at parse time.

* Source/WebCore/css/calc/CSSCalcInvertNode.cpp:
(WebCore::CSSCalcInvertNode::isResolvable const):
(WebCore::CSSCalcInvertNode::doubleValue const):
* Source/WebCore/css/calc/CSSCalcInvertNode.h:
    - Implement `isResolvable() and pass symbol table through to children
      in `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcNegateNode.cpp:
(WebCore::CSSCalcNegateNode::isResolvable const):
(WebCore::CSSCalcNegateNode::doubleValue const):
* Source/WebCore/css/calc/CSSCalcNegateNode.h:
    - Implement `isResolvable() and pass symbol table through to children
      in `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcOperationNode.h:
* Source/WebCore/css/calc/CSSCalcOperationNode.cpp:
(WebCore::CSSCalcOperationNode::combineChildren):
    - Adds check for `isResolvable()` before trying to early evaluate
      math functions, as you can't evaluate them if child is a symbol.
    - Passes empty symbol table through to resolvable children via
      `doubleValue()`.

(WebCore::CSSCalcOperationNode::isResolvable const):
    - Implement by checking children for `isResolvable()`.

(WebCore::CSSCalcOperationNode::isZero const):
    - Move definition to implementation file to avoid #including
      CSSCalcSymbolTable in the header.

(WebCore::CSSCalcOperationNode::primitiveType const):
    - Add support for computing the primitive type when there are
      multiple nodes of the same type that have not been combined,
      which can now happen with symbols.

(WebCore::CSSCalcOperationNode::doubleValue const):
    - Pass symbol table through to children.

(WebCore::CSSCalcOperationNode::buildCSSTextRecursive):
    - Pass empty symbol table to `doubleValue()`, which is fine because the
      child has been explicitly checked to be a `CSSCalcPrimitiveValueNode`.

* Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.cpp:
(WebCore::CSSCalcPrimitiveValueNode::add):
(WebCore::CSSCalcPrimitiveValueNode::doubleValue const):
(WebCore::CSSCalcPrimitiveValueNode::isResolvable const):
* Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.h:
    - Implement `isResolvable() and pass symbol table through to children
      in `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcSymbolNode.cpp: Added.
* Source/WebCore/css/calc/CSSCalcSymbolNode.h: Added.
    - New `calc` node. Only supported for numeric (`doubleValue()`) `calc()`,
      as there are currently no uses of symbols for anything else and adding
      additional support would require significantly more plumbing with
      `CalcExpressionNode` for no benefit.

* Source/WebCore/css/calc/CSSCalcSymbolTable.cpp:
* Source/WebCore/css/calc/CSSCalcSymbolTable.h:
    - Remove WeakPtr support. `CSSCalcSymbolTable` is not stored anywhere
      anymore, just passed through `doubleValue()`.

* Source/WebCore/css/calc/CSSCalcSymbolsAllowed.cpp: Copied from Source/WebCore/css/calc/CSSCalcSymbolTable.cpp.
(WebCore::CSSCalcSymbolsAllowed::CSSCalcSymbolsAllowed):
(WebCore::CSSCalcSymbolsAllowed::get const):
(WebCore::CSSCalcSymbolsAllowed::contains const):
* Source/WebCore/css/calc/CSSCalcSymbolsAllowed.h: Copied from Source/WebCore/css/calc/CSSCalcSymbolTable.h.
    - Added new table for allowed symbols which includes the symbol's
      name and type.

* Source/WebCore/css/calc/CSSCalcValue.cpp:
(WebCore::CSSCalcValue::doubleValue const):
(WebCore::CSSCalcValue::create):
* Source/WebCore/css/calc/CSSCalcValue.h:
    - Pass the `CSSCalcSymbolsAllowed` through the constructor, and
      `CSSCalcSymbolTable` through `doubleValue()`.

* Source/WebCore/css/parser/CSSCalcParser.cpp:
(WebCore::CSSPropertyParserHelpers::CalcParser::CalcParser):
(WebCore::CSSPropertyParserHelpers::consumeCalcRawWithKnownTokenTypeFunction):
* Source/WebCore/css/parser/CSSCalcParser.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Angle.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+AngleDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+CSSPrimitiveValueResolver.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+CSSPrimitiveValueResolver.h:
    - Pass `CSSCalcSymbolTable` to `doubleValue()` for resolution.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Color.cpp:
(WebCore::CSSPropertyParserHelpers::consumeAbsoluteComponent):
(WebCore::CSSPropertyParserHelpers::consumeRelativeComponent):
(WebCore::CSSPropertyParserHelpers::consumeRelativeComponents):
    - Pass both the `CSSCalcSymbolsAllowed` and `CSSCalcSymbolTable` to shared
      consume/resolve functions. This is temporary, and in a subsequent change
      resolution will be in a distinct place from consuming.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Integer.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+IntegerDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Length.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Length.h:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+LengthDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+MetaConsumer.h:
(WebCore::CSSPropertyParserHelpers::MetaConsumer::consume):
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+MetaResolver.h:
(WebCore::CSSPropertyParserHelpers::MetaResolver::consumeAndResolve):
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+None.cpp:
(WebCore::CSSPropertyParserHelpers::NoneKnownTokenTypeIdentConsumer::consume):
* Source/WebCore/css/parser/CSSPropertyParserConsumer+NoneDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Number.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+NumberDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Percent.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+PercentDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Primitives.cpp:
(WebCore::CSSPropertyParserHelpers::equal): Deleted.
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Primitives.h:
    - Moves UnevaluatedCalc out to its own files.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+RawResolver.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+RawResolver.h:
    - Pass `CSSCalcSymbolTable` to `doubleValue()` for resolution.
    - Utilize shared support for symbol replacing via `replaceSymbol()`.
    - Utilize shared support for calc evaluation via `evaluateCalc().

* Source/WebCore/css/parser/CSSPropertyParserConsumer+RawTypes.cpp: Added
(WebCore::replaceSymbol):
    - Added shared place for direct symbol (e.g. not in a `calc()`) replacement
      via a symbol table.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+RawTypes.h:
    - Moved raw types out of the CSSPropertyParserHelpers as it is now used
      beyond just the parser helpers and having that long prefix was unwieldily.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Resolution.cpp:
(WebCore::CSSPropertyParserHelpers::consumeResolution):
* Source/WebCore/css/parser/CSSPropertyParserConsumer+ResolutionDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Symbol.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+SymbolDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Time.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Time.h:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+TimeDefinitions.h:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+UnevaluatedCalc.cpp: Copied from Source/WebCore/css/parser/CSSPropertyParserConsumer+Primitives.cpp.
(WebCore::CSSPropertyParserHelpers::unevaluatedCalcEqual):
(WebCore::CSSPropertyParserHelpers::unevaluatedCalcSerialization):
(WebCore::CSSPropertyParserHelpers::evaluateCalc):
* Source/WebCore/css/parser/CSSPropertyParserConsumer+UnevaluatedCalc.h: Added.
(WebCore::CSSPropertyParserHelpers::UnevaluatedCalc::operator== const):
(WebCore::CSSPropertyParserHelpers::serializationForCSS):
(WebCore::CSSPropertyParserHelpers::evaluateCalc):
    - Move UnevaluatedCalc to its own files and add support for evaluation that
      passes symbol table through and returns the correct raw type.

* Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:
    - Replace `CSSCalcSymbolTable` with `CSSCalcSymbolsAllowed` for parsing,
      and pass empty values for both to combined `consumeAndResolve()`.

* Source/WebCore/css/parser/CSSPropertyParserHelpers.h:
    - Removed CSSPropertyParserHelpers namespace prefixes from raw types.

* Source/WebCore/css/parser/CSSPropertyParserWorkerSafe.cpp:
    - Added now needed missing #include.

* Source/WebCore/css/typedom/CSSNumericValue.cpp:
(WebCore::reifyMathExpression):
(WebCore::CSSNumericValue::reifyMathExpression):
    - Fill in support for the new calc node. Currently unsupported by the spec,
      so throughs an exception.

* Source/WebCore/style/StyleResolveForFontRaw.cpp:
    - Removed CSSPropertyParserHelpers namespace prefixes from raw types.

Canonical link: https://commits.webkit.org/278635@main



To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications


More information about the webkit-changes mailing list