OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "core/css/cssom/StyleCalcLength.h" | 5 #include "core/css/cssom/StyleCalcLength.h" |
6 | 6 |
7 #include "core/css/CSSCalculationValue.h" | 7 #include "core/css/CSSCalculationValue.h" |
8 #include "core/css/CSSPrimitiveValue.h" | 8 #include "core/css/CSSPrimitiveValue.h" |
9 #include "core/css/cssom/CalcDictionary.h" | 9 #include "core/css/cssom/CalcDictionary.h" |
10 #include "core/css/cssom/SimpleLength.h" | 10 #include "core/css/cssom/SimpleLength.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 StyleCalcLength* result = StyleCalcLength::create(this, exceptionState); | 117 StyleCalcLength* result = StyleCalcLength::create(this, exceptionState); |
118 for (unsigned i = 0; i < LengthUnit::Count; ++i) { | 118 for (unsigned i = 0; i < LengthUnit::Count; ++i) { |
119 LengthUnit lengthUnit = static_cast<LengthUnit>(i); | 119 LengthUnit lengthUnit = static_cast<LengthUnit>(i); |
120 if (has(lengthUnit)) { | 120 if (has(lengthUnit)) { |
121 result->set(m_values[i] / x, lengthUnit); | 121 result->set(m_values[i] / x, lengthUnit); |
122 } | 122 } |
123 } | 123 } |
124 return result; | 124 return result; |
125 } | 125 } |
126 | 126 |
| 127 String StyleCalcLength::cssString() const |
| 128 { |
| 129 StringBuilder builder; |
| 130 builder.appendLiteral("calc("); |
| 131 for (unsigned i = 0; i < LengthUnit::Count; ++i) { |
| 132 LengthUnit lengthUnit = static_cast<LengthUnit>(i); |
| 133 if (has(lengthUnit)) { |
| 134 double value = get(lengthUnit); |
| 135 if (value >= 0 && i > 0) { |
| 136 builder.appendLiteral(" + "); |
| 137 } else if (value < 0 && i > 0) { |
| 138 builder.appendLiteral(" - "); |
| 139 } else if (value < 0) { |
| 140 builder.append('-'); |
| 141 } |
| 142 builder.appendNumber(std::abs(get(lengthUnit))); |
| 143 builder.append(lengthTypeToString(lengthUnit)); |
| 144 } |
| 145 } |
| 146 builder.append(')'); |
| 147 return builder.toString(); |
| 148 } |
| 149 |
127 PassRefPtrWillBeRawPtr<CSSValue> StyleCalcLength::toCSSValue() const | 150 PassRefPtrWillBeRawPtr<CSSValue> StyleCalcLength::toCSSValue() const |
128 { | 151 { |
129 // Create a CSS Calc Value, then put it into a CSSPrimitiveValue | 152 // Create a CSS Calc Value, then put it into a CSSPrimitiveValue |
130 RefPtrWillBeRawPtr<CSSCalcExpressionNode> node = nullptr; | 153 RefPtrWillBeRawPtr<CSSCalcExpressionNode> node = nullptr; |
131 for (unsigned i = 0; i < LengthUnit::Count; ++i) { | 154 for (unsigned i = 0; i < LengthUnit::Count; ++i) { |
132 LengthUnit lengthUnit = static_cast<LengthUnit>(i); | 155 LengthUnit lengthUnit = static_cast<LengthUnit>(i); |
133 if (!has(lengthUnit)) | 156 if (!has(lengthUnit)) |
134 continue; | 157 break; |
135 double value = get(lengthUnit); | 158 double value = get(lengthUnit); |
136 CSSPrimitiveValue::UnitType primitiveUnit = lengthTypeToPrimitiveType(le
ngthUnit); | 159 CSSPrimitiveValue::UnitType primitiveUnit; |
| 160 if (lengthUnit == LengthUnit::Percent) { |
| 161 primitiveUnit = CSSPrimitiveValue::UnitType::Percentage; |
| 162 } else { |
| 163 // TODO: Don't re-parse the unit here. |
| 164 primitiveUnit = CSSPrimitiveValue::fromName(lengthTypeToString(lengt
hUnit)); |
| 165 } |
137 if (node) { | 166 if (node) { |
138 node = CSSCalcValue::createExpressionNode( | 167 node = CSSCalcValue::createExpressionNode( |
139 node, | 168 node, |
140 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(std
::abs(value), primitiveUnit)), | 169 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(val
ue, primitiveUnit)), |
141 value >= 0 ? CalcAdd : CalcSubtract); | 170 CalcAdd); |
142 } else { | 171 } else { |
143 node = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(
value, primitiveUnit)); | 172 node = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(
value, primitiveUnit)); |
144 } | 173 } |
145 } | 174 } |
146 return CSSPrimitiveValue::create(CSSCalcValue::create(node)); | 175 return CSSPrimitiveValue::create(CSSCalcValue::create(node)); |
147 } | 176 } |
148 | 177 |
149 } // namespace blink | 178 } // namespace blink |
OLD | NEW |