1/*
2 * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23#include "KeyframeList.h"
24#include "RenderObject.h"
25
26namespace WebCore {
27
28KeyframeList::~KeyframeList()
29{
30    clear();
31}
32
33void KeyframeList::clear()
34{
35    m_keyframes.clear();
36    m_properties.clear();
37}
38
39bool KeyframeList::operator==(const KeyframeList& o) const
40{
41    if (m_keyframes.size() != o.m_keyframes.size())
42        return false;
43
44    Vector<KeyframeValue>::const_iterator it2 = o.m_keyframes.begin();
45    for (Vector<KeyframeValue>::const_iterator it1 = m_keyframes.begin(); it1 != m_keyframes.end(); ++it1) {
46        if (it1->key() != it2->key())
47            return false;
48        const RenderStyle& style1 = *it1->style();
49        const RenderStyle& style2 = *it2->style();
50        if (style1 != style2)
51            return false;
52        ++it2;
53    }
54
55    return true;
56}
57
58void KeyframeList::insert(const KeyframeValue& keyframe)
59{
60    if (keyframe.key() < 0 || keyframe.key() > 1)
61        return;
62
63    bool inserted = false;
64    bool replaced = false;
65    for (size_t i = 0; i < m_keyframes.size(); ++i) {
66        if (m_keyframes[i].key() == keyframe.key()) {
67            m_keyframes[i] = keyframe;
68            replaced = true;
69            break;
70        }
71
72        if (m_keyframes[i].key() > keyframe.key()) {
73            // insert before
74            m_keyframes.insert(i, keyframe);
75            inserted = true;
76            break;
77        }
78    }
79
80    if (!replaced && !inserted)
81        m_keyframes.append(keyframe);
82
83    if (replaced) {
84        // We have to rebuild the properties list from scratch.
85        m_properties.clear();
86        for (Vector<KeyframeValue>::const_iterator it = m_keyframes.begin(); it != m_keyframes.end(); ++it) {
87            const KeyframeValue& currKeyframe = *it;
88            for (HashSet<CSSPropertyID>::const_iterator it = currKeyframe.properties().begin(); it != currKeyframe.properties().end(); ++it)
89                m_properties.add(*it);
90        }
91    } else {
92        for (HashSet<CSSPropertyID>::const_iterator it = keyframe.properties().begin(); it != keyframe.properties().end(); ++it)
93            m_properties.add(*it);
94    }
95}
96
97} // namespace WebCore
98