1/*
2 * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "SVGLengthValue.h"
29#include "SVGValueProperty.h"
30
31namespace WebCore {
32
33class SVGLength : public SVGValueProperty<SVGLengthValue> {
34 using Base = SVGValueProperty<SVGLengthValue>;
35 using Base::Base;
36 using Base::m_value;
37
38public:
39 // Forward declare these enums in the w3c naming scheme, for IDL generation
40 enum {
41 SVG_LENGTHTYPE_UNKNOWN = LengthTypeUnknown,
42 SVG_LENGTHTYPE_NUMBER = LengthTypeNumber,
43 SVG_LENGTHTYPE_PERCENTAGE = LengthTypePercentage,
44 SVG_LENGTHTYPE_EMS = LengthTypeEMS,
45 SVG_LENGTHTYPE_EXS = LengthTypeEXS,
46 SVG_LENGTHTYPE_PX = LengthTypePX,
47 SVG_LENGTHTYPE_CM = LengthTypeCM,
48 SVG_LENGTHTYPE_MM = LengthTypeMM,
49 SVG_LENGTHTYPE_IN = LengthTypeIN,
50 SVG_LENGTHTYPE_PT = LengthTypePT,
51 SVG_LENGTHTYPE_PC = LengthTypePC
52 };
53
54 static Ref<SVGLength> create()
55 {
56 return adoptRef(*new SVGLength());
57 }
58
59 static Ref<SVGLength> create(const SVGLengthValue& value)
60 {
61 return adoptRef(*new SVGLength(value));
62 }
63
64 static Ref<SVGLength> create(SVGPropertyOwner* owner, SVGPropertyAccess access, const SVGLengthValue& value = { })
65 {
66 return adoptRef(*new SVGLength(owner, access, value));
67 }
68
69 template<typename T>
70 static ExceptionOr<Ref<SVGLength>> create(ExceptionOr<T>&& value)
71 {
72 if (value.hasException())
73 return value.releaseException();
74 return adoptRef(*new SVGLength(value.releaseReturnValue()));
75 }
76
77 Ref<SVGLength> clone() const
78 {
79 return SVGLength::create(m_value);
80 }
81
82 unsigned short unitType()
83 {
84 return m_value.unitType();
85 }
86
87 ExceptionOr<float> valueForBindings()
88 {
89 return m_value.valueForBindings(SVGLengthContext { contextElement() });
90 }
91
92 ExceptionOr<void> setValueForBindings(float value)
93 {
94 if (isReadOnly())
95 return Exception { NoModificationAllowedError };
96
97 auto result = m_value.setValue(value, SVGLengthContext { contextElement() });
98 if (result.hasException())
99 return result;
100
101 commitChange();
102 return result;
103 }
104
105 float valueInSpecifiedUnits()
106 {
107 return m_value.valueInSpecifiedUnits();
108 }
109
110 ExceptionOr<void> setValueInSpecifiedUnits(float valueInSpecifiedUnits)
111 {
112 if (isReadOnly())
113 return Exception { NoModificationAllowedError };
114
115 m_value.setValueInSpecifiedUnits(valueInSpecifiedUnits);
116 commitChange();
117 return { };
118 }
119
120 ExceptionOr<void> setValueAsString(const String& value)
121 {
122 if (isReadOnly())
123 return Exception { NoModificationAllowedError };
124
125 auto result = m_value.setValueAsString(value);
126 if (result.hasException())
127 return result;
128
129 commitChange();
130 return result;
131 }
132
133 ExceptionOr<void> newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits)
134 {
135 if (isReadOnly())
136 return Exception { NoModificationAllowedError };
137
138 auto result = m_value.newValueSpecifiedUnits(unitType, valueInSpecifiedUnits);
139 if (result.hasException())
140 return result;
141
142 commitChange();
143 return result;
144 }
145
146 ExceptionOr<void> convertToSpecifiedUnits(unsigned short unitType)
147 {
148 if (isReadOnly())
149 return Exception { NoModificationAllowedError };
150
151 auto result = m_value.convertToSpecifiedUnits(unitType, SVGLengthContext { contextElement() });
152 if (result.hasException())
153 return result;
154
155 commitChange();
156 return result;
157 }
158
159 String valueAsString() const override
160 {
161 return m_value.valueAsString();
162 }
163};
164
165} // namespace WebCore
166