| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008, 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 #include "core/css/CSSKeyframeRule.h" | 27 #include "core/css/CSSKeyframeRule.h" |
| 28 | 28 |
| 29 #include "core/css/CSSKeyframesRule.h" | 29 #include "core/css/CSSKeyframesRule.h" |
| 30 #include "core/css/CSSParser.h" |
| 30 #include "core/css/PropertySetCSSStyleDeclaration.h" | 31 #include "core/css/PropertySetCSSStyleDeclaration.h" |
| 31 #include "core/css/StylePropertySet.h" | 32 #include "core/css/StylePropertySet.h" |
| 32 #include "wtf/text/StringBuilder.h" | 33 #include "wtf/text/StringBuilder.h" |
| 33 | 34 |
| 34 namespace WebCore { | 35 namespace WebCore { |
| 35 | 36 |
| 36 StyleKeyframe::StyleKeyframe() | 37 StyleKeyframe::StyleKeyframe() |
| 37 { | 38 { |
| 38 } | 39 } |
| 39 | 40 |
| 40 StyleKeyframe::~StyleKeyframe() | 41 StyleKeyframe::~StyleKeyframe() |
| 41 { | 42 { |
| 42 } | 43 } |
| 43 | 44 |
| 45 String StyleKeyframe::keyText() const |
| 46 { |
| 47 if (m_keyText.isNull()) { |
| 48 // Keys are always set when these objects are created. |
| 49 ASSERT(m_keys && !m_keys->isEmpty()); |
| 50 StringBuilder keyText; |
| 51 for (unsigned i = 0; i < m_keys->size(); ++i) { |
| 52 if (i) |
| 53 keyText.append(','); |
| 54 keyText.append(String::number(m_keys->at(i) * 100)); |
| 55 keyText.append('%'); |
| 56 } |
| 57 m_keyText = keyText.toString(); |
| 58 } |
| 59 ASSERT(!m_keyText.isNull()); |
| 60 return m_keyText; |
| 61 } |
| 62 |
| 63 void StyleKeyframe::setKeyText(const String& keyText) |
| 64 { |
| 65 // FIXME: Should we trim whitespace? |
| 66 // FIXME: Should we leave keyText unchanged when attempting to set to an |
| 67 // invalid string? |
| 68 ASSERT(!keyText.isNull()); |
| 69 m_keyText = keyText; |
| 70 m_keys.clear(); |
| 71 } |
| 72 |
| 73 const Vector<double>& StyleKeyframe::keys() const |
| 74 { |
| 75 if (!m_keys) { |
| 76 // Keys can only be cleared by setting the key text from JavaScript |
| 77 // and this can never be null. |
| 78 ASSERT(!m_keyText.isNull()); |
| 79 m_keys = CSSParser(CSSStrictMode).parseKeyframeKeyList(m_keyText); |
| 80 } |
| 81 // If an invalid key string was set, m_keys may be empty. |
| 82 ASSERT(m_keys); |
| 83 return *m_keys; |
| 84 } |
| 85 |
| 86 void StyleKeyframe::setKeys(PassOwnPtr<Vector<double> > keys) |
| 87 { |
| 88 ASSERT(keys && !keys->isEmpty()); |
| 89 m_keys = keys; |
| 90 m_keyText = String(); |
| 91 ASSERT(m_keyText.isNull()); |
| 92 } |
| 93 |
| 44 MutableStylePropertySet* StyleKeyframe::mutableProperties() | 94 MutableStylePropertySet* StyleKeyframe::mutableProperties() |
| 45 { | 95 { |
| 46 if (!m_properties->isMutable()) | 96 if (!m_properties->isMutable()) |
| 47 m_properties = m_properties->mutableCopy(); | 97 m_properties = m_properties->mutableCopy(); |
| 48 return static_cast<MutableStylePropertySet*>(m_properties.get()); | 98 return static_cast<MutableStylePropertySet*>(m_properties.get()); |
| 49 } | 99 } |
| 50 | 100 |
| 51 void StyleKeyframe::setProperties(PassRefPtr<StylePropertySet> properties) | 101 void StyleKeyframe::setProperties(PassRefPtr<StylePropertySet> properties) |
| 52 { | 102 { |
| 53 m_properties = properties; | 103 m_properties = properties; |
| 54 } | 104 } |
| 55 | 105 |
| 56 /* static */ | |
| 57 void StyleKeyframe::parseKeyString(const String& s, Vector<double>& keys) | |
| 58 { | |
| 59 keys.clear(); | |
| 60 Vector<String> strings; | |
| 61 s.split(',', strings); | |
| 62 | |
| 63 for (size_t i = 0; i < strings.size(); ++i) { | |
| 64 double key = -1; | |
| 65 String cur = strings[i].stripWhiteSpace(); | |
| 66 | |
| 67 // For now the syntax MUST be 'xxx%' or 'from' or 'to', where xxx is a l
egal floating point number | |
| 68 if (cur == "from") | |
| 69 key = 0; | |
| 70 else if (cur == "to") | |
| 71 key = 1; | |
| 72 else if (cur.endsWith('%')) { | |
| 73 double k = cur.substring(0, cur.length() - 1).toDouble(); | |
| 74 if (k >= 0 && k <= 100) | |
| 75 key = k / 100; | |
| 76 } | |
| 77 if (key < 0) { | |
| 78 keys.clear(); | |
| 79 return; | |
| 80 } | |
| 81 keys.append(key); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 String StyleKeyframe::cssText() const | 106 String StyleKeyframe::cssText() const |
| 86 { | 107 { |
| 87 StringBuilder result; | 108 StringBuilder result; |
| 88 result.append(keyText()); | 109 result.append(keyText()); |
| 89 result.appendLiteral(" { "); | 110 result.appendLiteral(" { "); |
| 90 String decls = m_properties->asText(); | 111 String decls = m_properties->asText(); |
| 91 result.append(decls); | 112 result.append(decls); |
| 92 if (!decls.isEmpty()) | 113 if (!decls.isEmpty()) |
| 93 result.append(' '); | 114 result.append(' '); |
| 94 result.append('}'); | 115 result.append('}'); |
| 95 return result.toString(); | 116 return result.toString(); |
| 96 } | 117 } |
| 97 | 118 |
| 119 PassOwnPtr<Vector<double> > StyleKeyframe::createKeyList(CSSParserValueList* key
s) |
| 120 { |
| 121 OwnPtr<Vector<double> > keyVector = adoptPtr(new Vector<double>(keys->size()
)); |
| 122 for (unsigned i = 0; i < keys->size(); ++i) { |
| 123 ASSERT(keys->valueAt(i)->unit == WebCore::CSSPrimitiveValue::CSS_NUMBER)
; |
| 124 double key = keys->valueAt(i)->fValue; |
| 125 if (key < 0 || key > 100) { |
| 126 // As per http://www.w3.org/TR/css3-animations/#keyframes, |
| 127 // "If a keyframe selector specifies negative percentage values |
| 128 // or values higher than 100%, then the keyframe will be ignored." |
| 129 keyVector->clear(); |
| 130 break; |
| 131 } |
| 132 keyVector->at(i) = key / 100; |
| 133 } |
| 134 return keyVector.release(); |
| 135 } |
| 136 |
| 137 |
| 98 CSSKeyframeRule::CSSKeyframeRule(StyleKeyframe* keyframe, CSSKeyframesRule* pare
nt) | 138 CSSKeyframeRule::CSSKeyframeRule(StyleKeyframe* keyframe, CSSKeyframesRule* pare
nt) |
| 99 : CSSRule(0) | 139 : CSSRule(0) |
| 100 , m_keyframe(keyframe) | 140 , m_keyframe(keyframe) |
| 101 { | 141 { |
| 102 setParentRule(parent); | 142 setParentRule(parent); |
| 103 } | 143 } |
| 104 | 144 |
| 105 CSSKeyframeRule::~CSSKeyframeRule() | 145 CSSKeyframeRule::~CSSKeyframeRule() |
| 106 { | 146 { |
| 107 if (m_propertiesCSSOMWrapper) | 147 if (m_propertiesCSSOMWrapper) |
| 108 m_propertiesCSSOMWrapper->clearParentRule(); | 148 m_propertiesCSSOMWrapper->clearParentRule(); |
| 109 } | 149 } |
| 110 | 150 |
| 111 CSSStyleDeclaration* CSSKeyframeRule::style() const | 151 CSSStyleDeclaration* CSSKeyframeRule::style() const |
| 112 { | 152 { |
| 113 if (!m_propertiesCSSOMWrapper) | 153 if (!m_propertiesCSSOMWrapper) |
| 114 m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_keyfra
me->mutableProperties(), const_cast<CSSKeyframeRule*>(this)); | 154 m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_keyfra
me->mutableProperties(), const_cast<CSSKeyframeRule*>(this)); |
| 115 return m_propertiesCSSOMWrapper.get(); | 155 return m_propertiesCSSOMWrapper.get(); |
| 116 } | 156 } |
| 117 | 157 |
| 118 void CSSKeyframeRule::reattach(StyleRuleBase*) | 158 void CSSKeyframeRule::reattach(StyleRuleBase*) |
| 119 { | 159 { |
| 120 // No need to reattach, the underlying data is shareable on mutation. | 160 // No need to reattach, the underlying data is shareable on mutation. |
| 121 ASSERT_NOT_REACHED(); | 161 ASSERT_NOT_REACHED(); |
| 122 } | 162 } |
| 123 | 163 |
| 124 } // namespace WebCore | 164 } // namespace WebCore |
| OLD | NEW |