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
|
/*
* Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "StyleCustomProperty.h"
#include "CSSCalcValue.h"
#include "CSSPrimitiveValue.h"
#include "CSSSerializationContext.h"
#include "CSSTokenizer.h"
#include "CSSValueList.h"
#include "CSSValuePool.h"
#include "CSSVariableReferenceValue.h"
#include "RenderStyle.h"
#include "StyleCalculationValue.h"
#include "StyleExtractorConverter.h"
#include "StyleExtractorSerializer.h"
#include "StylePrimitiveNumericTypes+CSSValueCreation.h"
#include "StylePrimitiveNumericTypes+Serialization.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
namespace Style {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(CustomProperty);
bool CustomProperty::operator==(const CustomProperty& other) const
{
if (m_name != other.m_name || m_value.index() != other.m_value.index())
return false;
return WTF::switchOn(m_value,
[&](const GuaranteedInvalid&) {
return true;
},
[&](const Ref<CSSVariableData>& value) {
return arePointingToEqualData(value, std::get<Ref<CSSVariableData>>(other.m_value));
},
[&](const Value& value) {
return value == std::get<Value>(other.m_value);
},
[&](const ValueList& value) {
return value == std::get<ValueList>(other.m_value);
}
);
}
Ref<CSSValue> CustomProperty::propertyValue(CSSValuePool& pool, const RenderStyle& style) const
{
auto convertValue = [&](const Value& value) {
return WTF::switchOn(value,
[&](const auto& value) -> Ref<CSSValue> {
return createCSSValue(pool, style, value);
}
);
};
return WTF::switchOn(m_value,
[&](const GuaranteedInvalid&) -> Ref<CSSValue> {
return CSSPrimitiveValue::create(""_s);
},
[&](const Ref<CSSVariableData>& variableData) -> Ref<CSSValue> {
return CSSVariableReferenceValue::create(variableData.copyRef());
},
[&](const Value& value) -> Ref<CSSValue> {
return convertValue(value);
},
[&](const ValueList& valueList) -> Ref<CSSValue> {
CSSValueListBuilder builder;
for (auto& value : valueList.values)
builder.append(convertValue(value));
switch (valueList.separator) {
case CSSValue::SpaceSeparator:
return CSSValueList::createSpaceSeparated(WTFMove(builder));
case CSSValue::CommaSeparator:
return CSSValueList::createCommaSeparated(WTFMove(builder));
case CSSValue::SlashSeparator:
return CSSValueList::createSlashSeparated(WTFMove(builder));
}
RELEASE_ASSERT_NOT_REACHED();
}
);
}
String CustomProperty::propertyValueSerialization(const CSS::SerializationContext& context, const RenderStyle& style) const
{
StringBuilder builder;
propertyValueSerialization(builder, context, style);
return builder.toString();
}
void CustomProperty::propertyValueSerialization(StringBuilder& builder, const CSS::SerializationContext& context, const RenderStyle& style) const
{
auto serializeValue = [&](StringBuilder& builder, const Value& value) {
WTF::switchOn(value,
[&](const auto& value) {
Style::serializationForCSS(builder, context, style, value);
}
);
};
WTF::switchOn(m_value,
[&](const GuaranteedInvalid&) {
// "The guaranteed-invalid value serializes as the empty string".
// https://drafts.csswg.org/css-variables-2/#guaranteed-invalid
builder.append(emptyString());
},
[&](const Ref<CSSVariableData>& value) {
builder.append(value->serialize());
},
[&](const Value& value) {
serializeValue(builder, value);
},
[&](const ValueList& valueList) {
builder.append(interleave(valueList.values, [&](auto& builder, const auto& value) {
serializeValue(builder, value);
}, CSSValue::separatorCSSText(valueList.separator)));
}
);
}
String CustomProperty::propertyValueSerializationForTokenization(const CSS::SerializationContext& context, const RenderStyle& style) const
{
StringBuilder builder;
propertyValueSerializationForTokenization(builder, context, style);
return builder.toString();
}
void CustomProperty::propertyValueSerializationForTokenization(StringBuilder& builder, const CSS::SerializationContext& context, const RenderStyle& style) const
{
// `propertyValueSerializationForTokenization` differs from `propertyValueSerialization` only in how it handles custom `color`
// values:
//
// `propertyValueSerialization`: serializes the used/resolved value of `color` values (needed for getComputedStyle)
// `propertyValueSerializationForTokenization`: serializes the computed value of `color` values (needed for style building)
auto serializeValue = [&](StringBuilder& builder, const Value& value) {
WTF::switchOn(value,
[&](const Color& value) {
Style::serializationForCSSTokenization(builder, context, value);
},
[&](const auto& value) {
Style::serializationForCSS(builder, context, style, value);
}
);
};
WTF::switchOn(m_value,
[&](const GuaranteedInvalid&) {
// "The guaranteed-invalid value serializes as the empty string".
// https://drafts.csswg.org/css-variables-2/#guaranteed-invalid
builder.append(emptyString());
},
[&](const Ref<CSSVariableData>& value) {
builder.append(value->serialize());
},
[&](const Value& value) {
serializeValue(builder, value);
},
[&](const ValueList& valueList) {
builder.append(interleave(valueList.values, [&](auto& builder, const auto& value) {
serializeValue(builder, value);
}, CSSValue::separatorCSSText(valueList.separator)));
}
);
}
const Vector<CSSParserToken>& CustomProperty::tokens() const
{
static NeverDestroyed<Vector<CSSParserToken>> emptyTokens;
return WTF::switchOn(m_value,
[&](const Ref<CSSVariableData>& value) -> const Vector<CSSParserToken>& {
return value->tokens();
},
[&](auto&) -> const Vector<CSSParserToken>& {
if (!m_cachedTokens) {
CSSTokenizer tokenizer { propertyValueSerializationForTokenization(CSS::defaultSerializationContext(), RenderStyle::defaultStyleSingleton()) };
m_cachedTokens = CSSVariableData::create(tokenizer.tokenRange());
}
return m_cachedTokens->tokens();
}
);
}
} // namespace Style
} // namespace WebCore
|