File: interpolable_length.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (682 lines) | stat: -rw-r--r-- 24,345 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/interpolable_length.h"

#include "third_party/blink/renderer/core/animation/length_property_functions.h"
#include "third_party/blink/renderer/core/animation/underlying_value.h"
#include "third_party/blink/renderer/core/css/css_math_expression_node.h"
#include "third_party/blink/renderer/core/css/css_math_function_value.h"
#include "third_party/blink/renderer/core/css/css_math_operator.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_to_length_conversion_data.h"
#include "third_party/blink/renderer/core/css/css_value_clamping_utils.h"
#include "third_party/blink/renderer/platform/geometry/blend.h"
#include "third_party/blink/renderer/platform/geometry/calculation_value.h"

namespace blink {

using UnitType = CSSPrimitiveValue::UnitType;

namespace {

CSSMathExpressionNode* NumberNode(double number) {
  return CSSMathExpressionNumericLiteral::Create(
      CSSNumericLiteralValue::Create(number, UnitType::kNumber));
}

CSSMathExpressionNode* PercentageNode(double number) {
  return CSSMathExpressionNumericLiteral::Create(
      CSSNumericLiteralValue::Create(number, UnitType::kPercentage));
}

}  // namespace

// static
InterpolableLength* InterpolableLength::CreatePixels(double pixels) {
  CSSLengthArray length_array;
  length_array.values[CSSPrimitiveValue::kUnitTypePixels] = pixels;
  length_array.type_flags.set(CSSPrimitiveValue::kUnitTypePixels);
  return MakeGarbageCollected<InterpolableLength>(std::move(length_array));
}

// static
InterpolableLength* InterpolableLength::CreatePercent(double percent) {
  CSSLengthArray length_array;
  length_array.values[CSSPrimitiveValue::kUnitTypePercentage] = percent;
  length_array.type_flags.set(CSSPrimitiveValue::kUnitTypePercentage);
  return MakeGarbageCollected<InterpolableLength>(std::move(length_array));
}

// static
InterpolableLength* InterpolableLength::CreateNeutral() {
  return MakeGarbageCollected<InterpolableLength>(CSSLengthArray());
}

// static
InterpolableLength* InterpolableLength::MaybeConvertCSSValue(
    const CSSValue& value) {
  const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value);
  if (!primitive_value)
    return nullptr;

  if (!primitive_value->IsLength() && !primitive_value->IsPercentage() &&
      primitive_value->IsResolvableBeforeLayout()) {
    return nullptr;
  }

  CSSLengthArray length_array;
  if (primitive_value->AccumulateLengthArray(length_array))
    return MakeGarbageCollected<InterpolableLength>(std::move(length_array));

  const CSSMathExpressionNode* expression_node = nullptr;

  if (const auto* numeric_literal =
          DynamicTo<CSSNumericLiteralValue>(primitive_value)) {
    expression_node = CSSMathExpressionNumericLiteral::Create(numeric_literal);
  } else {
    DCHECK(primitive_value->IsMathFunctionValue());
    expression_node =
        To<CSSMathFunctionValue>(primitive_value)->ExpressionNode();
  }

  return MakeGarbageCollected<InterpolableLength>(*expression_node);
}

CSSValueID InterpolableLength::LengthTypeToCSSValueID(Length::Type lt) {
  switch (lt) {
    case Length::Type::kAuto:
      return CSSValueID::kAuto;
    case Length::Type::kMinContent:
      return CSSValueID::kMinContent;
    case Length::Type::kMaxContent:
      return CSSValueID::kMaxContent;
    case Length::Type::kFitContent:
      return CSSValueID::kFitContent;
    case Length::Type::kStretch:
      return CSSValueID::kStretch;
    case Length::Type::kFillAvailable:
      return CSSValueID::kWebkitFillAvailable;
    case Length::Type::kContent:  // only valid for flex-basis.
      return CSSValueID::kContent;
    default:
      return CSSValueID::kInvalid;
  }
}

Length::Type InterpolableLength::CSSValueIDToLengthType(CSSValueID id) {
  switch (id) {
    case CSSValueID::kAuto:
      return Length::Type::kAuto;
    case CSSValueID::kMinContent:
    case CSSValueID::kWebkitMinContent:
      return Length::Type::kMinContent;
    case CSSValueID::kMaxContent:
    case CSSValueID::kWebkitMaxContent:
      return Length::Type::kMaxContent;
    case CSSValueID::kFitContent:
    case CSSValueID::kWebkitFitContent:
      return Length::Type::kFitContent;
    case CSSValueID::kStretch:
      return Length::Type::kStretch;
    case CSSValueID::kWebkitFillAvailable:
      return Length::Type::kFillAvailable;
    case CSSValueID::kContent:  // only valid for flex-basis.
      return Length::Type::kContent;
    default:
      NOTREACHED();
  }
}

// static
InterpolableLength* InterpolableLength::MaybeConvertLength(
    const Length& length,
    const CSSProperty& property,
    float zoom,
    std::optional<EInterpolateSize> interpolate_size) {
  if (!length.CanConvertToCalculation()) {
    CSSValueID keyword = LengthTypeToCSSValueID(length.GetType());
    if (keyword == CSSValueID::kInvalid ||
        !LengthPropertyFunctions::CanAnimateKeyword(property, keyword)) {
      return nullptr;
    }
    return MakeGarbageCollected<InterpolableLength>(keyword, interpolate_size);
  }

  if (length.IsCalculated() && length.GetCalculationValue().IsExpression()) {
    auto unzoomed_calc = length.GetCalculationValue().Zoom(1.0 / zoom);
    return MakeGarbageCollected<InterpolableLength>(
        *CSSMathExpressionNode::Create(*unzoomed_calc));
  }

  PixelsAndPercent pixels_and_percent = length.GetPixelsAndPercent();
  CSSLengthArray length_array;

  length_array.values[CSSPrimitiveValue::kUnitTypePixels] =
      pixels_and_percent.pixels / zoom;
  length_array.type_flags[CSSPrimitiveValue::kUnitTypePixels] =
      pixels_and_percent.has_explicit_pixels;

  length_array.values[CSSPrimitiveValue::kUnitTypePercentage] =
      pixels_and_percent.percent;
  length_array.type_flags[CSSPrimitiveValue::kUnitTypePercentage] =
      pixels_and_percent.has_explicit_percent;
  return MakeGarbageCollected<InterpolableLength>(std::move(length_array));
}

bool InterpolableLength::IsCalcSize() const {
  if (!IsExpression()) {
    return false;
  }
  const auto* operation =
      DynamicTo<CSSMathExpressionOperation>(expression_.Get());
  return operation && operation->IsCalcSize();
}

namespace {

const CSSMathExpressionNode& ExtractCalcSizeBasis(
    const CSSMathExpressionNode* node) {
  const auto* operation = DynamicTo<CSSMathExpressionOperation>(node);
  if (!operation || !operation->IsCalcSize()) {
    return *node;
  }

  return ExtractCalcSizeBasis(operation->GetOperands()[0]);
}

}  // namespace

// static
bool InterpolableLength::CanMergeValues(const InterpolableValue* start,
                                        const InterpolableValue* end) {
  const auto& start_length = To<InterpolableLength>(*start);
  const auto& end_length = To<InterpolableLength>(*end);

  // Implement the rules in
  // https://drafts.csswg.org/css-values-5/#interp-calc-size, but
  // without actually writing the implicit conversion of the "other"
  // value to a calc-size().  This means that if one value is a
  // calc-size(), the other value converts to:
  // * for intrinsic size keywords, a calc-size(value, size)
  // * for other values, a calc-size(any, value)

  // Only animate to or from width keywords if the other endpoint of the
  // animation is a calc-size() expression.  And only animate between
  // calc-size() expressions or between a keyword and a calc-size() expression
  // if they have compatible basis.

  const bool start_is_keyword = start_length.IsKeyword();
  const bool end_is_keyword = end_length.IsKeyword();
  if (start_is_keyword || end_is_keyword) {
    // Only animate to or from width keywords if the other endpoint of the
    // animation is a calc-size() expression.
    const InterpolableLength* keyword;
    const InterpolableLength* non_keyword;
    if (start_is_keyword) {
      if (end_is_keyword) {
        return false;
      }
      keyword = &start_length;
      non_keyword = &end_length;
    } else {
      non_keyword = &start_length;
      keyword = &end_length;
    }

    if (!non_keyword->IsCalcSize()) {
      // Check the 'interpolate-size' value stored with the keyword.
      return keyword->IsKeywordFullyInterpolable();
    }
    const CSSMathExpressionNode& basis =
        ExtractCalcSizeBasis(non_keyword->expression_);

    if (const auto* basis_literal =
            DynamicTo<CSSMathExpressionKeywordLiteral>(basis)) {
      return basis_literal->GetValue() == keyword->keyword_ ||
             basis_literal->GetValue() == CSSValueID::kAny;
    }

    return false;
  }

  // Only animate between calc-size() expressions if they have compatible
  // basis.  This includes checking the type of the keyword, but it also
  // includes broad compatibility for 'any', and for animating between
  // different <calc-sum> values.  There are also some cases where we
  // need to check that we don't exceed the expansion limit for
  // substituting to handle nested calc-size() expressions.
  //
  // CreateArithmeticOperationAndSimplifyCalcSize knows how to determine
  // this.
  if (start_length.IsCalcSize() && end_length.IsCalcSize()) {
    return CSSMathExpressionOperation::
               CreateArithmeticOperationAndSimplifyCalcSize(
                   start_length.expression_, end_length.expression_,
                   CSSMathOperator::kAdd) != nullptr;
  }

  return true;
}

// static
PairwiseInterpolationValue InterpolableLength::MaybeMergeSingles(
    InterpolableValue* start,
    InterpolableValue* end) {
  // TODO(crbug.com/991672): We currently have a lot of "fast paths" that do not
  // go through here, and hence, do not merge the percentage info of two
  // lengths. We should stop doing that.
  auto& start_length = To<InterpolableLength>(*start);
  auto& end_length = To<InterpolableLength>(*end);

  if (!CanMergeValues(start, end)) {
    return nullptr;
  }

  if (start_length.HasPercentage() || end_length.HasPercentage()) {
    start_length.SetHasPercentage();
    end_length.SetHasPercentage();
  }
  if (start_length.IsExpression() || end_length.IsExpression()) {
    start_length.SetExpression(start_length.AsExpression());
    end_length.SetExpression(end_length.AsExpression());
  }
  return PairwiseInterpolationValue(start, end);
}

InterpolableLength::InterpolableLength(CSSLengthArray&& length_array) {
  SetLengthArray(std::move(length_array));
}

void InterpolableLength::SetLengthArray(CSSLengthArray&& length_array) {
  type_ = Type::kLengthArray;
  length_array_ = std::move(length_array);
  expression_.Clear();
}

InterpolableLength::InterpolableLength(
    const CSSMathExpressionNode& expression) {
  SetExpression(expression);
}

void InterpolableLength::SetExpression(
    const CSSMathExpressionNode& expression) {
  type_ = Type::kExpression;
  expression_ = &expression;
}

InterpolableLength::InterpolableLength(
    CSSValueID keyword,
    std::optional<EInterpolateSize> interpolate_size) {
  SetKeyword(keyword, interpolate_size);
}

void InterpolableLength::SetKeyword(
    CSSValueID keyword,
    std::optional<EInterpolateSize> interpolate_size) {
  if (interpolate_size) {
    switch (*interpolate_size) {
      case EInterpolateSize::kNumericOnly:
        type_ = Type::kRestrictedKeyword;
        break;
      case EInterpolateSize::kAllowKeywords:
        type_ = Type::kFullyInterpolableKeyword;
        break;
      default:
        NOTREACHED();
    }
  } else {
    type_ = Type::kUnknownKeyword;
  }
  keyword_ = keyword;
  expression_.Clear();
}

void InterpolableLength::SetInterpolateSize(EInterpolateSize interpolate_size) {
  if (!IsKeyword()) {
    return;
  }

  // We can't make useful assertions about this not changing an
  // already-set type because, for CSS transitions, we do exactly that,
  // for the length that comes from the before-change style (in the case
  // where it comes from an underlying value), so that it uses the
  // interpolate-size value from the after-change style.

  switch (interpolate_size) {
    case EInterpolateSize::kNumericOnly:
      type_ = Type::kRestrictedKeyword;
      break;
    case EInterpolateSize::kAllowKeywords:
      type_ = Type::kFullyInterpolableKeyword;
      break;
    default:
      NOTREACHED();
  }
}

InterpolableLength* InterpolableLength::RawClone() const {
  return MakeGarbageCollected<InterpolableLength>(*this);
}

bool InterpolableLength::HasPercentage() const {
  switch (type_) {
    case Type::kRestrictedKeyword:
    case Type::kFullyInterpolableKeyword:
    case Type::kUnknownKeyword:
      return false;
    case Type::kLengthArray:
      return length_array_.type_flags.test(
          CSSPrimitiveValue::kUnitTypePercentage);
    case Type::kExpression:
      return expression_->HasPercentage();
  }
  NOTREACHED();
}

void InterpolableLength::SetHasPercentage() {
  if (HasPercentage())
    return;

  if (IsLengthArray()) {
    length_array_.type_flags.set(CSSPrimitiveValue::kUnitTypePercentage);
    return;
  }

  if (IsKeyword()) {
    SetExpression(AsExpression());
  }

  DEFINE_STATIC_LOCAL(Persistent<CSSMathExpressionNode>, zero_percent,
                      {PercentageNode(0)});
  SetExpression(
      *CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          expression_, zero_percent, CSSMathOperator::kAdd));
}

void InterpolableLength::SubtractFromOneHundredPercent() {
  if (IsLengthArray()) {
    for (double& value : length_array_.values)
      value *= -1;
    length_array_.values[CSSPrimitiveValue::kUnitTypePercentage] += 100;
    length_array_.type_flags.set(CSSPrimitiveValue::kUnitTypePercentage);
    return;
  }

  if (IsKeyword()) {
    SetExpression(AsExpression());
  }

  DEFINE_STATIC_LOCAL(Persistent<CSSMathExpressionNode>, hundred_percent,
                      {PercentageNode(100)});
  SetExpression(
      *CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          hundred_percent, expression_, CSSMathOperator::kSubtract));
}

bool InterpolableLength::IsNeutralValue() const {
  return IsLengthArray() && length_array_.type_flags.none();
}

static double ClampToRange(double x, Length::ValueRange range) {
  return (range == Length::ValueRange::kNonNegative && x < 0) ? 0 : x;
}

static const CSSNumericLiteralValue& ClampNumericLiteralValueToRange(
    const CSSNumericLiteralValue& value,
    CSSPrimitiveValue::ValueRange range) {
  if (range == CSSPrimitiveValue::ValueRange::kAll || value.DoubleValue() >= 0)
    return value;
  return *CSSNumericLiteralValue::Create(0, value.GetType());
}

static UnitType IndexToUnitType(wtf_size_t index) {
  return CSSPrimitiveValue::LengthUnitTypeToUnitType(
      static_cast<CSSPrimitiveValue::LengthUnitType>(index));
}

Length InterpolableLength::CreateLength(
    const CSSToLengthConversionData& conversion_data,
    Length::ValueRange range) const {
  if (IsExpression()) {
    if (expression_->Category() == kCalcLength) {
      double pixels = expression_->ComputeLengthPx(conversion_data);
      return Length::Fixed(CSSPrimitiveValue::ClampToCSSLengthRange(
          ClampToRange(pixels, range)));
    }
    // Passing true for ToCalcValue is a dirty hack to ensure that we don't
    // create a degenerate value when animating 'background-position', while we
    // know it may cause some minor animation glitches for the other properties.
    return Length(expression_->ToCalcValue(conversion_data, range, true));
  }

  if (IsKeyword()) {
    return Length(CSSValueIDToLengthType(keyword_));
  }

  DCHECK(IsLengthArray());
  bool has_percentage = HasPercentage();
  double pixels = 0;
  double percentage = 0;
  for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
    double value = CSSValueClampingUtils::ClampLength(length_array_.values[i]);
    if (value == 0)
      continue;
    if (i == CSSPrimitiveValue::kUnitTypePercentage) {
      percentage = value;
    } else {
      pixels += conversion_data.ZoomedComputedPixels(value, IndexToUnitType(i));
    }
  }
  pixels = CSSValueClampingUtils::ClampLength(pixels);

  if (percentage != 0)
    has_percentage = true;
  if (pixels != 0 && has_percentage) {
    pixels = ClampTo<float>(pixels);
    if (percentage == 0) {
      // Match the clamping behavior in the StyleBuilder code path,
      // which goes through CSSPrimitiveValue::CreateFromLength and then
      // CSSPrimitiveValue::ConvertToLength.
      pixels = CSSPrimitiveValue::ClampToCSSLengthRange(pixels);
    }
    return Length(CalculationValue::Create(
        PixelsAndPercent(pixels, ClampTo<float>(percentage),
                         /*has_explicit_pixels=*/true,
                         /*has_explicit_percent=*/true),
        range));
  }
  if (has_percentage)
    return Length::Percent(ClampToRange(percentage, range));
  return Length::Fixed(
      CSSPrimitiveValue::ClampToCSSLengthRange(ClampToRange(pixels, range)));
}

const CSSPrimitiveValue* InterpolableLength::CreateCSSValue(
    Length::ValueRange range) const {
  if (!IsLengthArray()) {
    return CSSMathFunctionValue::Create(
        &AsExpression(),
        CSSPrimitiveValue::ValueRangeForLengthValueRange(range));
  }

  DCHECK(IsLengthArray());
  if (length_array_.type_flags.count() > 1u) {
    const CSSMathExpressionNode& expression = AsExpression();
    if (!expression.IsNumericLiteral()) {
      return CSSMathFunctionValue::Create(
          &expression, CSSPrimitiveValue::ValueRangeForLengthValueRange(range));
    }

    // This creates a temporary CSSMathExpressionNode. Eliminate it if this
    // results in significant performance regression.
    return &ClampNumericLiteralValueToRange(
        To<CSSMathExpressionNumericLiteral>(expression).GetValue(),
        CSSPrimitiveValue::ValueRangeForLengthValueRange(range));
  }

  for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
    if (length_array_.type_flags.test(i)) {
      double value = ClampToRange(length_array_.values[i], range);
      UnitType unit_type = IndexToUnitType(i);
      return CSSNumericLiteralValue::Create(value, unit_type);
    }
  }

  return CSSNumericLiteralValue::Create(0, UnitType::kPixels);
}

const CSSMathExpressionNode& InterpolableLength::AsExpression() const {
  if (IsExpression())
    return *expression_;

  if (IsKeyword()) {
    const auto* basis = CSSMathExpressionKeywordLiteral::Create(
        keyword_, CSSMathExpressionKeywordLiteral::Context::kCalcSize);
    const auto* calculation = CSSMathExpressionKeywordLiteral::Create(
        CSSValueID::kSize, CSSMathExpressionKeywordLiteral::Context::kCalcSize);
    return *CSSMathExpressionOperation::CreateCalcSizeOperation(basis,
                                                                calculation);
  }

  DCHECK(IsLengthArray());
  bool has_percentage = HasPercentage();

  CSSMathExpressionNode* root_node = nullptr;
  for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
    double value = length_array_.values[i];
    if (value == 0 &&
        (i != CSSPrimitiveValue::kUnitTypePercentage || !has_percentage)) {
      continue;
    }
    CSSNumericLiteralValue* current_value =
        CSSNumericLiteralValue::Create(value, IndexToUnitType(i));
    CSSMathExpressionNode* current_node =
        CSSMathExpressionNumericLiteral::Create(current_value);
    if (!root_node) {
      root_node = current_node;
    } else {
      root_node = CSSMathExpressionOperation::CreateArithmeticOperation(
          root_node, current_node, CSSMathOperator::kAdd);
    }
  }

  if (root_node)
    return *root_node;
  return *CSSMathExpressionNumericLiteral::Create(
      CSSNumericLiteralValue::Create(0, UnitType::kPixels));
}

void InterpolableLength::Scale(double scale) {
  if (IsLengthArray()) {
    for (auto& value : length_array_.values)
      value *= scale;
    return;
  }

  if (IsKeyword()) {
    SetExpression(AsExpression());
  }

  DCHECK(IsExpression());
  SetExpression(
      *CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          expression_, NumberNode(scale), CSSMathOperator::kMultiply));
}

void InterpolableLength::Add(const InterpolableValue& other) {
  const InterpolableLength& other_length = To<InterpolableLength>(other);
  if (IsLengthArray() && other_length.IsLengthArray()) {
    for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
      length_array_.values[i] =
          length_array_.values[i] + other_length.length_array_.values[i];
    }
    length_array_.type_flags |= other_length.length_array_.type_flags;
    return;
  }

  const CSSMathExpressionNode* result =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          &AsExpression(), &other_length.AsExpression(), CSSMathOperator::kAdd);
  CHECK(result)
      << "should not attempt to interpolate when result would be IACVT";
  SetExpression(*result);
}

void InterpolableLength::ScaleAndAdd(double scale,
                                     const InterpolableValue& other) {
  const InterpolableLength& other_length = To<InterpolableLength>(other);
  if (IsLengthArray() && other_length.IsLengthArray()) {
    for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
      length_array_.values[i] = length_array_.values[i] * scale +
                                other_length.length_array_.values[i];
    }
    length_array_.type_flags |= other_length.length_array_.type_flags;
    return;
  }

  const CSSMathExpressionNode* scaled =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          &AsExpression(), NumberNode(scale), CSSMathOperator::kMultiply);
  const CSSMathExpressionNode* result =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          scaled, &other_length.AsExpression(), CSSMathOperator::kAdd);
  CHECK(result)
      << "should not attempt to interpolate when result would be IACVT";
  SetExpression(*result);
}

void InterpolableLength::AssertCanInterpolateWith(
    const InterpolableValue& other) const {
  DCHECK(other.IsLength());
  // TODO(crbug.com/991672): Ensure that all |MergeSingles| variants that merge
  // two |InterpolableLength| objects should also assign them the same shape
  // (i.e. type flags) after merging into a |PairwiseInterpolationValue|. We
  // currently fail to do that, and hit the following DCHECK:
  // DCHECK_EQ(HasPercentage(),
  //           To<InterpolableLength>(other).HasPercentage());
}

void InterpolableLength::Interpolate(const InterpolableValue& to,
                                     const double progress,
                                     InterpolableValue& result) const {
  const auto& to_length = To<InterpolableLength>(to);
  auto& result_length = To<InterpolableLength>(result);
  if (IsLengthArray() && to_length.IsLengthArray()) {
    if (!result_length.IsLengthArray())
      result_length.SetLengthArray(CSSLengthArray());
    const CSSLengthArray& to_length_array = to_length.length_array_;
    CSSLengthArray& result_length_array =
        To<InterpolableLength>(result).length_array_;
    for (wtf_size_t i = 0; i < length_array_.values.size(); ++i) {
      result_length_array.values[i] =
          Blend(length_array_.values[i], to_length_array.values[i], progress);
    }
    result_length_array.type_flags =
        length_array_.type_flags | to_length_array.type_flags;
    return;
  }

  const CSSMathExpressionNode* blended_from =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          &AsExpression(), NumberNode(1 - progress),
          CSSMathOperator::kMultiply);
  const CSSMathExpressionNode* blended_to =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          &to_length.AsExpression(), NumberNode(progress),
          CSSMathOperator::kMultiply);
  const CSSMathExpressionNode* result_expression =
      CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
          blended_from, blended_to, CSSMathOperator::kAdd);
  CHECK(result_expression)
      << "should not attempt to interpolate when result would be IACVT";
  result_length.SetExpression(*result_expression);
}

void InterpolableLength::Trace(Visitor* v) const {
  InterpolableValue::Trace(v);
  v->Trace(expression_);
}

}  // namespace blink