summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/animation/length_units_checker.h
blob: f687891ae9b5bb4fa3e1cc99f66321379f754135 (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
68
69
70
71
72
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_LENGTH_UNITS_CHECKER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_LENGTH_UNITS_CHECKER_H_

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_type.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"

namespace blink {

class LengthUnitsChecker : public CSSInterpolationType::CSSConversionChecker {
 public:
  static std::unique_ptr<LengthUnitsChecker> MaybeCreate(
      const CSSPrimitiveValue::LengthTypeFlags& length_types,
      const StyleResolverState& state) {
    bool create = false;
    wtf_size_t last_index = 0;
    CSSLengthArray length_array;
    for (wtf_size_t i = 0; i < length_types.size(); ++i) {
      if (i == CSSPrimitiveValue::kUnitTypePercentage || !length_types[i])
        continue;
      length_array.values[i] = LengthUnit(i, state.CssToLengthConversionData());
      length_array.type_flags.set(i);
      create = true;
      last_index = i;
    }
    if (!create)
      return nullptr;
    return base::WrapUnique(
        new LengthUnitsChecker(std::move(length_array), last_index));
  }

  bool IsValid(const StyleResolverState& state,
               const InterpolationValue& underlying) const final {
    for (wtf_size_t i = 0; i <= last_index_; i++) {
      if (i == CSSPrimitiveValue::kUnitTypePercentage ||
          !length_array_.type_flags[i])
        continue;
      if (length_array_.values[i] !=
          LengthUnit(i, state.CssToLengthConversionData()))
        return false;
    }
    return true;
  }

  static double LengthUnit(wtf_size_t length_unit_type,
                           const CSSToLengthConversionData& conversion_data) {
    return conversion_data.ZoomedComputedPixels(
        1,
        CSSPrimitiveValue::LengthUnitTypeToUnitType(
            static_cast<CSSPrimitiveValue::LengthUnitType>(length_unit_type)));
  }

 private:
  LengthUnitsChecker(CSSPrimitiveValue::CSSLengthArray&& length_array,
                     wtf_size_t last_index)
      : length_array_(std::move(length_array)), last_index_(last_index) {}

  const CSSLengthArray length_array_;
  const wtf_size_t last_index_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_LENGTH_UNITS_CHECKER_H_