OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ListStyleInterpolation_h | 5 #ifndef ListStyleInterpolation_h |
6 #define ListStyleInterpolation_h | 6 #define ListStyleInterpolation_h |
7 | 7 |
8 #include "core/animation/StyleInterpolation.h" | 8 #include "core/animation/StyleInterpolation.h" |
9 #include "core/css/CSSPrimitiveValue.h" | 9 #include "core/css/CSSPrimitiveValue.h" |
10 #include "core/css/CSSValueList.h" | 10 #include "core/css/CSSValueList.h" |
11 #include "core/css/resolver/StyleBuilder.h" | 11 #include "core/css/resolver/StyleBuilder.h" |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 template<typename InterpolationType, typename NonInterpolableData> | 15 template<typename InterpolationType, typename NonInterpolableData> |
16 class ListStyleInterpolationImpl : public StyleInterpolation { | 16 class ListStyleInterpolationImpl : public StyleInterpolation { |
17 public: | 17 public: |
18 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
NonInterpolableData>> maybeCreateFromList(const CSSValue& start, const CSSValue&
end, CSSPropertyID id, InterpolationRange range = RangeAll) | 18 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
NonInterpolableData>> maybeCreateFromList(const CSSValueList& startList, const C
SSValueList& endList, CSSPropertyID id, InterpolationRange range = RangeAll) |
19 { | 19 { |
20 if (start.isValueList() && end.isValueList() && toCSSValueList(start).le
ngth() == toCSSValueList(end).length()) { | 20 if (startList.length() == endList.length()) { |
21 const CSSValueList& startList = toCSSValueList(start); | 21 for (size_t i = 0; i < startList.length(); i++) { |
22 const CSSValueList& endList = toCSSValueList(end); | 22 if (!InterpolationType::canCreateFrom(startList.item(i), endList
.item(i))) { |
23 | |
24 for (size_t i = 0; i < toCSSValueList(start).length(); i++) { | |
25 if (!InterpolationType::canCreateFrom(*startList.item(i), *endLi
st.item(i))) { | |
26 return nullptr; | 23 return nullptr; |
27 } | 24 } |
28 } | 25 } |
29 | 26 |
30 Vector<typename InterpolationType::NonInterpolableType> startNonInte
rpolableData; | 27 Vector<typename InterpolationType::NonInterpolableType> startNonInte
rpolableData; |
31 | 28 |
32 OwnPtrWillBeRawPtr<InterpolableValue> startValue = listToInterpolabl
eValue(start, &startNonInterpolableData); | 29 OwnPtrWillBeRawPtr<InterpolableValue> startValue = listToInterpolabl
eValue(startList, &startNonInterpolableData); |
33 OwnPtrWillBeRawPtr<InterpolableValue> endValue = listToInterpolableV
alue(end); | 30 OwnPtrWillBeRawPtr<InterpolableValue> endValue = listToInterpolableV
alue(endList); |
34 | 31 |
35 return adoptRefWillBeNoop(new ListStyleInterpolationImpl<Interpolati
onType, NonInterpolableData>(startValue.release(), endValue.release(), id, start
NonInterpolableData, range)); | 32 return adoptRefWillBeNoop(new ListStyleInterpolationImpl<Interpolati
onType, NonInterpolableData>(startValue.release(), endValue.release(), id, start
NonInterpolableData, range)); |
36 } | 33 } |
37 return nullptr; | 34 return nullptr; |
38 } | 35 } |
39 | 36 |
40 virtual void apply(StyleResolverState& state) const override | 37 virtual void apply(StyleResolverState& state) const override |
41 { | 38 { |
42 StyleBuilder::applyProperty(m_id, state, interpolableValueToList(m_cache
dValue.get(), m_nonInterpolableData, m_range).get()); | 39 StyleBuilder::applyProperty(m_id, state, interpolableValueToList(m_cache
dValue.get(), m_nonInterpolableData, m_range)); |
43 } | 40 } |
44 | 41 |
45 private: | 42 private: |
46 ListStyleInterpolationImpl(PassOwnPtrWillBeRawPtr<InterpolableValue> start,
PassOwnPtrWillBeRawPtr<InterpolableValue> end, CSSPropertyID id, | 43 ListStyleInterpolationImpl(PassOwnPtrWillBeRawPtr<InterpolableValue> start,
PassOwnPtrWillBeRawPtr<InterpolableValue> end, CSSPropertyID id, |
47 Vector<typename InterpolationType::NonInterpolableType> nonInterpolableD
ata, InterpolationRange range = RangeAll) | 44 Vector<typename InterpolationType::NonInterpolableType> nonInterpolableD
ata, InterpolationRange range = RangeAll) |
48 : StyleInterpolation(start, end, id) | 45 : StyleInterpolation(start, end, id) |
49 , m_range(range) | 46 , m_range(range) |
50 { | 47 { |
51 m_nonInterpolableData.swap(nonInterpolableData); | 48 m_nonInterpolableData.swap(nonInterpolableData); |
52 } | 49 } |
53 | 50 |
54 InterpolationRange m_range; | 51 InterpolationRange m_range; |
55 | 52 |
56 Vector<typename InterpolationType::NonInterpolableType> m_nonInterpolableDat
a; | 53 Vector<typename InterpolationType::NonInterpolableType> m_nonInterpolableDat
a; |
57 | 54 |
58 static PassOwnPtrWillBeRawPtr<InterpolableValue> listToInterpolableValue(con
st CSSValue& value, Vector<typename InterpolationType::NonInterpolableType>* non
InterpolableData = nullptr) | 55 static PassOwnPtrWillBeRawPtr<InterpolableValue> listToInterpolableValue(con
st CSSValueList& listValue, Vector<typename InterpolationType::NonInterpolableTy
pe>* nonInterpolableData = nullptr) |
59 { | 56 { |
60 const CSSValueList& listValue = toCSSValueList(value); | |
61 if (nonInterpolableData) | 57 if (nonInterpolableData) |
62 nonInterpolableData->reserveCapacity(listValue.length()); | 58 nonInterpolableData->reserveCapacity(listValue.length()); |
63 OwnPtrWillBeRawPtr<InterpolableList> result = InterpolableList::create(l
istValue.length()); | 59 OwnPtrWillBeRawPtr<InterpolableList> result = InterpolableList::create(l
istValue.length()); |
64 typename InterpolationType::NonInterpolableType elementData = typename I
nterpolationType::NonInterpolableType(); | 60 typename InterpolationType::NonInterpolableType elementData = typename I
nterpolationType::NonInterpolableType(); |
65 for (size_t i = 0; i < listValue.length(); i++) { | 61 for (size_t i = 0; i < listValue.length(); i++) { |
66 result->set(i, InterpolationType::toInterpolableValue(*listValue.ite
m(i), elementData)); | 62 result->set(i, InterpolationType::toInterpolableValue(listValue.item
(i), elementData)); |
67 if (nonInterpolableData) | 63 if (nonInterpolableData) |
68 nonInterpolableData->append(elementData); | 64 nonInterpolableData->append(elementData); |
69 } | 65 } |
70 return result.release(); | 66 return result.release(); |
71 } | 67 } |
72 | 68 |
73 static PassRefPtrWillBeRawPtr<CSSValue> interpolableValueToList(Interpolable
Value* value, const Vector<typename InterpolationType::NonInterpolableType>& non
InterpolableData, InterpolationRange range = RangeAll) | 69 static CSSValue interpolableValueToList(InterpolableValue* value, const Vect
or<typename InterpolationType::NonInterpolableType>& nonInterpolableData, Interp
olationRange range = RangeAll) |
74 { | 70 { |
75 InterpolableList* listValue = toInterpolableList(value); | 71 InterpolableList* listValue = toInterpolableList(value); |
76 RefPtrWillBeRawPtr<CSSValueList> result = CSSValueList::createCommaSepar
ated(); | 72 RefPtrWillBeRawPtr<CSSValueList> result = CSSValueList::createCommaSepar
ated(); |
77 | 73 |
78 ASSERT(nonInterpolableData.size() == listValue->length()); | 74 ASSERT(nonInterpolableData.size() == listValue->length()); |
79 | 75 |
80 for (size_t i = 0; i < listValue->length(); i++) | 76 for (size_t i = 0; i < listValue->length(); i++) |
81 result->append(InterpolationType::fromInterpolableValue(*(listValue-
>get(i)), nonInterpolableData[i], range)); | 77 result->append(InterpolationType::fromInterpolableValue(*(listValue-
>get(i)), nonInterpolableData[i], range)); |
82 return result.release(); | 78 return result.release(); |
83 } | 79 } |
84 | 80 |
85 friend class ListStyleInterpolationTest; | 81 friend class ListStyleInterpolationTest; |
86 }; | 82 }; |
87 | 83 |
88 template<typename InterpolationType> | 84 template<typename InterpolationType> |
89 class ListStyleInterpolationImpl<InterpolationType, void> : public StyleInterpol
ation { | 85 class ListStyleInterpolationImpl<InterpolationType, void> : public StyleInterpol
ation { |
90 public: | 86 public: |
91 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
void>> maybeCreateFromList(const CSSValue& start, const CSSValue& end, CSSProper
tyID id, InterpolationRange range = RangeAll) | 87 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
void>> maybeCreateFromList(const CSSValueList& startList, const CSSValueList& en
dList, CSSPropertyID id, InterpolationRange range = RangeAll) |
92 { | 88 { |
93 if (!start.isValueList() || !end.isValueList()) | |
94 return nullptr; | |
95 const CSSValueList& startList = toCSSValueList(start); | |
96 const CSSValueList& endList = toCSSValueList(end); | |
97 if (startList.length() != endList.length()) | 89 if (startList.length() != endList.length()) |
98 return nullptr; | 90 return nullptr; |
99 for (const auto& value : startList) { | 91 for (const auto& value : startList) { |
100 if (!InterpolationType::canCreateFrom(*value)) | 92 if (!InterpolationType::canCreateFrom(value)) |
101 return nullptr; | 93 return nullptr; |
102 } | 94 } |
103 for (const auto& value : endList) { | 95 for (const auto& value : endList) { |
104 if (!InterpolationType::canCreateFrom(*value)) | 96 if (!InterpolationType::canCreateFrom(value)) |
105 return nullptr; | 97 return nullptr; |
106 } | 98 } |
107 return adoptRefWillBeNoop(new ListStyleInterpolationImpl<InterpolationTy
pe, void>(listToInterpolableValue(start), listToInterpolableValue(end), id, rang
e)); | 99 return adoptRefWillBeNoop(new ListStyleInterpolationImpl<InterpolationTy
pe, void>(listToInterpolableValue(startList), listToInterpolableValue(endList),
id, range)); |
108 } | 100 } |
109 | 101 |
110 private: | 102 private: |
111 ListStyleInterpolationImpl(PassOwnPtrWillBeRawPtr<InterpolableValue> start,
PassOwnPtrWillBeRawPtr<InterpolableValue> end, CSSPropertyID id, InterpolationRa
nge range = RangeAll) | 103 ListStyleInterpolationImpl(PassOwnPtrWillBeRawPtr<InterpolableValue> start,
PassOwnPtrWillBeRawPtr<InterpolableValue> end, CSSPropertyID id, InterpolationRa
nge range = RangeAll) |
112 : StyleInterpolation(start, end, id), m_range(range) | 104 : StyleInterpolation(start, end, id), m_range(range) |
113 { | 105 { |
114 } | 106 } |
115 | 107 |
116 InterpolationRange m_range; | 108 InterpolationRange m_range; |
117 | 109 |
118 static PassOwnPtrWillBeRawPtr<InterpolableValue> listToInterpolableValue(con
st CSSValue& value) | 110 static PassOwnPtrWillBeRawPtr<InterpolableValue> listToInterpolableValue(con
st CSSValueList& listValue) |
119 { | 111 { |
120 const CSSValueList& listValue = toCSSValueList(value); | |
121 OwnPtrWillBeRawPtr<InterpolableList> result = InterpolableList::create(l
istValue.length()); | 112 OwnPtrWillBeRawPtr<InterpolableList> result = InterpolableList::create(l
istValue.length()); |
122 for (size_t i = 0; i < listValue.length(); i++) | 113 for (size_t i = 0; i < listValue.length(); i++) |
123 result->set(i, InterpolationType::toInterpolableValue(*listValue.ite
m(i))); | 114 result->set(i, InterpolationType::toInterpolableValue(listValue.item
(i))); |
124 return result.release(); | 115 return result.release(); |
125 } | 116 } |
126 | 117 |
127 static PassRefPtrWillBeRawPtr<CSSValue> interpolableValueToList(Interpolable
Value* value, InterpolationRange range = RangeAll) | 118 static CSSValue interpolableValueToList(InterpolableValue* value, Interpolat
ionRange range = RangeAll) |
128 { | 119 { |
129 InterpolableList* listValue = toInterpolableList(value); | 120 InterpolableList* listValue = toInterpolableList(value); |
130 RefPtrWillBeRawPtr<CSSValueList> result = CSSValueList::createCommaSepar
ated(); | 121 RefPtrWillBeRawPtr<CSSValueList> result = CSSValueList::createCommaSepar
ated(); |
131 | 122 |
132 for (size_t i = 0; i < listValue->length(); i++) | 123 for (size_t i = 0; i < listValue->length(); i++) |
133 result->append(InterpolationType::fromInterpolableValue(*(listValue-
>get(i)), range)); | 124 result->append(InterpolationType::fromInterpolableValue(*(listValue-
>get(i)), range)); |
134 return result.release(); | 125 return result.release(); |
135 } | 126 } |
136 | 127 |
137 virtual void apply(StyleResolverState& state) const override | 128 virtual void apply(StyleResolverState& state) const override |
138 { | 129 { |
139 StyleBuilder::applyProperty(m_id, state, interpolableValueToList(m_cache
dValue.get(), m_range).get()); | 130 StyleBuilder::applyProperty(m_id, state, interpolableValueToList(m_cache
dValue.get(), m_range)); |
140 } | 131 } |
141 | 132 |
142 friend class ListStyleInterpolationTest; | 133 friend class ListStyleInterpolationTest; |
143 | 134 |
144 }; | 135 }; |
145 | 136 |
146 template<typename InterpolationType> | 137 template<typename InterpolationType> |
147 class ListStyleInterpolation { | 138 class ListStyleInterpolation { |
148 public: | 139 public: |
149 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
typename InterpolationType::NonInterpolableType>> maybeCreateFromList(const CSS
Value& start, const CSSValue& end, CSSPropertyID id, InterpolationRange range =
RangeAll) | 140 static PassRefPtrWillBeRawPtr<ListStyleInterpolationImpl<InterpolationType,
typename InterpolationType::NonInterpolableType>> maybeCreateFromList(const CSS
ValueList& start, const CSSValueList& end, CSSPropertyID id, InterpolationRange
range = RangeAll) |
150 { | 141 { |
151 return ListStyleInterpolationImpl<InterpolationType, typename Interpolat
ionType::NonInterpolableType>::maybeCreateFromList(start, end, id, range); | 142 return ListStyleInterpolationImpl<InterpolationType, typename Interpolat
ionType::NonInterpolableType>::maybeCreateFromList(start, end, id, range); |
152 } | 143 } |
153 }; | 144 }; |
154 | 145 |
155 } // namespace blink | 146 } // namespace blink |
156 | 147 |
157 #endif // ListStyleInterpolation_h | 148 #endif // ListStyleInterpolation_h |
OLD | NEW |