1/**
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "CSSRuleList.h"
24
25#include "CSSMutableStyleDeclaration.h"
26#include "CSSRule.h"
27#include "StyleList.h"
28#include "WebKitCSSKeyframeRule.h"
29
30namespace WebCore {
31
32CSSRuleList::CSSRuleList()
33{
34}
35
36CSSRuleList::CSSRuleList(StyleList* list, bool omitCharsetRules)
37{
38    m_list = list;
39    if (list && omitCharsetRules) {
40        m_list = 0;
41        unsigned len = list->length();
42        for (unsigned i = 0; i < len; ++i) {
43            StyleBase* style = list->item(i);
44            if (style->isRule() && !style->isCharsetRule())
45                append(static_cast<CSSRule*>(style));
46        }
47    }
48}
49
50CSSRuleList::~CSSRuleList()
51{
52}
53
54unsigned CSSRuleList::length() const
55{
56    return m_list ? m_list->length() : m_lstCSSRules.size();
57}
58
59CSSRule* CSSRuleList::item(unsigned index)
60{
61    if (m_list) {
62        StyleBase* rule = m_list->item(index);
63        ASSERT(!rule || rule->isRule());
64        return static_cast<CSSRule*>(rule);
65    }
66
67    if (index < m_lstCSSRules.size())
68        return m_lstCSSRules[index].get();
69    return 0;
70}
71
72void CSSRuleList::deleteRule(unsigned index)
73{
74    ASSERT(!m_list);
75
76    if (index >= m_lstCSSRules.size()) {
77        // FIXME: Should we throw an INDEX_SIZE_ERR exception here?
78        return;
79    }
80
81    if (m_lstCSSRules[index]->isKeyframeRule()) {
82        if (CSSMutableStyleDeclaration* style = static_cast<WebKitCSSKeyframeRule*>(m_lstCSSRules[index].get())->style())
83            style->setParent(0);
84    }
85
86    m_lstCSSRules[index]->setParent(0);
87    m_lstCSSRules.remove(index);
88}
89
90void CSSRuleList::append(CSSRule* rule)
91{
92    ASSERT(!m_list);
93    if (!rule) {
94        // FIXME: Should we throw an exception?
95        return;
96    }
97
98    m_lstCSSRules.append(rule);
99}
100
101unsigned CSSRuleList::insertRule(CSSRule* rule, unsigned index)
102{
103    ASSERT(!m_list);
104    if (!rule) {
105        // FIXME: Should we throw an exception?
106        return 0;
107    }
108
109    if (index > m_lstCSSRules.size()) {
110        // FIXME: Should we throw an INDEX_SIZE_ERR exception here?
111        return 0;
112    }
113
114    m_lstCSSRules.insert(index, rule);
115    return index;
116}
117
118} // namespace WebCore
119