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/SVGEnumeration.h" 34 35 #include "bindings/v8/ExceptionState.h" 36 #include "bindings/v8/ExceptionStatePlaceholder.h" 37 #include "core/dom/ExceptionCode.h" 38 #include "core/svg/SVGAnimationElement.h" 39 40 namespace WebCore { 41 42 inline PassRefPtr<SVGEnumerationBase> toSVGEnumerationBase(PassRefPtr<SVGPropertyBase> passBase) 43 { 44 RefPtr<SVGPropertyBase> base = passBase; 45 ASSERT(base->type() == SVGEnumerationBase::classType()); 46 return static_pointer_cast<SVGEnumerationBase>(base.release()); 47 } 48 49 SVGEnumerationBase::~SVGEnumerationBase() 50 { 51 } 52 53 PassRefPtr<SVGPropertyBase> SVGEnumerationBase::cloneForAnimation(const String& value) const 54 { 55 RefPtr<SVGEnumerationBase> svgEnumeration = clone(); 56 svgEnumeration->setValueAsString(value, IGNORE_EXCEPTION); 57 return svgEnumeration.release(); 58 } 59 60 String SVGEnumerationBase::valueAsString() const 61 { 62 StringEntries::const_iterator it = m_entries.begin(); 63 StringEntries::const_iterator itEnd = m_entries.end(); 64 for (; it != itEnd; ++it) { 65 if (m_value == it->first) 66 return it->second; 67 } 68 69 ASSERT(m_value < maxEnumValue()); 70 return emptyString(); 71 } 72 73 void SVGEnumerationBase::setValue(unsigned short value, ExceptionState& exceptionState) 74 { 75 if (!value) { 76 exceptionState.throwTypeError("The enumeration value provided is 0, which is not settable."); 77 return; 78 } 79 80 if (value > maxEnumValue()) { 81 exceptionState.throwTypeError("The enumeration value provided (" + String::number(value) + ") is larger than the largest allowed value (" + String::number(maxEnumValue()) + ")."); 82 return; 83 } 84 85 m_value = value; 86 notifyChange(); 87 } 88 89 void SVGEnumerationBase::setValueAsString(const String& string, ExceptionState& exceptionState) 90 { 91 StringEntries::const_iterator it = m_entries.begin(); 92 StringEntries::const_iterator itEnd = m_entries.end(); 93 for (; it != itEnd; ++it) { 94 if (string == it->second) { 95 // 0 corresponds to _UNKNOWN enumeration values, and should not be settable. 96 ASSERT(it->first); 97 m_value = it->first; 98 notifyChange(); 99 return; 100 } 101 } 102 103 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid."); 104 notifyChange(); 105 } 106 107 void SVGEnumerationBase::add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) 108 { 109 ASSERT_NOT_REACHED(); 110 } 111 112 void SVGEnumerationBase::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase>, SVGElement*) 113 { 114 ASSERT(animationElement); 115 unsigned short fromEnumeration = animationElement->animationMode() == ToAnimation ? m_value : toSVGEnumerationBase(from)->value(); 116 unsigned short toEnumeration = toSVGEnumerationBase(to)->value(); 117 118 animationElement->animateDiscreteType<unsigned short>(percentage, fromEnumeration, toEnumeration, m_value); 119 } 120 121 float SVGEnumerationBase::calculateDistance(PassRefPtr<SVGPropertyBase>, SVGElement*) 122 { 123 // No paced animations for boolean. 124 return -1; 125 } 126 127 } 128