1 | /* |
2 | * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
3 | * Copyright (C) 2004, 2005, 2006, 2007, 2010, 2013 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 "DeprecatedCSSOMValue.h" |
25 | #include <wtf/text/StringBuilder.h> |
26 | |
27 | namespace WebCore { |
28 | |
29 | CSSValueList::CSSValueList(ClassType classType, ValueListSeparator listSeparator) |
30 | : CSSValue(classType) |
31 | { |
32 | m_valueListSeparator = listSeparator; |
33 | } |
34 | |
35 | CSSValueList::CSSValueList(ValueListSeparator listSeparator) |
36 | : CSSValue(ValueListClass) |
37 | { |
38 | m_valueListSeparator = listSeparator; |
39 | } |
40 | |
41 | bool CSSValueList::removeAll(CSSValue* value) |
42 | { |
43 | // FIXME: Why even take a pointer? |
44 | if (!value) |
45 | return false; |
46 | |
47 | return m_values.removeAllMatching([value](auto& current) { |
48 | return current->equals(*value); |
49 | }) > 0; |
50 | } |
51 | |
52 | bool CSSValueList::hasValue(CSSValue* val) const |
53 | { |
54 | // FIXME: Why even take a pointer? |
55 | if (!val) |
56 | return false; |
57 | |
58 | for (unsigned i = 0, size = m_values.size(); i < size; ++i) { |
59 | if (m_values[i].get().equals(*val)) |
60 | return true; |
61 | } |
62 | return false; |
63 | } |
64 | |
65 | Ref<CSSValueList> CSSValueList::copy() |
66 | { |
67 | RefPtr<CSSValueList> newList; |
68 | switch (m_valueListSeparator) { |
69 | case SpaceSeparator: |
70 | newList = createSpaceSeparated(); |
71 | break; |
72 | case CommaSeparator: |
73 | newList = createCommaSeparated(); |
74 | break; |
75 | case SlashSeparator: |
76 | newList = createSlashSeparated(); |
77 | break; |
78 | default: |
79 | ASSERT_NOT_REACHED(); |
80 | } |
81 | for (auto& value : m_values) |
82 | newList->append(value.get()); |
83 | return newList.releaseNonNull(); |
84 | } |
85 | |
86 | String CSSValueList::customCSSText() const |
87 | { |
88 | StringBuilder result; |
89 | String separator; |
90 | switch (m_valueListSeparator) { |
91 | case SpaceSeparator: |
92 | separator = " "_s ; |
93 | break; |
94 | case CommaSeparator: |
95 | separator = ", "_s ; |
96 | break; |
97 | case SlashSeparator: |
98 | separator = " / "_s ; |
99 | break; |
100 | default: |
101 | ASSERT_NOT_REACHED(); |
102 | } |
103 | |
104 | for (auto& value : m_values) { |
105 | if (!result.isEmpty()) |
106 | result.append(separator); |
107 | result.append(value.get().cssText()); |
108 | } |
109 | |
110 | return result.toString(); |
111 | } |
112 | |
113 | bool CSSValueList::equals(const CSSValueList& other) const |
114 | { |
115 | if (m_valueListSeparator != other.m_valueListSeparator) |
116 | return false; |
117 | |
118 | if (m_values.size() != other.m_values.size()) |
119 | return false; |
120 | |
121 | for (unsigned i = 0, size = m_values.size(); i < size; ++i) { |
122 | if (!m_values[i].get().equals(other.m_values[i])) |
123 | return false; |
124 | } |
125 | return true; |
126 | } |
127 | |
128 | bool CSSValueList::equals(const CSSValue& other) const |
129 | { |
130 | if (m_values.size() != 1) |
131 | return false; |
132 | |
133 | return m_values[0].get().equals(other); |
134 | } |
135 | |
136 | bool CSSValueList::traverseSubresources(const WTF::Function<bool (const CachedResource&)>& handler) const |
137 | { |
138 | for (unsigned i = 0; i < m_values.size(); ++i) { |
139 | if (m_values[i].get().traverseSubresources(handler)) |
140 | return true; |
141 | } |
142 | return false; |
143 | } |
144 | |
145 | } // namespace WebCore |
146 | |