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
25#include "Animation.h"
26#include "RenderObject.h"
27
28namespace WebCore {
29
30TimingFunction* KeyframeValue::timingFunction(const AtomicString& name) const
31{
32    const RenderStyle* keyframeStyle = style();
33    if (!keyframeStyle || !keyframeStyle->animations())
34        return nullptr;
35
36    for (size_t i = 0; i < keyframeStyle->animations()->size(); ++i) {
37        const Animation& animation = keyframeStyle->animations()->animation(i);
38        if (name == animation.name())
39            return animation.timingFunction().get();
40    }
41
42    return nullptr;
43}
44
45KeyframeList::~KeyframeList()
46{
47    clear();
48}
49
50void KeyframeList::clear()
51{
52    m_keyframes.clear();
53    m_properties.clear();
54}
55
56bool KeyframeList::operator==(const KeyframeList& o) const
57{
58    if (m_keyframes.size() != o.m_keyframes.size())
59        return false;
60
61    Vector<KeyframeValue>::const_iterator it2 = o.m_keyframes.begin();
62    for (Vector<KeyframeValue>::const_iterator it1 = m_keyframes.begin(); it1 != m_keyframes.end(); ++it1) {
63        if (it1->key() != it2->key())
64            return false;
65        const RenderStyle& style1 = *it1->style();
66        const RenderStyle& style2 = *it2->style();
67        if (style1 != style2)
68            return false;
69        ++it2;
70    }
71
72    return true;
73}
74
75void KeyframeList::insert(const KeyframeValue& keyframe)
76{
77    if (keyframe.key() < 0 || keyframe.key() > 1)
78        return;
79
80    bool inserted = false;
81    bool replaced = false;
82    for (size_t i = 0; i < m_keyframes.size(); ++i) {
83        if (m_keyframes[i].key() == keyframe.key()) {
84            m_keyframes[i] = keyframe;
85            replaced = true;
86            break;
87        }
88
89        if (m_keyframes[i].key() > keyframe.key()) {
90            // insert before
91            m_keyframes.insert(i, keyframe);
92            inserted = true;
93            break;
94        }
95    }
96
97    if (!replaced && !inserted)
98        m_keyframes.append(keyframe);
99
100    if (replaced) {
101        // We have to rebuild the properties list from scratch.
102        m_properties.clear();
103        for (Vector<KeyframeValue>::const_iterator it = m_keyframes.begin(); it != m_keyframes.end(); ++it) {
104            const KeyframeValue& currKeyframe = *it;
105            for (HashSet<CSSPropertyID>::const_iterator it = currKeyframe.properties().begin(); it != currKeyframe.properties().end(); ++it)
106                m_properties.add(*it);
107        }
108    } else {
109        for (HashSet<CSSPropertyID>::const_iterator it = keyframe.properties().begin(); it != keyframe.properties().end(); ++it)
110            m_properties.add(*it);
111    }
112}
113
114} // namespace WebCore
115