OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/css/cssom/CSSSimpleLength.h" | |
6 | |
7 #include "bindings/core/v8/ExceptionState.h" | |
8 #include "core/css/CSSPrimitiveValue.h" | |
9 #include "core/css/cssom/CSSCalcLength.h" | |
10 #include "platform/wtf/text/StringBuilder.h" | |
11 | |
12 namespace blink { | |
13 | |
14 CSSSimpleLength* CSSSimpleLength::Create(double value, | |
15 const String& type, | |
16 ExceptionState& exception_state) { | |
17 CSSPrimitiveValue::UnitType unit = CSSLengthValue::UnitFromName(type); | |
18 if (!CSSLengthValue::IsSupportedLengthUnit(unit)) { | |
19 exception_state.ThrowTypeError("Invalid unit for CSSSimpleLength: " + type); | |
20 return nullptr; | |
21 } | |
22 return new CSSSimpleLength(value, unit); | |
23 } | |
24 | |
25 CSSSimpleLength* CSSSimpleLength::FromCSSValue(const CSSPrimitiveValue& value) { | |
26 DCHECK(value.IsLength() || value.IsPercentage()); | |
27 if (value.IsPercentage()) | |
28 return new CSSSimpleLength(value.GetDoubleValue(), | |
29 CSSPrimitiveValue::UnitType::kPercentage); | |
30 return new CSSSimpleLength(value.GetDoubleValue(), | |
31 value.TypeWithCalcResolved()); | |
32 } | |
33 | |
34 bool CSSSimpleLength::ContainsPercent() const { | |
35 return LengthUnit() == CSSPrimitiveValue::UnitType::kPercentage; | |
36 } | |
37 | |
38 String CSSSimpleLength::unit() const { | |
39 if (LengthUnit() == CSSPrimitiveValue::UnitType::kPercentage) | |
40 return "percent"; | |
41 return CSSPrimitiveValue::UnitTypeToString(unit_); | |
42 } | |
43 | |
44 CSSLengthValue* CSSSimpleLength::AddInternal(const CSSLengthValue* other) { | |
45 const CSSSimpleLength* o = ToCSSSimpleLength(other); | |
46 DCHECK_EQ(unit_, o->unit_); | |
47 return Create(value_ + o->value(), unit_); | |
48 } | |
49 | |
50 CSSLengthValue* CSSSimpleLength::SubtractInternal(const CSSLengthValue* other) { | |
51 const CSSSimpleLength* o = ToCSSSimpleLength(other); | |
52 DCHECK_EQ(unit_, o->unit_); | |
53 return Create(value_ - o->value(), unit_); | |
54 } | |
55 | |
56 CSSLengthValue* CSSSimpleLength::MultiplyInternal(double x) { | |
57 return Create(value_ * x, unit_); | |
58 } | |
59 | |
60 CSSLengthValue* CSSSimpleLength::DivideInternal(double x) { | |
61 DCHECK_NE(x, 0); | |
62 return Create(value_ / x, unit_); | |
63 } | |
64 | |
65 CSSValue* CSSSimpleLength::ToCSSValue() const { | |
66 return CSSPrimitiveValue::Create(value_, unit_); | |
67 } | |
68 | |
69 } // namespace blink | |
OLD | NEW |