• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008, 2012 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
24  */
25 
26 #include "config.h"
27 #include "core/css/CSSKeyframeRule.h"
28 
29 #include "core/css/CSSKeyframesRule.h"
30 #include "core/css/CSSParser.h"
31 #include "core/css/PropertySetCSSStyleDeclaration.h"
32 #include "core/css/StylePropertySet.h"
33 #include "wtf/text/StringBuilder.h"
34 
35 namespace WebCore {
36 
StyleKeyframe()37 StyleKeyframe::StyleKeyframe()
38 {
39 }
40 
~StyleKeyframe()41 StyleKeyframe::~StyleKeyframe()
42 {
43 }
44 
keyText() const45 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 
setKeyText(const String & keyText)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 
keys() const73 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(HTMLStandardMode).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 
setKeys(PassOwnPtr<Vector<double>> keys)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 
mutableProperties()94 MutableStylePropertySet* StyleKeyframe::mutableProperties()
95 {
96     if (!m_properties->isMutable())
97         m_properties = m_properties->mutableCopy();
98     return toMutableStylePropertySet(m_properties);
99 }
100 
setProperties(PassRefPtr<StylePropertySet> properties)101 void StyleKeyframe::setProperties(PassRefPtr<StylePropertySet> properties)
102 {
103     m_properties = properties;
104 }
105 
cssText() const106 String StyleKeyframe::cssText() const
107 {
108     StringBuilder result;
109     result.append(keyText());
110     result.appendLiteral(" { ");
111     String decls = m_properties->asText();
112     result.append(decls);
113     if (!decls.isEmpty())
114         result.append(' ');
115     result.append('}');
116     return result.toString();
117 }
118 
createKeyList(CSSParserValueList * keys)119 PassOwnPtr<Vector<double> > StyleKeyframe::createKeyList(CSSParserValueList* keys)
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 
CSSKeyframeRule(StyleKeyframe * keyframe,CSSKeyframesRule * parent)138 CSSKeyframeRule::CSSKeyframeRule(StyleKeyframe* keyframe, CSSKeyframesRule* parent)
139     : CSSRule(0)
140     , m_keyframe(keyframe)
141 {
142     setParentRule(parent);
143 }
144 
~CSSKeyframeRule()145 CSSKeyframeRule::~CSSKeyframeRule()
146 {
147     if (m_propertiesCSSOMWrapper)
148         m_propertiesCSSOMWrapper->clearParentRule();
149 }
150 
style() const151 CSSStyleDeclaration* CSSKeyframeRule::style() const
152 {
153     if (!m_propertiesCSSOMWrapper)
154         m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_keyframe->mutableProperties(), const_cast<CSSKeyframeRule*>(this));
155     return m_propertiesCSSOMWrapper.get();
156 }
157 
reattach(StyleRuleBase *)158 void CSSKeyframeRule::reattach(StyleRuleBase*)
159 {
160     // No need to reattach, the underlying data is shareable on mutation.
161     ASSERT_NOT_REACHED();
162 }
163 
164 } // namespace WebCore
165