2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebKitCSSKeyframesRule.h"
29 #include "CSSParser.h"
30 #include "CSSRuleList.h"
31 #include "CSSStyleSheet.h"
32 #include "StylePropertySet.h"
33 #include "StyleSheet.h"
34 #include "WebKitCSSKeyframeRule.h"
35 #include <wtf/text/StringBuilder.h>
39 StyleRuleKeyframes::StyleRuleKeyframes()
40 : StyleRuleBase(Keyframes, 0)
44 StyleRuleKeyframes::StyleRuleKeyframes(const StyleRuleKeyframes& o)
46 , m_keyframes(o.m_keyframes)
51 StyleRuleKeyframes::~StyleRuleKeyframes()
55 void StyleRuleKeyframes::parserAppendKeyframe(PassRefPtr<StyleKeyframe> keyframe)
59 m_keyframes.append(keyframe);
62 void StyleRuleKeyframes::wrapperAppendKeyframe(PassRefPtr<StyleKeyframe> keyframe)
64 m_keyframes.append(keyframe);
67 void StyleRuleKeyframes::wrapperRemoveKeyframe(unsigned index)
69 m_keyframes.remove(index);
72 int StyleRuleKeyframes::findKeyframeIndex(const String& key) const
74 String percentageString;
75 if (equalIgnoringCase(key, "from"))
76 percentageString = "0%";
77 else if (equalIgnoringCase(key, "to"))
78 percentageString = "100%";
80 percentageString = key;
82 for (unsigned i = 0; i < m_keyframes.size(); ++i) {
83 if (m_keyframes[i]->keyText() == percentageString)
89 WebKitCSSKeyframesRule::WebKitCSSKeyframesRule(StyleRuleKeyframes* keyframesRule, CSSStyleSheet* parent)
90 : CSSRule(parent, CSSRule::WEBKIT_KEYFRAMES_RULE)
91 , m_keyframesRule(keyframesRule)
92 , m_childRuleCSSOMWrappers(keyframesRule->keyframes().size())
96 WebKitCSSKeyframesRule::~WebKitCSSKeyframesRule()
98 ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
100 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
101 if (m_childRuleCSSOMWrappers[i])
102 m_childRuleCSSOMWrappers[i]->setParentRule(0);
106 void WebKitCSSKeyframesRule::setName(const String& name)
108 CSSStyleSheet::RuleMutationScope mutationScope(this);
110 m_keyframesRule->setName(name);
113 void WebKitCSSKeyframesRule::insertRule(const String& ruleText)
115 ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
117 CSSParser parser(parserContext());
118 CSSStyleSheet* styleSheet = parentStyleSheet();
119 RefPtr<StyleKeyframe> keyframe = parser.parseKeyframeRule(styleSheet ? styleSheet->contents() : 0, ruleText);
123 CSSStyleSheet::RuleMutationScope mutationScope(this);
125 m_keyframesRule->wrapperAppendKeyframe(keyframe);
127 m_childRuleCSSOMWrappers.grow(length());
130 void WebKitCSSKeyframesRule::deleteRule(const String& s)
132 ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
134 int i = m_keyframesRule->findKeyframeIndex(s);
138 CSSStyleSheet::RuleMutationScope mutationScope(this);
140 m_keyframesRule->wrapperRemoveKeyframe(i);
142 if (m_childRuleCSSOMWrappers[i])
143 m_childRuleCSSOMWrappers[i]->setParentRule(0);
144 m_childRuleCSSOMWrappers.remove(i);
147 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::findRule(const String& s)
149 int i = m_keyframesRule->findKeyframeIndex(s);
150 return (i >= 0) ? item(i) : 0;
153 String WebKitCSSKeyframesRule::cssText() const
155 StringBuilder result;
156 result.append("@-webkit-keyframes ");
157 result.append(name());
158 result.append(" { \n");
160 unsigned size = length();
161 for (unsigned i = 0; i < size; ++i) {
163 result.append(m_keyframesRule->keyframes()[i]->cssText());
167 return result.toString();
170 unsigned WebKitCSSKeyframesRule::length() const
172 return m_keyframesRule->keyframes().size();
175 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index) const
177 if (index >= length())
180 ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
181 RefPtr<WebKitCSSKeyframeRule>& rule = m_childRuleCSSOMWrappers[index];
183 rule = adoptRef(new WebKitCSSKeyframeRule(m_keyframesRule->keyframes()[index].get(), const_cast<WebKitCSSKeyframesRule*>(this)));
188 CSSRuleList* WebKitCSSKeyframesRule::cssRules()
190 if (!m_ruleListCSSOMWrapper)
191 m_ruleListCSSOMWrapper = adoptPtr(new LiveCSSRuleList<WebKitCSSKeyframesRule>(this));
192 return m_ruleListCSSOMWrapper.get();
195 void WebKitCSSKeyframesRule::reattach(StyleRuleKeyframes* rule)
198 m_keyframesRule = rule;
201 } // namespace WebCore