/
CSSCustomPropertyValue.cpp
179 lines (163 loc) · 7.38 KB
/
CSSCustomPropertyValue.cpp
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
/*
* Copyright (C) 2016-2021 Apple Inc. All rights reserved.
*
* 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,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* 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 "CSSCustomPropertyValue.h"
#include "CSSCalcValue.h"
#include "CSSFunctionValue.h"
#include "CSSMarkup.h"
#include "CSSParserIdioms.h"
#include "CSSTokenizer.h"
#include "ColorSerialization.h"
#include "ComputedStyleExtractor.h"
#include "RenderStyle.h"
namespace WebCore {
Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createEmpty(const AtomString& name)
{
return adoptRef(*new CSSCustomPropertyValue(name, std::monostate { }));
}
Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createWithID(const AtomString& name, CSSValueID id)
{
ASSERT(WebCore::isCSSWideKeyword(id) || id == CSSValueInvalid);
return adoptRef(*new CSSCustomPropertyValue(name, { id }));
}
bool CSSCustomPropertyValue::equals(const CSSCustomPropertyValue& other) const
{
if (m_name != other.m_name || m_value.index() != other.m_value.index())
return false;
return WTF::switchOn(m_value, [&](const std::monostate&) {
return true;
}, [&](const Ref<CSSVariableReferenceValue>& value) {
return value.get() == std::get<Ref<CSSVariableReferenceValue>>(other.m_value).get();
}, [&](const CSSValueID& value) {
return value == std::get<CSSValueID>(other.m_value);
}, [&](const Ref<CSSVariableData>& value) {
return value.get() == std::get<Ref<CSSVariableData>>(other.m_value).get();
}, [&](const SyntaxValue& value) {
return value == std::get<SyntaxValue>(other.m_value);
}, [&](const SyntaxValueList& value) {
return value == std::get<SyntaxValueList>(other.m_value);
});
}
String CSSCustomPropertyValue::customCSSText() const
{
auto serializeSyntaxValue = [](const SyntaxValue& syntaxValue) -> String {
return WTF::switchOn(syntaxValue, [&](const Length& value) {
if (value.type() == LengthType::Calculated) {
auto calcValue = CSSCalcValue::create(value.calculationValue(), RenderStyle::defaultStyle());
if (!calcValue) {
ASSERT_NOT_REACHED();
return emptyString();
}
return calcValue->cssText();
}
return CSSPrimitiveValue::create(value, RenderStyle::defaultStyle())->cssText();
}, [&](const NumericSyntaxValue& value) {
return CSSPrimitiveValue::create(value.value, value.unitType)->cssText();
}, [&](const StyleColor& value) {
return serializationForCSS(value);
}, [&](const RefPtr<StyleImage>& value) {
// FIXME: This is not right for gradients that use `currentcolor`. There should be a way preserve it.
return value->computedStyleValue(RenderStyle::defaultStyle())->cssText();
}, [&](const URL& value) {
return serializeURL(value.string());
}, [&](const String& value) {
return value;
}, [&](const TransformSyntaxValue& value) {
auto cssValue = transformOperationAsCSSValue(*value.transform, RenderStyle::defaultStyle());
if (!cssValue)
return emptyString();
return cssValue->cssText();
});
};
auto serialize = [&] {
return WTF::switchOn(m_value, [&](const std::monostate&) {
return emptyString();
}, [&](const Ref<CSSVariableReferenceValue>& value) {
return value->cssText();
}, [&](const CSSValueID& value) {
return nameString(value).string();
}, [&](const Ref<CSSVariableData>& value) {
return value->tokenRange().serialize();
}, [&](const SyntaxValue& syntaxValue) {
return serializeSyntaxValue(syntaxValue);
}, [&](const SyntaxValueList& syntaxValueList) {
StringBuilder builder;
auto separator = separatorCSSText(syntaxValueList.separator);
for (auto& syntaxValue : syntaxValueList.values) {
if (!builder.isEmpty())
builder.append(separator);
builder.append(serializeSyntaxValue(syntaxValue));
}
return builder.toString();
});
};
if (m_cachedCSSText.isNull())
m_cachedCSSText = serialize();
return m_cachedCSSText;
}
const Vector<CSSParserToken>& CSSCustomPropertyValue::tokens() const
{
static NeverDestroyed<Vector<CSSParserToken>> emptyTokens;
return WTF::switchOn(m_value, [&](const std::monostate&) -> const Vector<CSSParserToken>& {
// Do nothing.
return emptyTokens;
}, [&](const Ref<CSSVariableReferenceValue>&) -> const Vector<CSSParserToken>& {
ASSERT_NOT_REACHED();
return emptyTokens;
}, [&](const CSSValueID&) -> const Vector<CSSParserToken>& {
// Do nothing.
return emptyTokens;
}, [&](const Ref<CSSVariableData>& value) -> const Vector<CSSParserToken>& {
return value->tokens();
}, [&](auto&) -> const Vector<CSSParserToken>& {
if (!m_cachedTokens) {
CSSTokenizer tokenizer { customCSSText() };
m_cachedTokens = CSSVariableData::create(tokenizer.tokenRange());
}
return m_cachedTokens->tokens();
});
}
bool CSSCustomPropertyValue::containsCSSWideKeyword() const
{
return std::holds_alternative<CSSValueID>(m_value) && WebCore::isCSSWideKeyword(std::get<CSSValueID>(m_value));
}
Ref<const CSSVariableData> CSSCustomPropertyValue::asVariableData() const
{
return WTF::switchOn(m_value, [&](const Ref<CSSVariableData>& value) -> Ref<const CSSVariableData> {
return value.get();
}, [&](const Ref<CSSVariableReferenceValue>& value) -> Ref<const CSSVariableData> {
return value->data();
}, [&](auto&) -> Ref<const CSSVariableData> {
return CSSVariableData::create(tokens());
});
}
bool CSSCustomPropertyValue::isCurrentColor() const
{
// FIXME: it's surprising that setting a custom property to "currentcolor"
// results in m_value being a CSSVariableReferenceValue rather than a
// CSSValueID set to CSSValueCurrentcolor or a StyleValue.
return std::holds_alternative<Ref<CSSVariableReferenceValue>>(m_value) && std::get<Ref<CSSVariableReferenceValue>>(m_value)->customCSSText() == "currentcolor"_s;
}
}