1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2010 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#include "config.h"
22#include "CSSValueList.h"
23
24#include "CSSParserValues.h"
25#include "PlatformString.h"
26#include <wtf/PassOwnPtr.h>
27
28namespace WebCore {
29
30CSSValueList::CSSValueList(bool isSpaceSeparated)
31    : m_isSpaceSeparated(isSpaceSeparated)
32{
33}
34
35CSSValueList::CSSValueList(CSSParserValueList* list)
36    : m_isSpaceSeparated(true)
37{
38    if (list) {
39        size_t size = list->size();
40        for (unsigned i = 0; i < size; ++i)
41            append(list->valueAt(i)->createCSSValue());
42    }
43}
44
45CSSValueList::~CSSValueList()
46{
47}
48
49CSSValue* CSSValueList::item(unsigned index)
50{
51    if (index >= m_values.size())
52        return 0;
53    return m_values[index].get();
54}
55
56unsigned short CSSValueList::cssValueType() const
57{
58    return CSS_VALUE_LIST;
59}
60
61void CSSValueList::append(PassRefPtr<CSSValue> val)
62{
63    m_values.append(val);
64}
65
66void CSSValueList::prepend(PassRefPtr<CSSValue> val)
67{
68    m_values.prepend(val);
69}
70
71bool CSSValueList::removeAll(CSSValue* val)
72{
73    bool found = false;
74    // FIXME: we should be implementing operator== to CSSValue and its derived classes
75    // to make comparison more flexible and fast.
76    for (size_t index = 0; index < m_values.size(); index++) {
77        if (m_values.at(index)->cssText() == val->cssText()) {
78            m_values.remove(index);
79            found = true;
80        }
81    }
82
83    return found;
84}
85
86bool CSSValueList::hasValue(CSSValue* val)
87{
88    // FIXME: we should be implementing operator== to CSSValue and its derived classes
89    // to make comparison more flexible and fast.
90    for (size_t index = 0; index < m_values.size(); index++) {
91        if (m_values.at(index)->cssText() == val->cssText())
92            return true;
93    }
94    return false;
95}
96
97PassRefPtr<CSSValueList> CSSValueList::copy()
98{
99    PassRefPtr<CSSValueList> newList = m_isSpaceSeparated ? createSpaceSeparated() : createCommaSeparated();
100    for (size_t index = 0; index < m_values.size(); index++)
101        newList->append(item(index));
102    return newList;
103}
104
105String CSSValueList::cssText() const
106{
107    String result = "";
108
109    unsigned size = m_values.size();
110    for (unsigned i = 0; i < size; i++) {
111        if (!result.isEmpty()) {
112            if (m_isSpaceSeparated)
113                result += " ";
114            else
115                result += ", ";
116        }
117        result += m_values[i]->cssText();
118    }
119
120    return result;
121}
122
123void CSSValueList::addSubresourceStyleURLs(ListHashSet<KURL>& urls, const CSSStyleSheet* styleSheet)
124{
125    size_t size = m_values.size();
126    for (size_t i = 0; i < size; ++i)
127        m_values[i]->addSubresourceStyleURLs(urls, styleSheet);
128}
129
130} // namespace WebCore
131