1 /*
2 * Copyright (C) 2007, 2008 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
28 #include "CSSParser.h"
29 #include "WebKitCSSKeyframesRule.h"
30 #include "WebKitCSSKeyframeRule.h"
31 #include "CSSRuleList.h"
32 #include "StyleSheet.h"
33
34 namespace WebCore {
35
WebKitCSSKeyframesRule(CSSStyleSheet * parent)36 WebKitCSSKeyframesRule::WebKitCSSKeyframesRule(CSSStyleSheet* parent)
37 : CSSRule(parent)
38 , m_lstCSSRules(CSSRuleList::create())
39 {
40 }
41
~WebKitCSSKeyframesRule()42 WebKitCSSKeyframesRule::~WebKitCSSKeyframesRule()
43 {
44 int length = m_lstCSSRules->length();
45 if (length == 0)
46 return;
47
48 for (int i = 0; i < length; i++)
49 m_lstCSSRules->item(i)->setParent(0);
50 }
51
name() const52 String WebKitCSSKeyframesRule::name() const
53 {
54 return m_name;
55 }
56
setName(const String & name)57 void WebKitCSSKeyframesRule::setName(const String& name)
58 {
59 m_name = name;
60
61 // Since the name is used in the keyframe map list in CSSStyleSelector, we need
62 // to recompute the style sheet to get the updated name.
63 stylesheet()->styleSheetChanged();
64 }
65
length() const66 unsigned WebKitCSSKeyframesRule::length() const
67 {
68 return m_lstCSSRules.get()->length();
69 }
70
item(unsigned index)71 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index)
72 {
73 CSSRule* rule = m_lstCSSRules.get()->item(index);
74 return (rule && rule->isKeyframeRule()) ? static_cast<WebKitCSSKeyframeRule*>(rule) : 0;
75 }
76
item(unsigned index) const77 const WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index) const
78 {
79 CSSRule* rule = m_lstCSSRules.get()->item(index);
80 return (rule && rule->isKeyframeRule()) ? static_cast<const WebKitCSSKeyframeRule*>(rule) : 0;
81 }
82
append(WebKitCSSKeyframeRule * rule)83 void WebKitCSSKeyframesRule::append(WebKitCSSKeyframeRule* rule)
84 {
85 m_lstCSSRules.get()->append(rule);
86 }
87
insertRule(const String & rule)88 void WebKitCSSKeyframesRule::insertRule(const String& rule)
89 {
90 CSSParser p(useStrictParsing());
91 RefPtr<CSSRule> newRule = p.parseKeyframeRule(parentStyleSheet(), rule);
92 if (newRule.get() && newRule.get()->isKeyframeRule())
93 append(static_cast<WebKitCSSKeyframeRule*>(newRule.get()));
94 }
95
deleteRule(const String & s)96 void WebKitCSSKeyframesRule::deleteRule(const String& s)
97 {
98 int i = findRuleIndex(s);
99 if (i >= 0)
100 m_lstCSSRules.get()->deleteRule(i);
101 }
102
findRule(const String & s)103 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::findRule(const String& s)
104 {
105 int i = findRuleIndex(s);
106 return (i >= 0) ? item(i) : 0;
107 }
108
findRuleIndex(const String & key) const109 int WebKitCSSKeyframesRule::findRuleIndex(const String& key) const
110 {
111 for (unsigned i = 0; i < length(); ++i) {
112 if (item(i)->keyText() == key)
113 return i;
114 }
115
116 return -1;
117 }
118
cssText() const119 String WebKitCSSKeyframesRule::cssText() const
120 {
121 String result = "@-webkit-keyframes ";
122 result += m_name;
123 result += " { \n";
124
125 if (m_lstCSSRules) {
126 unsigned len = m_lstCSSRules->length();
127 for (unsigned i = 0; i < len; i++) {
128 result += " ";
129 result += m_lstCSSRules->item(i)->cssText();
130 result += "\n";
131 }
132 }
133
134 result += "}";
135 return result;
136 }
137
138 } // namespace WebCore
139