1/*
2    Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "JSSVGLength.h"
22
23#include "ExceptionCode.h"
24#include "JSDOMBinding.h"
25#include "SVGAnimatedProperty.h"
26#include "SVGException.h"
27#include "SVGLengthContext.h"
28#include <runtime/Error.h>
29
30using namespace JSC;
31
32namespace WebCore {
33
34JSValue JSSVGLength::value(ExecState* exec) const
35{
36    SVGLength& podImp = impl().propertyReference();
37    ExceptionCode ec = 0;
38    SVGLengthContext lengthContext(impl().contextElement());
39    float value = podImp.value(lengthContext, ec);
40    if (ec) {
41        setDOMException(exec, ec);
42        return jsUndefined();
43    }
44
45    return jsNumber(value);
46}
47
48void JSSVGLength::setValue(ExecState* exec, JSValue value)
49{
50    if (impl().isReadOnly()) {
51        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);
52        return;
53    }
54
55    if (!value.isUndefinedOrNull() && !value.isNumber() && !value.isBoolean()) {
56        throwVMTypeError(exec);
57        return;
58    }
59
60    SVGLength& podImp = impl().propertyReference();
61
62    ExceptionCode ec = 0;
63    SVGLengthContext lengthContext(impl().contextElement());
64    podImp.setValue(value.toFloat(exec), lengthContext, ec);
65    if (ec) {
66        setDOMException(exec, ec);
67        return;
68    }
69
70    impl().commitChange();
71}
72
73JSValue JSSVGLength::convertToSpecifiedUnits(ExecState* exec)
74{
75    if (impl().isReadOnly()) {
76        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);
77        return jsUndefined();
78    }
79
80    SVGLength& podImp = impl().propertyReference();
81
82    if (exec->argumentCount() < 1)
83        return exec->vm().throwException(exec, createNotEnoughArgumentsError(exec));
84
85    unsigned short unitType = exec->uncheckedArgument(0).toUInt32(exec);
86    if (exec->hadException())
87        return jsUndefined();
88
89    ExceptionCode ec = 0;
90    SVGLengthContext lengthContext(impl().contextElement());
91    podImp.convertToSpecifiedUnits(unitType, lengthContext, ec);
92    if (ec) {
93        setDOMException(exec, ec);
94        return jsUndefined();
95    }
96
97    impl().commitChange();
98    return jsUndefined();
99}
100
101}
102