2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
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.
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.
22 #include "core/svg/SVGStringList.h"
24 #include "core/svg/SVGElement.h"
25 #include "core/svg/SVGParserUtilities.h"
26 #include "wtf/text/StringBuilder.h"
30 SVGStringList::SVGStringList()
31 : NewSVGPropertyBase(classType())
35 SVGStringList::~SVGStringList()
39 void SVGStringList::initialize(const String& item)
42 m_values.append(item);
45 String SVGStringList::getItem(size_t index, ExceptionState& exceptionState)
47 if (!checkIndexBound(index, exceptionState))
50 return m_values.at(index);
53 void SVGStringList::insertItemBefore(const String& newItem, size_t index)
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();
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);
64 String SVGStringList::removeItem(size_t index, ExceptionState& exceptionState)
66 if (!checkIndexBound(index, exceptionState))
69 String oldItem = m_values.at(index);
70 m_values.remove(index);
74 void SVGStringList::appendItem(const String& newItem)
76 m_values.append(newItem);
79 void SVGStringList::replaceItem(const String& newItem, size_t index, ExceptionState& exceptionState)
81 if (!checkIndexBound(index, exceptionState))
84 // Update the value at the desired position 'index'.
85 m_values[index] = newItem;
88 template<typename CharType>
89 void SVGStringList::parseInternal(const CharType*& ptr, const CharType* end)
91 const UChar delimiter = ' ';
94 const CharType* start = ptr;
95 while (ptr < end && *ptr != delimiter && !isSVGSpace(*ptr))
99 m_values.append(String(start, ptr - start));
100 skipOptionalSVGSpacesOrDelimiter(ptr, end, delimiter);
104 PassRefPtr<SVGStringList> SVGStringList::clone()
106 RefPtr<SVGStringList> svgStringList = create();
107 svgStringList->m_values = m_values;
108 return svgStringList.release();
111 void SVGStringList::setValueAsString(const String& data, ExceptionState&)
113 // FIXME: Add more error checking and reporting.
118 const LChar* ptr = data.characters8();
119 const LChar* end = ptr + data.length();
120 parseInternal(ptr, end);
122 const UChar* ptr = data.characters16();
123 const UChar* end = ptr + data.length();
124 parseInternal(ptr, end);
128 PassRefPtr<NewSVGPropertyBase> SVGStringList::cloneForAnimation(const String& string) const
130 RefPtr<SVGStringList> svgStringList = create();
131 svgStringList->setValueAsString(string, IGNORE_EXCEPTION);
132 return svgStringList.release();
135 String SVGStringList::valueAsString() const
137 StringBuilder builder;
139 Vector<String>::const_iterator it = m_values.begin();
140 Vector<String>::const_iterator itEnd = m_values.end();
145 for (; it != itEnd; ++it) {
151 return builder.toString();
154 bool SVGStringList::checkIndexBound(size_t index, ExceptionState& exceptionState)
156 if (index >= m_values.size()) {
157 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("index", index, m_values.size()));
164 void SVGStringList::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contextElement)
166 // SVGStringList is never animated.
167 ASSERT_NOT_REACHED();
170 void SVGStringList::calculateAnimatedValue(SVGAnimationElement*, float, unsigned, PassRefPtr<NewSVGPropertyBase>, PassRefPtr<NewSVGPropertyBase>, PassRefPtr<NewSVGPropertyBase>, SVGElement*)
172 // SVGStringList is never animated.
173 ASSERT_NOT_REACHED();
176 float SVGStringList::calculateDistance(PassRefPtr<NewSVGPropertyBase>, SVGElement*)
178 // SVGStringList is never animated.
179 ASSERT_NOT_REACHED();