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 "CSSMutableStyleDeclaration.h"
30 #include "CSSParser.h"
31 #include "CSSRuleList.h"
32 #include "StyleSheet.h"
33 #include "WebKitCSSKeyframeRule.h"
37 WebKitCSSKeyframesRule::WebKitCSSKeyframesRule(CSSStyleSheet* parent)
39 , m_lstCSSRules(CSSRuleList::create())
43 WebKitCSSKeyframesRule::~WebKitCSSKeyframesRule()
45 int length = m_lstCSSRules->length();
49 for (int i = 0; i < length; i++) {
50 if (m_lstCSSRules->item(i)->isKeyframeRule()) {
51 if (CSSMutableStyleDeclaration* style = static_cast<WebKitCSSKeyframeRule*>(m_lstCSSRules->item(i))->style())
54 m_lstCSSRules->item(i)->setParent(0);
58 String WebKitCSSKeyframesRule::name() const
63 void WebKitCSSKeyframesRule::setName(const String& name)
67 // Since the name is used in the keyframe map list in CSSStyleSelector, we need
68 // to recompute the style sheet to get the updated name.
70 stylesheet()->styleSheetChanged();
73 unsigned WebKitCSSKeyframesRule::length() const
75 return m_lstCSSRules.get()->length();
78 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index)
80 CSSRule* rule = m_lstCSSRules.get()->item(index);
81 return (rule && rule->isKeyframeRule()) ? static_cast<WebKitCSSKeyframeRule*>(rule) : 0;
84 const WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index) const
86 CSSRule* rule = m_lstCSSRules.get()->item(index);
87 return (rule && rule->isKeyframeRule()) ? static_cast<const WebKitCSSKeyframeRule*>(rule) : 0;
90 void WebKitCSSKeyframesRule::append(WebKitCSSKeyframeRule* rule)
95 m_lstCSSRules->append(rule);
96 rule->setParent(this);
98 if (CSSMutableStyleDeclaration* style = rule->style())
99 style->setParent(this);
102 void WebKitCSSKeyframesRule::insertRule(const String& rule)
104 CSSParser p(useStrictParsing());
105 RefPtr<CSSRule> newRule = p.parseKeyframeRule(parentStyleSheet(), rule);
106 if (newRule.get() && newRule.get()->isKeyframeRule())
107 append(static_cast<WebKitCSSKeyframeRule*>(newRule.get()));
110 void WebKitCSSKeyframesRule::deleteRule(const String& s)
112 int i = findRuleIndex(s);
114 m_lstCSSRules.get()->deleteRule(i);
117 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::findRule(const String& s)
119 int i = findRuleIndex(s);
120 return (i >= 0) ? item(i) : 0;
123 int WebKitCSSKeyframesRule::findRuleIndex(const String& key) const
125 String percentageString;
126 if (equalIgnoringCase(key, "from"))
127 percentageString = "0%";
128 else if (equalIgnoringCase(key, "to"))
129 percentageString = "100%";
131 percentageString = key;
133 for (unsigned i = 0; i < length(); ++i) {
134 if (item(i)->keyText() == percentageString)
141 String WebKitCSSKeyframesRule::cssText() const
143 String result = "@-webkit-keyframes ";
148 unsigned len = m_lstCSSRules->length();
149 for (unsigned i = 0; i < len; i++) {
151 result += m_lstCSSRules->item(i)->cssText();
160 } // namespace WebCore