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 "WebKitCSSKeyframeRule.h"
28
29#include "PropertySetCSSStyleDeclaration.h"
30#include "StylePropertySet.h"
31#include "WebKitCSSKeyframesRule.h"
32#include <wtf/text/StringBuilder.h>
33
34namespace WebCore {
35
36StyleKeyframe::StyleKeyframe()
37{
38}
39
40StyleKeyframe::~StyleKeyframe()
41{
42}
43
44MutableStylePropertySet* StyleKeyframe::mutableProperties()
45{
46    if (!m_properties->isMutable())
47        m_properties = m_properties->mutableCopy();
48    return static_cast<MutableStylePropertySet*>(m_properties.get());
49}
50
51void StyleKeyframe::setProperties(PassRefPtr<StylePropertySet> properties)
52{
53    m_properties = properties;
54}
55
56/* static */
57void StyleKeyframe::parseKeyString(const String& s, Vector<float>& keys)
58{
59    keys.clear();
60    Vector<String> strings;
61    s.split(',', strings);
62
63    for (size_t i = 0; i < strings.size(); ++i) {
64        float key = -1;
65        String cur = strings[i].stripWhiteSpace();
66
67        // For now the syntax MUST be 'xxx%' or 'from' or 'to', where xxx is a legal floating point number
68        if (cur == "from")
69            key = 0;
70        else if (cur == "to")
71            key = 1;
72        else if (cur.endsWith('%')) {
73            float k = cur.substring(0, cur.length() - 1).toFloat();
74            if (k >= 0 && k <= 100)
75                key = k/100;
76        }
77        if (key < 0) {
78            keys.clear();
79            return;
80        }
81        else
82            keys.append(key);
83    }
84}
85
86String StyleKeyframe::cssText() const
87{
88    StringBuilder result;
89    result.append(keyText());
90    result.appendLiteral(" { ");
91    String decls = m_properties->asText();
92    result.append(decls);
93    if (!decls.isEmpty())
94        result.append(' ');
95    result.append('}');
96    return result.toString();
97}
98
99WebKitCSSKeyframeRule::WebKitCSSKeyframeRule(StyleKeyframe* keyframe, WebKitCSSKeyframesRule* parent)
100    : CSSRule(0)
101    , m_keyframe(keyframe)
102{
103    setParentRule(parent);
104}
105
106WebKitCSSKeyframeRule::~WebKitCSSKeyframeRule()
107{
108    if (m_propertiesCSSOMWrapper)
109        m_propertiesCSSOMWrapper->clearParentRule();
110}
111
112CSSStyleDeclaration* WebKitCSSKeyframeRule::style()
113{
114    if (!m_propertiesCSSOMWrapper)
115        m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_keyframe->mutableProperties(), this);
116    return m_propertiesCSSOMWrapper.get();
117}
118
119void WebKitCSSKeyframeRule::reattach(StyleRuleBase*)
120{
121    // No need to reattach, the underlying data is shareable on mutation.
122    ASSERT_NOT_REACHED();
123}
124
125} // namespace WebCore
126