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#include "WebKitCSSKeyframesRule.h"
28
29#include "CSSMutableStyleDeclaration.h"
30#include "CSSParser.h"
31#include "CSSRuleList.h"
32#include "StyleSheet.h"
33#include "WebKitCSSKeyframeRule.h"
34
35namespace WebCore {
36
37WebKitCSSKeyframesRule::WebKitCSSKeyframesRule(CSSStyleSheet* parent)
38    : CSSRule(parent)
39    , m_lstCSSRules(CSSRuleList::create())
40{
41}
42
43WebKitCSSKeyframesRule::~WebKitCSSKeyframesRule()
44{
45    int length = m_lstCSSRules->length();
46    if (length == 0)
47        return;
48
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())
52                style->setParent(0);
53        }
54        m_lstCSSRules->item(i)->setParent(0);
55    }
56}
57
58String WebKitCSSKeyframesRule::name() const
59{
60    return m_name;
61}
62
63void WebKitCSSKeyframesRule::setName(const String& name)
64{
65    m_name = name;
66
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.
69    stylesheet()->styleSheetChanged();
70}
71
72unsigned WebKitCSSKeyframesRule::length() const
73{
74    return m_lstCSSRules.get()->length();
75}
76
77WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index)
78{
79    CSSRule* rule = m_lstCSSRules.get()->item(index);
80    return (rule && rule->isKeyframeRule()) ? static_cast<WebKitCSSKeyframeRule*>(rule) : 0;
81}
82
83const WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index) const
84{
85    CSSRule* rule = m_lstCSSRules.get()->item(index);
86    return (rule && rule->isKeyframeRule()) ? static_cast<const WebKitCSSKeyframeRule*>(rule) : 0;
87}
88
89void WebKitCSSKeyframesRule::append(WebKitCSSKeyframeRule* rule)
90{
91    if (!rule)
92        return;
93
94    m_lstCSSRules->append(rule);
95    rule->setParent(this);
96
97    if (CSSMutableStyleDeclaration* style = rule->style())
98        style->setParent(this);
99}
100
101void WebKitCSSKeyframesRule::insertRule(const String& rule)
102{
103    CSSParser p(useStrictParsing());
104    RefPtr<CSSRule> newRule = p.parseKeyframeRule(parentStyleSheet(), rule);
105    if (newRule.get() && newRule.get()->isKeyframeRule())
106        append(static_cast<WebKitCSSKeyframeRule*>(newRule.get()));
107}
108
109void WebKitCSSKeyframesRule::deleteRule(const String& s)
110{
111    int i = findRuleIndex(s);
112    if (i >= 0)
113        m_lstCSSRules.get()->deleteRule(i);
114}
115
116WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::findRule(const String& s)
117{
118    int i = findRuleIndex(s);
119    return (i >= 0) ? item(i) : 0;
120}
121
122int WebKitCSSKeyframesRule::findRuleIndex(const String& key) const
123{
124    String percentageString;
125    if (equalIgnoringCase(key, "from"))
126        percentageString = "0%";
127    else if (equalIgnoringCase(key, "to"))
128        percentageString = "100%";
129    else
130        percentageString = key;
131
132    for (unsigned i = 0; i < length(); ++i) {
133        if (item(i)->keyText() == percentageString)
134            return i;
135    }
136
137    return -1;
138}
139
140String WebKitCSSKeyframesRule::cssText() const
141{
142    String result = "@-webkit-keyframes ";
143    result += m_name;
144    result += " { \n";
145
146    if (m_lstCSSRules) {
147        unsigned len = m_lstCSSRules->length();
148        for (unsigned i = 0; i < len; i++) {
149            result += "  ";
150            result += m_lstCSSRules->item(i)->cssText();
151            result += "\n";
152        }
153    }
154
155    result += "}";
156    return result;
157}
158
159} // namespace WebCore
160