1/*
2 * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "SVGParserUtilities.h"
29#include "SVGPrimitiveList.h"
30#include <wtf/text/StringBuilder.h>
31
32namespace WebCore {
33
34class SVGStringList final : public SVGPrimitiveList<String> {
35 using Base = SVGPrimitiveList<String>;
36 using Base::Base;
37 using Base::m_items;
38
39public:
40 static Ref<SVGStringList> create(SVGPropertyOwner* owner)
41 {
42 return adoptRef(*new SVGStringList(owner));
43 }
44
45 void reset(const String& string)
46 {
47 parse(string, ' ');
48
49 // Add empty string, if list is empty.
50 if (m_items.isEmpty())
51 m_items.append(emptyString());
52 }
53
54 bool parse(const String& data, UChar delimiter)
55 {
56 clearItems();
57
58 auto upconvertedCharacters = StringView(data).upconvertedCharacters();
59 const UChar* ptr = upconvertedCharacters;
60 const UChar* end = ptr + data.length();
61 while (ptr < end) {
62 const UChar* start = ptr;
63 while (ptr < end && *ptr != delimiter && !isSVGSpace(*ptr))
64 ptr++;
65 if (ptr == start)
66 break;
67 m_items.append(String(start, ptr - start));
68 skipOptionalSVGSpacesOrDelimiter(ptr, end, delimiter);
69 }
70
71 return ptr == end;
72 }
73
74 String valueAsString() const override
75 {
76 StringBuilder builder;
77
78 for (auto string : m_items) {
79 if (builder.length())
80 builder.append(' ');
81
82 builder.append(string);
83 }
84
85 return builder.toString();
86 }
87};
88
89} // namespace WebCore
90