Skip to content

Commit

Permalink
[anchor] Always resolve anchor()/anchor-size() computed-value time
Browse files Browse the repository at this point in the history
This CL removes the option of evaluating anchor() and anchor-size()
functions at used-value time, per recent CSSWG resolution [1].

This eliminates the need for an AnchorEvaluator to resolve a Length,
which means don't need to send that around to the extent we did before.
It's still needed in some cases, because AnchorEvaluatorImpl is used
for some secondary purposes in absolute_utils.cc.

We still use CalculationExpressionAnchorQueryNode to represent
the anchor query during Length::AnchorEvaluator::Evaluate for now.
A follow-up CL will address this.

Except for the removal of the feature flag, there should be no
behavior change.

Bug: 41483417

[1] w3c/csswg-drafts#9598

Change-Id: I068a57fcb6b5c90e133a36e095e8bbf25861714d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5355673
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Commit-Queue: Anders Hartvoll Ruud <andruud@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1270318}
  • Loading branch information
andruud authored and Chromium LUCI CQ committed Mar 8, 2024
1 parent dd3c965 commit b6b27b6
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 395 deletions.
Expand Up @@ -87,18 +87,9 @@ CalculationExpressionAnchorQueryNode::Zoom(double factor) const {

float CalculationExpressionAnchorQueryNode::Evaluate(
float max_value,
const Length::EvaluationInput& input) const {
if (input.anchor_evaluator) {
if (const std::optional<LayoutUnit> value =
input.anchor_evaluator->Evaluate(*this)) {
return value->ToFloat();
}
}
// If we did not provide an AnchorEvaluator, then this is not
// for an absolutely-positioned element, and we should use the fallback.
//
// https://drafts.csswg.org/css-anchor-position-1/#valid-anchor-function
return FloatValueForLength(fallback_, max_value, input);
const Length::EvaluationInput&) const {
// TODO(crbug.com//41483417): Remove CalculationExpressionAnchorQueryNode.
return 0;
}

} // namespace blink
3 changes: 1 addition & 2 deletions third_party/blink/renderer/core/css/css_length_resolver.h
Expand Up @@ -61,8 +61,7 @@ class CORE_EXPORT CSSLengthResolver {
// https://drafts.csswg.org/css-anchor-position-1/
virtual void ReferenceAnchor() const = 0;

// The AnchorEvaluator used to evaluate anchor()/anchor-size() queries,
// when the runtime flag CSSAnchorPositioningComputeAnchor is enabled.
// The AnchorEvaluator used to evaluate anchor()/anchor-size() queries.
virtual Length::AnchorEvaluator* AnchorEvaluator() const { return nullptr; }

float Zoom() const { return zoom_; }
Expand Down
16 changes: 2 additions & 14 deletions third_party/blink/renderer/core/css/css_math_expression_node.cc
Expand Up @@ -278,9 +278,7 @@ bool CanEagerlySimplify(const CSSMathExpressionNode* operand) {
return true;
case CalculationResultCategory::kCalcLength:
return !CSSPrimitiveValue::IsRelativeUnit(operand->ResolvedUnitType()) &&
(!RuntimeEnabledFeatures::
CSSAnchorPositioningComputeAnchorEnabled() ||
!operand->IsAnchorQuery());
!operand->IsAnchorQuery();
default:
return false;
}
Expand Down Expand Up @@ -2352,10 +2350,6 @@ namespace {

CalculationResultCategory AnchorQueryCategory(
const CSSPrimitiveValue* fallback) {
if (!RuntimeEnabledFeatures::CSSAnchorPositioningComputeAnchorEnabled()) {
// Anchor queries are resolved used-value time.
return kCalcLengthFunction;
}
// Note that the main (non-fallback) result of an anchor query is always
// a kCalcLength, so the only thing that can make our overall result anything
// else is the fallback.
Expand Down Expand Up @@ -2405,8 +2399,6 @@ double CSSMathExpressionAnchorQuery::ComputeDouble(
// AnchorQueryCategory), in which case we'll reach ToCalculationExpression
// instead.

CHECK(RuntimeEnabledFeatures::CSSAnchorPositioningComputeAnchorEnabled());

// TODO(crbug.com/41483417): Remove CalculationExpressionAnchorQueryNode.
scoped_refptr<const CalculationExpressionNode> query =
ToCalculationExpressionQuery(length_resolver);
Expand Down Expand Up @@ -2503,11 +2495,6 @@ CSSMathExpressionAnchorQuery::ToCalculationExpression(
scoped_refptr<const CalculationExpressionNode> query =
ToCalculationExpressionQuery(length_resolver);

// TODO(crbug.com/41483417): Remove CalculationExpressionAnchorQueryNode.
if (!RuntimeEnabledFeatures::CSSAnchorPositioningComputeAnchorEnabled()) {
return query;
}

Length result;

if (std::optional<LayoutUnit> px = EvaluateQuery(*query, length_resolver)) {
Expand Down Expand Up @@ -2535,6 +2522,7 @@ std::optional<LayoutUnit> CSSMathExpressionAnchorQuery::EvaluateQuery(
scoped_refptr<const CalculationExpressionNode>
CSSMathExpressionAnchorQuery::ToCalculationExpressionQuery(
const CSSLengthResolver& length_resolver) const {
// TODO(crbug.com/41483417): Remove CalculationExpressionAnchorQueryNode.
DCHECK(IsScopedValue());
AnchorSpecifierValue* anchor_specifier = AnchorSpecifierValue::Default();
if (const auto* implicit =
Expand Down
Expand Up @@ -356,7 +356,6 @@ TEST_F(CSSToLengthConversionDataTest, ConversionWithoutPrimaryFont) {

TEST_F(CSSToLengthConversionDataTest, AnchorFunction) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ LayoutUnit(60.0));
CSSToLengthConversionData data =
Expand All @@ -371,7 +370,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorFunction) {

TEST_F(CSSToLengthConversionDataTest, AnchorFunctionFallback) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ std::nullopt);
CSSToLengthConversionData data =
Expand All @@ -389,7 +387,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorFunctionFallback) {

TEST_F(CSSToLengthConversionDataTest, AnchorSizeFunction) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ LayoutUnit(60.0));
CSSToLengthConversionData data =
Expand All @@ -405,7 +402,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorSizeFunction) {

TEST_F(CSSToLengthConversionDataTest, AnchorSizeFunctionFallback) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ std::nullopt);
CSSToLengthConversionData data =
Expand All @@ -425,7 +421,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorSizeFunctionFallback) {

TEST_F(CSSToLengthConversionDataTest, AnchorWithinOtherFunction) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ std::nullopt);
CSSToLengthConversionData data =
Expand Down Expand Up @@ -458,7 +453,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorWithinOtherFunction) {

TEST_F(CSSToLengthConversionDataTest, AnchorFunctionPercentageFallback) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ std::nullopt);
CSSToLengthConversionData data =
Expand All @@ -485,7 +479,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorFunctionPercentageFallback) {
TEST_F(CSSToLengthConversionDataTest,
AnchorFunctionPercentageFallbackNotTaken) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ LayoutUnit(60.0));
CSSToLengthConversionData data =
Expand All @@ -503,7 +496,6 @@ TEST_F(CSSToLengthConversionDataTest,

TEST_F(CSSToLengthConversionDataTest, AnchorFunctionFallbackNullEvaluator) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

CSSToLengthConversionData data =
ConversionData({.anchor_evaluator = nullptr});
Expand All @@ -515,7 +507,6 @@ TEST_F(CSSToLengthConversionDataTest, AnchorFunctionFallbackNullEvaluator) {

TEST_F(CSSToLengthConversionDataTest, AnchorFunctionLengthPercentageFallback) {
ScopedCSSAnchorPositioningForTest anchor_feature(true);
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

TestAnchorEvaluator anchor_evaluator(/* result */ std::nullopt);
CSSToLengthConversionData data =
Expand Down
Expand Up @@ -54,20 +54,7 @@ TEST_F(ComputedStylePropertyMapTest, TransformPerspectiveZoom) {
EXPECT_EQ("perspective(100px)", style_value->toString());
}

TEST_F(ComputedStylePropertyMapTest, TopWithAnchor) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(false);

ComputedStylePropertyMap* map =
SetBodyStyle("position: absolute; top: anchor(bottom, 17px);");
CSSStyleValue* style_value =
map->get(GetDocument().GetExecutionContext(), "top", ASSERT_NO_EXCEPTION);
ASSERT_TRUE(style_value);
EXPECT_EQ("anchor(bottom, 17px)", style_value->toString());
}

TEST_F(ComputedStylePropertyMapTest, TopWithAnchorComputed) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ComputedStylePropertyMap* map =
SetBodyStyle("position: absolute; top: anchor(bottom, 17px);");
CSSStyleValue* style_value =
Expand Down
Expand Up @@ -336,8 +336,6 @@ TEST_F(CSSPropertyTest, AlternativePropertyCycle) {
}

TEST_F(CSSPropertyTest, AnchorModeTop) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ModeCheckingAnchorEvaluator anchor_evaluator(Length::AnchorScope::Mode::kTop);
StyleRecalcContext context = {.anchor_evaluator = &anchor_evaluator};

Expand All @@ -354,8 +352,6 @@ TEST_F(CSSPropertyTest, AnchorModeTop) {
}

TEST_F(CSSPropertyTest, AnchorModeRight) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ModeCheckingAnchorEvaluator anchor_evaluator(
Length::AnchorScope::Mode::kRight);
StyleRecalcContext context = {.anchor_evaluator = &anchor_evaluator};
Expand All @@ -373,8 +369,6 @@ TEST_F(CSSPropertyTest, AnchorModeRight) {
}

TEST_F(CSSPropertyTest, AnchorModeBottom) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ModeCheckingAnchorEvaluator anchor_evaluator(
Length::AnchorScope::Mode::kBottom);
StyleRecalcContext context = {.anchor_evaluator = &anchor_evaluator};
Expand All @@ -392,8 +386,6 @@ TEST_F(CSSPropertyTest, AnchorModeBottom) {
}

TEST_F(CSSPropertyTest, AnchorModeLeft) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ModeCheckingAnchorEvaluator anchor_evaluator(
Length::AnchorScope::Mode::kLeft);
StyleRecalcContext context = {.anchor_evaluator = &anchor_evaluator};
Expand All @@ -411,8 +403,6 @@ TEST_F(CSSPropertyTest, AnchorModeLeft) {
}

TEST_F(CSSPropertyTest, AnchorModeSize) {
ScopedCSSAnchorPositioningComputeAnchorForTest compute_anchor_feature(true);

ModeCheckingAnchorEvaluator anchor_evaluator(
Length::AnchorScope::Mode::kSize);
StyleRecalcContext context = {.anchor_evaluator = &anchor_evaluator};
Expand Down
Expand Up @@ -2388,8 +2388,7 @@ bool StyleResolver::CanReuseBaseComputedStyle(const StyleResolverState& state) {
return false;
}

if (RuntimeEnabledFeatures::CSSAnchorPositioningComputeAnchorEnabled() &&
base_style->HasAnchorFunctions()) {
if (base_style->HasAnchorFunctions()) {
// TODO(crbug.com/41483417): Enable this optimization for styles with
// anchor queries.
return false;
Expand Down

0 comments on commit b6b27b6

Please sign in to comment.