Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGLengthTearOff.cpp
1 /*
2  * Copyright (C) 2014 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "core/svg/SVGLengthTearOff.h"
34
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "core/dom/ExceptionCode.h"
37
38 namespace blink {
39
40 namespace {
41
42 inline SVGLengthType toSVGLengthType(unsigned short type)
43 {
44     ASSERT(type >= LengthTypeUnknown && type <= LengthTypePC);
45     return static_cast<SVGLengthType>(type);
46 }
47
48 } // namespace
49
50 SVGLengthType SVGLengthTearOff::unitType()
51 {
52     return target()->unitType();
53 }
54
55 SVGLengthMode SVGLengthTearOff::unitMode()
56 {
57     return target()->unitMode();
58 }
59
60 float SVGLengthTearOff::value(ExceptionState& es)
61 {
62     SVGLengthContext lengthContext(contextElement());
63     return target()->value(lengthContext, es);
64 }
65
66 void SVGLengthTearOff::setValue(float value, ExceptionState& es)
67 {
68     if (isImmutable()) {
69         es.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
70         return;
71     }
72
73     SVGLengthContext lengthContext(contextElement());
74     target()->setValue(value, lengthContext, es);
75     commitChange();
76 }
77
78 float SVGLengthTearOff::valueInSpecifiedUnits()
79 {
80     return target()->valueInSpecifiedUnits();
81 }
82
83 void SVGLengthTearOff::setValueInSpecifiedUnits(float value, ExceptionState& es)
84 {
85     if (isImmutable()) {
86         es.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
87         return;
88     }
89     target()->setValueInSpecifiedUnits(value);
90     commitChange();
91 }
92
93 String SVGLengthTearOff::valueAsString()
94 {
95     return target()->valueAsString();
96 }
97
98 void SVGLengthTearOff::setValueAsString(const String& str, ExceptionState& es)
99 {
100     if (isImmutable()) {
101         es.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
102         return;
103     }
104
105     target()->setValueAsString(str, es);
106     commitChange();
107 }
108
109 void SVGLengthTearOff::newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits, ExceptionState& exceptionState)
110 {
111     if (isImmutable()) {
112         exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
113         return;
114     }
115
116     if (unitType == LengthTypeUnknown || unitType > LengthTypePC) {
117         exceptionState.throwDOMException(NotSupportedError, "Cannot set value with unknown or invalid units (" + String::number(unitType) + ").");
118         return;
119     }
120
121     target()->newValueSpecifiedUnits(toSVGLengthType(unitType), valueInSpecifiedUnits);
122     commitChange();
123 }
124
125 void SVGLengthTearOff::convertToSpecifiedUnits(unsigned short unitType, ExceptionState& exceptionState)
126 {
127     if (isImmutable()) {
128         exceptionState.throwDOMException(NoModificationAllowedError, "The object is read-only.");
129         return;
130     }
131
132     if (unitType == LengthTypeUnknown || unitType > LengthTypePC) {
133         exceptionState.throwDOMException(NotSupportedError, "Cannot convert to unknown or invalid units (" + String::number(unitType) + ").");
134         return;
135     }
136
137     SVGLengthContext lengthContext(contextElement());
138     target()->convertToSpecifiedUnits(toSVGLengthType(unitType), lengthContext, exceptionState);
139     commitChange();
140 }
141
142 SVGLengthTearOff::SVGLengthTearOff(PassRefPtr<SVGLength> target, SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedName& attributeName)
143     : SVGPropertyTearOff<SVGLength>(target, contextElement, propertyIsAnimVal, attributeName)
144 {
145 }
146
147 }