summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/cssom/css_transform_value.cc
blob: 53b81acb11569f629722b9ba0d086aef88d38a4b (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
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
// 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/css/cssom/css_transform_value.h"

#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/cssom/css_transform_component.h"
#include "third_party/blink/renderer/core/geometry/dom_matrix.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

namespace blink {

CSSTransformValue* CSSTransformValue::Create(
    const HeapVector<Member<CSSTransformComponent>>& transform_components,
    ExceptionState& exception_state) {
  CSSTransformValue* value = Create(transform_components);
  if (!value) {
    exception_state.ThrowTypeError(
        "CSSTransformValue must have at least one component");
    return nullptr;
  }
  return value;
}

CSSTransformValue* CSSTransformValue::Create(
    const HeapVector<Member<CSSTransformComponent>>& transform_components) {
  if (transform_components.IsEmpty())
    return nullptr;
  return MakeGarbageCollected<CSSTransformValue>(transform_components);
}

CSSTransformValue* CSSTransformValue::FromCSSValue(const CSSValue& css_value) {
  auto* css_value_list = DynamicTo<CSSValueList>(css_value);
  if (!css_value_list) {
    // TODO(meade): Also need to check the separator here if we care.
    return nullptr;
  }
  HeapVector<Member<CSSTransformComponent>> components;
  for (const CSSValue* value : *css_value_list) {
    CSSTransformComponent* component =
        CSSTransformComponent::FromCSSValue(*value);
    if (!component)
      return nullptr;
    components.push_back(component);
  }
  return CSSTransformValue::Create(components);
}

bool CSSTransformValue::is2D() const {
  return std::all_of(transform_components_.begin(), transform_components_.end(),
                     [](const auto& component) { return component->is2D(); });
}

DOMMatrix* CSSTransformValue::toMatrix(ExceptionState& exception_state) const {
  DOMMatrix* matrix = DOMMatrix::Create();
  for (wtf_size_t i = 0; i < transform_components_.size(); i++) {
    const DOMMatrix* matrixComponent =
        transform_components_[i]->toMatrix(exception_state);
    if (matrixComponent) {
      matrix->multiplySelf(*matrixComponent);
    }
  }
  return matrix;
}

const CSSValue* CSSTransformValue::ToCSSValue() const {
  CSSValueList* transform_css_value = CSSValueList::CreateSpaceSeparated();
  for (wtf_size_t i = 0; i < transform_components_.size(); i++) {
    const CSSValue* component = transform_components_[i]->ToCSSValue();
    // TODO(meade): Remove this check once numbers and lengths are rewritten.
    if (!component)
      return nullptr;
    transform_css_value->Append(*component);
  }
  return transform_css_value;
}

IndexedPropertySetterResult CSSTransformValue::AnonymousIndexedSetter(
    unsigned index,
    const Member<CSSTransformComponent> component,
    ExceptionState& exception_state) {
  if (index < transform_components_.size()) {
    transform_components_[index] = component;
    return IndexedPropertySetterResult::kIntercepted;
  }

  if (index == transform_components_.size()) {
    transform_components_.push_back(component);
    return IndexedPropertySetterResult::kIntercepted;
  }

  exception_state.ThrowRangeError(
      ExceptionMessages::IndexOutsideRange<unsigned>(
          "index", index, 0, ExceptionMessages::kInclusiveBound,
          transform_components_.size(), ExceptionMessages::kInclusiveBound));
  return IndexedPropertySetterResult::kIntercepted;
}

}  // namespace blink