1d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
2d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Copyright (C) 2014 Google Inc. All rights reserved.
3d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
4d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * modification, are permitted provided that the following conditions are
6d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * met:
7d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
8d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * in the documentation and/or other materials provided with the
13d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * distribution.
14d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
16d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * this software without specific prior written permission.
17d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
18d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
30d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
31d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "config.h"
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/svg/SVGEnumeration.h"
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ExceptionState.h"
36197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ExceptionStatePlaceholder.h"
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/dom/ExceptionCode.h"
38d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/svg/SVGAnimationElement.h"
39d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
4207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochinline PassRefPtr<SVGEnumerationBase> toSVGEnumerationBase(PassRefPtr<SVGPropertyBase> passBase)
43d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
4407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    RefPtr<SVGPropertyBase> base = passBase;
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(base->type() == SVGEnumerationBase::classType());
46d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return static_pointer_cast<SVGEnumerationBase>(base.release());
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)SVGEnumerationBase::~SVGEnumerationBase()
50d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
51d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
52d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
5307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochPassRefPtr<SVGPropertyBase> SVGEnumerationBase::cloneForAnimation(const String& value) const
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    RefPtr<SVGEnumerationBase> svgEnumeration = clone();
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    svgEnumeration->setValueAsString(value, IGNORE_EXCEPTION);
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return svgEnumeration.release();
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
59d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)String SVGEnumerationBase::valueAsString() const
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
62d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    StringEntries::const_iterator it = m_entries.begin();
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    StringEntries::const_iterator itEnd = m_entries.end();
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    for (; it != itEnd; ++it) {
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (m_value == it->first)
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return it->second;
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
68d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
69e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    ASSERT(m_value < maxInternalEnumValue());
70a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    return emptyString();
71d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
72d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
73d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void SVGEnumerationBase::setValue(unsigned short value, ExceptionState& exceptionState)
74d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
75d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (!value) {
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        exceptionState.throwTypeError("The enumeration value provided is 0, which is not settable.");
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return;
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
79d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
80e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    if (value > maxExposedEnumValue()) {
81e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        exceptionState.throwTypeError("The enumeration value provided (" + String::number(value) + ") is larger than the largest allowed value (" + String::number(maxExposedEnumValue()) + ").");
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return;
83d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_value = value;
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    notifyChange();
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
88d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
89d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void SVGEnumerationBase::setValueAsString(const String& string, ExceptionState& exceptionState)
90d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    StringEntries::const_iterator it = m_entries.begin();
92d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    StringEntries::const_iterator itEnd = m_entries.end();
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    for (; it != itEnd; ++it) {
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (string == it->second) {
95a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            // 0 corresponds to _UNKNOWN enumeration values, and should not be settable.
96a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            ASSERT(it->first);
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            m_value = it->first;
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            notifyChange();
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return;
100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        }
101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
103d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
104d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    notifyChange();
105d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
106d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
107d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void SVGEnumerationBase::add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*)
108d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT_NOT_REACHED();
110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
11207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochvoid SVGEnumerationBase::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase>, SVGElement*)
113d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
114d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(animationElement);
115d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    unsigned short fromEnumeration = animationElement->animationMode() == ToAnimation ? m_value : toSVGEnumerationBase(from)->value();
116d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    unsigned short toEnumeration = toSVGEnumerationBase(to)->value();
117d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
118d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    animationElement->animateDiscreteType<unsigned short>(percentage, fromEnumeration, toEnumeration, m_value);
119d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
120d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
12107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochfloat SVGEnumerationBase::calculateDistance(PassRefPtr<SVGPropertyBase>, SVGElement*)
122d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
123d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // No paced animations for boolean.
124d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return -1;
125d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
126d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
127d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
128