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 #include "core/css/cssom/CSSSimpleLength.h" | 5 #include "core/css/cssom/CSSSimpleLength.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "core/css/CSSPrimitiveValue.h" | 8 #include "core/css/CSSPrimitiveValue.h" |
9 #include "core/css/cssom/CSSCalcLength.h" | 9 #include "core/css/cssom/CSSCalcLength.h" |
10 #include "wtf/text/StringBuilder.h" | 10 #include "wtf/text/StringBuilder.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 CSSSimpleLength* CSSSimpleLength::create(double value, const String& type, Excep
tionState& exceptionState) | 14 CSSSimpleLength* CSSSimpleLength::create(double value, const String& type, Excep
tionState& exceptionState) |
15 { | 15 { |
16 CSSPrimitiveValue::UnitType unit = CSSLengthValue::unitFromName(type); | 16 CSSPrimitiveValue::UnitType unit = CSSLengthValue::unitFromName(type); |
17 if (!CSSLengthValue::isSupportedLengthUnit(unit)) { | 17 if (!CSSLengthValue::isSupportedLengthUnit(unit)) { |
18 exceptionState.throwTypeError("Invalid unit for CSSSimpleLength: " + typ
e); | 18 exceptionState.throwTypeError("Invalid unit for CSSSimpleLength: " + typ
e); |
19 return nullptr; | 19 return nullptr; |
20 } | 20 } |
21 return new CSSSimpleLength(value, unit); | 21 return new CSSSimpleLength(value, unit); |
22 } | 22 } |
23 | 23 |
24 CSSSimpleLength* CSSSimpleLength::fromCSSValue(const CSSPrimitiveValue& value) | 24 CSSSimpleLength* CSSSimpleLength::fromCSSValue(const CSSPrimitiveValue& value) |
25 { | 25 { |
26 DCHECK(value.isLength()); | 26 DCHECK(value.isLength() || value.isPercentage()); |
| 27 if (value.isPercentage()) |
| 28 return new CSSSimpleLength(value.getDoubleValue(), CSSPrimitiveValue::Un
itType::Percentage); |
27 return new CSSSimpleLength(value.getDoubleValue(), value.typeWithCalcResolve
d()); | 29 return new CSSSimpleLength(value.getDoubleValue(), value.typeWithCalcResolve
d()); |
28 } | 30 } |
29 | 31 |
30 bool CSSSimpleLength::containsPercent() const | 32 bool CSSSimpleLength::containsPercent() const |
31 { | 33 { |
32 return lengthUnit() == CSSPrimitiveValue::UnitType::Percentage; | 34 return lengthUnit() == CSSPrimitiveValue::UnitType::Percentage; |
33 } | 35 } |
34 | 36 |
| 37 String CSSSimpleLength::unit() const |
| 38 { |
| 39 if (lengthUnit() == CSSPrimitiveValue::UnitType::Percentage) |
| 40 return "percent"; |
| 41 return CSSPrimitiveValue::unitTypeToString(m_unit); |
| 42 } |
| 43 |
35 CSSLengthValue* CSSSimpleLength::addInternal(const CSSLengthValue* other) | 44 CSSLengthValue* CSSSimpleLength::addInternal(const CSSLengthValue* other) |
36 { | 45 { |
37 const CSSSimpleLength* o = toCSSSimpleLength(other); | 46 const CSSSimpleLength* o = toCSSSimpleLength(other); |
38 DCHECK_EQ(m_unit, o->m_unit); | 47 DCHECK_EQ(m_unit, o->m_unit); |
39 return create(m_value + o->value(), m_unit); | 48 return create(m_value + o->value(), m_unit); |
40 } | 49 } |
41 | 50 |
42 CSSLengthValue* CSSSimpleLength::subtractInternal(const CSSLengthValue* other) | 51 CSSLengthValue* CSSSimpleLength::subtractInternal(const CSSLengthValue* other) |
43 { | 52 { |
44 const CSSSimpleLength* o = toCSSSimpleLength(other); | 53 const CSSSimpleLength* o = toCSSSimpleLength(other); |
(...skipping 11 matching lines...) Expand all Loading... |
56 DCHECK_NE(x, 0); | 65 DCHECK_NE(x, 0); |
57 return create(m_value / x, m_unit); | 66 return create(m_value / x, m_unit); |
58 } | 67 } |
59 | 68 |
60 CSSValue* CSSSimpleLength::toCSSValue() const | 69 CSSValue* CSSSimpleLength::toCSSValue() const |
61 { | 70 { |
62 return CSSPrimitiveValue::create(m_value, m_unit); | 71 return CSSPrimitiveValue::create(m_value, m_unit); |
63 } | 72 } |
64 | 73 |
65 } // namespace blink | 74 } // namespace blink |
OLD | NEW |