summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/animation/css_position_axis_list_interpolation_type.cc
blob: aefb23538d3d5969a28ce59c688a7be5d8db5adf (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/css_position_axis_list_interpolation_type.h"

#include "third_party/blink/renderer/core/animation/interpolable_length.h"
#include "third_party/blink/renderer/core/animation/list_interpolation_functions.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/css_value_pair.h"

namespace blink {

InterpolationValue
CSSPositionAxisListInterpolationType::ConvertPositionAxisCSSValue(
    const CSSValue& value) {
  if (const auto* pair = DynamicTo<CSSValuePair>(value)) {
    InterpolationValue result(
        InterpolableLength::MaybeConvertCSSValue(pair->Second()));
    CSSValueID side = To<CSSIdentifierValue>(pair->First()).GetValueID();
    if (side == CSSValueID::kRight || side == CSSValueID::kBottom) {
      To<InterpolableLength>(*result.interpolable_value)
          .SubtractFromOneHundredPercent();
    }
    return result;
  }

  if (value.IsPrimitiveValue())
    return InterpolationValue(InterpolableLength::MaybeConvertCSSValue(value));

  const auto* ident = DynamicTo<CSSIdentifierValue>(value);
  if (!ident)
    return nullptr;

  switch (ident->GetValueID()) {
    case CSSValueID::kLeft:
    case CSSValueID::kTop:
      return InterpolationValue(InterpolableLength::CreatePercent(0));
    case CSSValueID::kRight:
    case CSSValueID::kBottom:
      return InterpolationValue(InterpolableLength::CreatePercent(100));
    case CSSValueID::kCenter:
      return InterpolationValue(InterpolableLength::CreatePercent(50));
    default:
      NOTREACHED();
      return nullptr;
  }
}

InterpolationValue CSSPositionAxisListInterpolationType::MaybeConvertValue(
    const CSSValue& value,
    const StyleResolverState*,
    ConversionCheckers&) const {
  if (!value.IsBaseValueList()) {
    return ListInterpolationFunctions::CreateList(
        1, [&value](size_t) { return ConvertPositionAxisCSSValue(value); });
  }

  const auto& list = To<CSSValueList>(value);
  return ListInterpolationFunctions::CreateList(
      list.length(), [&list](wtf_size_t index) {
        return ConvertPositionAxisCSSValue(list.Item(index));
      });
}

}  // namespace blink