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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
*
* 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 "CSSValueList.h"
#include "CSSPrimitiveValue.h"
#include <wtf/Hasher.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator)
: CSSValue(type)
{
m_valueSeparator = separator;
}
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator, CSSValueListBuilder values)
: CSSValue(type)
, m_size(values.size())
{
m_valueSeparator = separator;
RELEASE_ASSERT(values.size() <= std::numeric_limits<unsigned>::max());
unsigned maxInlineSize = m_inlineStorage.size();
if (m_size <= maxInlineSize) {
for (unsigned i = 0; i < m_size; ++i)
m_inlineStorage[i] = &values[i].leakRef();
} else {
for (unsigned i = 0; i < maxInlineSize; ++i)
m_inlineStorage[i] = &values[i].leakRef();
m_additionalStorage = MallocSpan<const CSSValue*>::malloc(sizeof(const CSSValue*) * (m_size - maxInlineSize));
for (unsigned i = maxInlineSize; i < m_size; ++i)
m_additionalStorage[i - maxInlineSize] = &values[i].leakRef();
}
}
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator, Ref<CSSValue> value)
: CSSValue(type)
, m_size(1)
{
m_valueSeparator = separator;
m_inlineStorage[0] = &value.leakRef();
}
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2)
: CSSValue(type)
, m_size(2)
{
m_valueSeparator = separator;
m_inlineStorage[0] = &value1.leakRef();
m_inlineStorage[1] = &value2.leakRef();
}
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3)
: CSSValue(type)
, m_size(3)
{
m_valueSeparator = separator;
m_inlineStorage[0] = &value1.leakRef();
m_inlineStorage[1] = &value2.leakRef();
m_inlineStorage[2] = &value3.leakRef();
}
CSSValueContainingVector::CSSValueContainingVector(ClassType type, ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3, Ref<CSSValue> value4)
: CSSValue(type)
, m_size(4)
{
m_valueSeparator = separator;
m_inlineStorage[0] = &value1.leakRef();
m_inlineStorage[1] = &value2.leakRef();
m_inlineStorage[2] = &value3.leakRef();
m_inlineStorage[3] = &value4.leakRef();
}
CSSValueList::CSSValueList(ValueSeparator separator)
: CSSValueContainingVector(ClassType::ValueList, separator)
{
}
CSSValueList::CSSValueList(ValueSeparator separator, CSSValueListBuilder values)
: CSSValueContainingVector(ClassType::ValueList, separator, WTFMove(values))
{
}
CSSValueList::CSSValueList(ValueSeparator separator, Ref<CSSValue> value)
: CSSValueContainingVector(ClassType::ValueList, separator, WTFMove(value))
{
}
CSSValueList::CSSValueList(ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2)
: CSSValueContainingVector(ClassType::ValueList, separator, WTFMove(value1), WTFMove(value2))
{
}
CSSValueList::CSSValueList(ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3)
: CSSValueContainingVector(ClassType::ValueList, separator, WTFMove(value1), WTFMove(value2), WTFMove(value3))
{
}
CSSValueList::CSSValueList(ValueSeparator separator, Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3, Ref<CSSValue> value4)
: CSSValueContainingVector(ClassType::ValueList, separator, WTFMove(value1), WTFMove(value2), WTFMove(value3), WTFMove(value4))
{
}
Ref<CSSValueList> CSSValueList::createCommaSeparated(CSSValueListBuilder values)
{
return adoptRef(*new CSSValueList(CommaSeparator, WTFMove(values)));
}
Ref<CSSValueList> CSSValueList::createCommaSeparated(Ref<CSSValue> value)
{
return adoptRef(*new CSSValueList(CommaSeparator, WTFMove(value)));
}
Ref<CSSValueList> CSSValueList::createSlashSeparated(CSSValueListBuilder values)
{
return adoptRef(*new CSSValueList(SlashSeparator, WTFMove(values)));
}
Ref<CSSValueList> CSSValueList::createSlashSeparated(Ref<CSSValue> value)
{
return adoptRef(*new CSSValueList(SlashSeparator, WTFMove(value)));
}
Ref<CSSValueList> CSSValueList::createSlashSeparated(Ref<CSSValue> value1, Ref<CSSValue> value2)
{
return adoptRef(*new CSSValueList(SlashSeparator, WTFMove(value1), WTFMove(value2)));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated()
{
return adoptRef(*new CSSValueList(SpaceSeparator));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated(CSSValueListBuilder values)
{
return adoptRef(*new CSSValueList(SpaceSeparator, WTFMove(values)));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated(Ref<CSSValue> value)
{
return adoptRef(*new CSSValueList(SpaceSeparator, WTFMove(value)));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated(Ref<CSSValue> value1, Ref<CSSValue> value2)
{
return adoptRef(*new CSSValueList(SpaceSeparator, WTFMove(value1), WTFMove(value2)));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated(Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3)
{
return adoptRef(*new CSSValueList(SpaceSeparator, WTFMove(value1), WTFMove(value2), WTFMove(value3)));
}
Ref<CSSValueList> CSSValueList::createSpaceSeparated(Ref<CSSValue> value1, Ref<CSSValue> value2, Ref<CSSValue> value3, Ref<CSSValue> value4)
{
return adoptRef(*new CSSValueList(SpaceSeparator, WTFMove(value1), WTFMove(value2), WTFMove(value3), WTFMove(value4)));
}
Ref<CSSValueList> CSSValueList::create(UChar separator, CSSValueListBuilder builder)
{
switch (separator) {
case ',':
return createCommaSeparated(WTFMove(builder));
case '/':
return createSlashSeparated(WTFMove(builder));
case ' ':
return createSpaceSeparated(WTFMove(builder));
default:
break;
}
RELEASE_ASSERT_NOT_REACHED();
}
bool CSSValueContainingVector::hasValue(CSSValue& otherValue) const
{
for (auto& value : *this) {
if (value.equals(otherValue))
return true;
}
return false;
}
bool CSSValueContainingVector::hasValue(CSSValueID otherValue) const
{
for (auto& value : *this) {
if (WebCore::isValueID(value, otherValue))
return true;
}
return false;
}
CSSValueListBuilder CSSValueContainingVector::copyValues() const
{
return WTF::map<CSSValueListBuilderInlineCapacity>(*this, [](auto& value) -> Ref<CSSValue> {
return const_cast<CSSValue&>(value);
});
}
void CSSValueContainingVector::serializeItems(StringBuilder& builder, const CSS::SerializationContext& context) const
{
builder.append(interleave(*this, [&](auto& value) { return value.cssText(context); }, separatorCSSText()));
}
String CSSValueContainingVector::serializeItems(const CSS::SerializationContext& context) const
{
StringBuilder result;
serializeItems(result, context);
return result.toString();
}
String CSSValueList::customCSSText(const CSS::SerializationContext& context) const
{
return serializeItems(context);
}
bool CSSValueContainingVector::itemsEqual(const CSSValueContainingVector& other) const
{
unsigned size = this->size();
if (size != other.size())
return false;
for (unsigned i = 0; i < size; ++i) {
if (!(*this)[i].equals(other[i]))
return false;
}
return true;
}
bool CSSValueList::equals(const CSSValueList& other) const
{
return separator() == other.separator() && itemsEqual(other);
}
bool CSSValueContainingVector::containsSingleEqualItem(const CSSValue& other) const
{
return size() == 1 && (*this)[0].equals(other);
}
bool CSSValueContainingVector::addDerivedHash(Hasher& hasher) const
{
add(hasher, separator());
for (auto& item : *this) {
if (!item.addHash(hasher))
return false;
}
return true;
}
bool CSSValueContainingVector::customTraverseSubresources(NOESCAPE const Function<bool(const CachedResource&)>& handler) const
{
for (auto& value : *this) {
if (value.traverseSubresources(handler))
return true;
}
return false;
}
IterationStatus CSSValueContainingVector::customVisitChildren(NOESCAPE const Function<IterationStatus(CSSValue&)>& func) const
{
for (auto& value : *this) {
if (func(const_cast<CSSValue&>(value)) == IterationStatus::Done)
return IterationStatus::Done;
}
return IterationStatus::Continue;
}
} // namespace WebCore
|