summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/WebKit/Source/core/svg/SVGStringList.cpp
blob: 216cd2b4fa2d4d5abaa16c895a15b63b12699564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "core/svg/SVGStringList.h"

#include "core/svg/SVGElement.h"
#include "core/svg/SVGParserUtilities.h"
#include "wtf/text/StringBuilder.h"

namespace WebCore {

SVGStringList::SVGStringList()
    : SVGPropertyBase(classType())
{
}

SVGStringList::~SVGStringList()
{
}

void SVGStringList::initialize(const String& item)
{
    m_values.clear();
    m_values.append(item);
}

String SVGStringList::getItem(size_t index, ExceptionState& exceptionState)
{
    if (!checkIndexBound(index, exceptionState))
        return String();

    return m_values.at(index);
}

void SVGStringList::insertItemBefore(const String& newItem, size_t index)
{
    // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
    if (index > m_values.size())
        index = m_values.size();

    // Spec: Inserts a new item into the list at the specified position. The index of the item before which the new item is to be
    // inserted. The first item is number 0. If the index is equal to 0, then the new item is inserted at the front of the list.
    m_values.insert(index, newItem);
}

String SVGStringList::removeItem(size_t index, ExceptionState& exceptionState)
{
    if (!checkIndexBound(index, exceptionState))
        return String();

    String oldItem = m_values.at(index);
    m_values.remove(index);
    return oldItem;
}

void SVGStringList::appendItem(const String& newItem)
{
    m_values.append(newItem);
}

void SVGStringList::replaceItem(const String& newItem, size_t index, ExceptionState& exceptionState)
{
    if (!checkIndexBound(index, exceptionState))
        return;

    // Update the value at the desired position 'index'.
    m_values[index] = newItem;
}

template<typename CharType>
void SVGStringList::parseInternal(const CharType*& ptr, const CharType* end)
{
    const UChar delimiter = ' ';

    while (ptr < end) {
        const CharType* start = ptr;
        while (ptr < end && *ptr != delimiter && !isHTMLSpace<CharType>(*ptr))
            ptr++;
        if (ptr == start)
            break;
        m_values.append(String(start, ptr - start));
        skipOptionalSVGSpacesOrDelimiter(ptr, end, delimiter);
    }
}

PassRefPtr<SVGStringList> SVGStringList::clone()
{
    RefPtr<SVGStringList> svgStringList = create();
    svgStringList->m_values = m_values;
    return svgStringList.release();
}

void SVGStringList::setValueAsString(const String& data, ExceptionState&)
{
    // FIXME: Add more error checking and reporting.
    m_values.clear();
    if (data.isEmpty())
        return;
    if (data.is8Bit()) {
        const LChar* ptr = data.characters8();
        const LChar* end = ptr + data.length();
        parseInternal(ptr, end);
    } else {
        const UChar* ptr = data.characters16();
        const UChar* end = ptr + data.length();
        parseInternal(ptr, end);
    }
}

PassRefPtr<SVGPropertyBase> SVGStringList::cloneForAnimation(const String& string) const
{
    RefPtr<SVGStringList> svgStringList = create();
    svgStringList->setValueAsString(string, IGNORE_EXCEPTION);
    return svgStringList.release();
}

String SVGStringList::valueAsString() const
{
    StringBuilder builder;

    Vector<String>::const_iterator it = m_values.begin();
    Vector<String>::const_iterator itEnd = m_values.end();
    if (it != itEnd) {
        builder.append(*it);
        ++it;

        for (; it != itEnd; ++it) {
            builder.append(' ');
            builder.append(*it);
        }
    }

    return builder.toString();
}

bool SVGStringList::checkIndexBound(size_t index, ExceptionState& exceptionState)
{
    if (index >= m_values.size()) {
        exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("index", index, m_values.size()));
        return false;
    }

    return true;
}

void SVGStringList::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* contextElement)
{
    // SVGStringList is never animated.
    ASSERT_NOT_REACHED();
}

void SVGStringList::calculateAnimatedValue(SVGAnimationElement*, float, unsigned, PassRefPtr<SVGPropertyBase>, PassRefPtr<SVGPropertyBase>, PassRefPtr<SVGPropertyBase>, SVGElement*)
{
    // SVGStringList is never animated.
    ASSERT_NOT_REACHED();
}

float SVGStringList::calculateDistance(PassRefPtr<SVGPropertyBase>, SVGElement*)
{
    // SVGStringList is never animated.
    ASSERT_NOT_REACHED();

    return -1.0f;
}

}