Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGStringList.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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 "core/svg/SVGStringList.h"
23
24 #include "core/svg/SVGElement.h"
25 #include "core/svg/SVGParserUtilities.h"
26 #include "wtf/text/StringBuilder.h"
27
28 namespace WebCore {
29
30 SVGStringList::SVGStringList()
31     : NewSVGPropertyBase(classType())
32 {
33 }
34
35 SVGStringList::~SVGStringList()
36 {
37 }
38
39 void SVGStringList::initialize(const String& item)
40 {
41     m_values.clear();
42     m_values.append(item);
43 }
44
45 String SVGStringList::getItem(size_t index, ExceptionState& exceptionState)
46 {
47     if (!checkIndexBound(index, exceptionState))
48         return String();
49
50     return m_values.at(index);
51 }
52
53 void SVGStringList::insertItemBefore(const String& newItem, size_t index)
54 {
55     // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
56     if (index > m_values.size())
57         index = m_values.size();
58
59     // 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
60     // 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.
61     m_values.insert(index, newItem);
62 }
63
64 String SVGStringList::removeItem(size_t index, ExceptionState& exceptionState)
65 {
66     if (!checkIndexBound(index, exceptionState))
67         return String();
68
69     String oldItem = m_values.at(index);
70     m_values.remove(index);
71     return oldItem;
72 }
73
74 void SVGStringList::appendItem(const String& newItem)
75 {
76     m_values.append(newItem);
77 }
78
79 void SVGStringList::replaceItem(const String& newItem, size_t index, ExceptionState& exceptionState)
80 {
81     if (!checkIndexBound(index, exceptionState))
82         return;
83
84     // Update the value at the desired position 'index'.
85     m_values[index] = newItem;
86 }
87
88 template<typename CharType>
89 void SVGStringList::parseInternal(const CharType*& ptr, const CharType* end)
90 {
91     const UChar delimiter = ' ';
92
93     while (ptr < end) {
94         const CharType* start = ptr;
95         while (ptr < end && *ptr != delimiter && !isSVGSpace(*ptr))
96             ptr++;
97         if (ptr == start)
98             break;
99         m_values.append(String(start, ptr - start));
100         skipOptionalSVGSpacesOrDelimiter(ptr, end, delimiter);
101     }
102 }
103
104 PassRefPtr<SVGStringList> SVGStringList::clone()
105 {
106     RefPtr<SVGStringList> svgStringList = create();
107     svgStringList->m_values = m_values;
108     return svgStringList.release();
109 }
110
111 void SVGStringList::setValueAsString(const String& data, ExceptionState&)
112 {
113     // FIXME: Add more error checking and reporting.
114     m_values.clear();
115     if (data.isEmpty())
116         return;
117     if (data.is8Bit()) {
118         const LChar* ptr = data.characters8();
119         const LChar* end = ptr + data.length();
120         parseInternal(ptr, end);
121     } else {
122         const UChar* ptr = data.characters16();
123         const UChar* end = ptr + data.length();
124         parseInternal(ptr, end);
125     }
126 }
127
128 PassRefPtr<NewSVGPropertyBase> SVGStringList::cloneForAnimation(const String& string) const
129 {
130     RefPtr<SVGStringList> svgStringList = create();
131     svgStringList->setValueAsString(string, IGNORE_EXCEPTION);
132     return svgStringList.release();
133 }
134
135 String SVGStringList::valueAsString() const
136 {
137     StringBuilder builder;
138
139     Vector<String>::const_iterator it = m_values.begin();
140     Vector<String>::const_iterator itEnd = m_values.end();
141     if (it != itEnd) {
142         builder.append(*it);
143         ++it;
144
145         for (; it != itEnd; ++it) {
146             builder.append(' ');
147             builder.append(*it);
148         }
149     }
150
151     return builder.toString();
152 }
153
154 bool SVGStringList::checkIndexBound(size_t index, ExceptionState& exceptionState)
155 {
156     if (index >= m_values.size()) {
157         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("index", index, m_values.size()));
158         return false;
159     }
160
161     return true;
162 }
163
164 void SVGStringList::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contextElement)
165 {
166     // SVGStringList is never animated.
167     ASSERT_NOT_REACHED();
168 }
169
170 void SVGStringList::calculateAnimatedValue(SVGAnimationElement*, float, unsigned, PassRefPtr<NewSVGPropertyBase>, PassRefPtr<NewSVGPropertyBase>, PassRefPtr<NewSVGPropertyBase>, SVGElement*)
171 {
172     // SVGStringList is never animated.
173     ASSERT_NOT_REACHED();
174 }
175
176 float SVGStringList::calculateDistance(PassRefPtr<NewSVGPropertyBase>, SVGElement*)
177 {
178     // SVGStringList is never animated.
179     ASSERT_NOT_REACHED();
180
181     return -1.0f;
182 }
183
184 }