forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSStyleValueFactory.cpp
More file actions
282 lines (245 loc) · 13.4 KB
/
CSSStyleValueFactory.cpp
File metadata and controls
282 lines (245 loc) · 13.4 KB
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
/*
* Copyright (C) 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 THE COPYRIGHT HOLDER “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 THE COPYRIGHT HOLDER 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 "CSSStyleValueFactory.h"
#if ENABLE(CSS_TYPED_OM)
#include "CSSCustomPropertyValue.h"
#include "CSSKeywordValue.h"
#include "CSSNumericFactory.h"
#include "CSSParser.h"
#include "CSSPendingSubstitutionValue.h"
#include "CSSPropertyParser.h"
#include "CSSStyleImageValue.h"
#include "CSSStyleValue.h"
#include "CSSTokenizer.h"
#include "CSSUnitValue.h"
#include "CSSUnparsedValue.h"
#include "CSSValueList.h"
#include "CSSVariableData.h"
#include "CSSVariableReferenceValue.h"
#include "StylePropertyShorthand.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringView.h>
namespace WebCore {
ExceptionOr<void> CSSStyleValueFactory::extractCSSValues(Vector<Ref<CSSValue>>& cssValues, const CSSPropertyID& propertyID, const String& cssText)
{
auto styleDeclaration = MutableStyleProperties::create();
constexpr bool important = true;
CSSParser::ParseResult parseResult = CSSParser::parseValue(styleDeclaration, propertyID, cssText, important, strictCSSParserContext());
if (parseResult == CSSParser::ParseResult::Error)
return Exception { TypeError, makeString(cssText, " cannot be parsed.")};
if (auto cssValue = styleDeclaration->getPropertyCSSValue(propertyID))
cssValues.append(cssValue.releaseNonNull());
return { };
}
ExceptionOr<void> CSSStyleValueFactory::extractShorthandCSSValues(Vector<Ref<CSSValue>>& cssValues, const CSSPropertyID& propertyID, const String& cssText)
{
auto styleDeclaration = MutableStyleProperties::create();
constexpr bool important = true;
CSSParser::ParseResult parseResult = CSSParser::parseValue(styleDeclaration, propertyID, cssText, important, strictCSSParserContext());
if (parseResult == CSSParser::ParseResult::Error)
return Exception { TypeError, makeString(cssText, " cannot be parsed.")};
auto shorthand = shorthandForProperty(propertyID);
for (auto longhand : shorthand) {
if (auto cssValue = styleDeclaration->getPropertyCSSValue(longhand))
cssValues.append(cssValue.releaseNonNull());
}
return { };
}
ExceptionOr<void> CSSStyleValueFactory::extractCustomCSSValues(Vector<Ref<CSSValue>>& cssValues, const AtomString& customPropertyName, const String& cssText)
{
auto styleDeclaration = MutableStyleProperties::create();
constexpr bool important = true;
CSSParser::ParseResult parseResult = CSSParser::parseCustomPropertyValue(styleDeclaration, customPropertyName, cssText, important, strictCSSParserContext());
if (parseResult == CSSParser::ParseResult::Error)
return Exception { TypeError, makeString(cssText, " cannot be parsed.")};
if (auto customValue = styleDeclaration->getPropertyCSSValue(CSSPropertyCustom))
cssValues.append(customValue.releaseNonNull());
return { };
}
ExceptionOr<Vector<Ref<CSSStyleValue>>> CSSStyleValueFactory::parseStyleValue(const AtomString& cssProperty, const String& cssText, bool parseMultiple)
{
// https://www.w3.org/TR/css-typed-om-1/#cssstylevalue
Vector<Ref<CSSValue>> cssValues;
// Extract the CSSValue from cssText given cssProperty
if (isCustomPropertyName(cssProperty)) {
auto result = extractCustomCSSValues(cssValues, cssProperty, cssText);
if (result.hasException())
return result.releaseException();
} else {
auto property = cssProperty.convertToASCIILowercase();
auto propertyID = cssPropertyID(property);
if (propertyID == CSSPropertyInvalid)
return Exception { TypeError, "Property String is not a valid CSS property."_s };
if (isShorthandCSSProperty(propertyID)) {
auto result = extractShorthandCSSValues(cssValues, propertyID, cssText);
if (result.hasException())
return result.releaseException();
} else {
auto result = extractCSSValues(cssValues, propertyID, cssText);
if (result.hasException())
return result.releaseException();
}
}
Vector<Ref<CSSStyleValue>> results;
for (auto& cssValue : cssValues) {
auto reifiedValue = reifyValue(WTFMove(cssValue));
if (reifiedValue.hasException())
return reifiedValue.releaseException();
results.append(reifiedValue.releaseReturnValue());
if (!parseMultiple)
break;
}
return results;
}
ExceptionOr<Ref<CSSStyleValue>> CSSStyleValueFactory::reifyValue(Ref<CSSValue> cssValue, Document* document)
{
if (is<CSSPrimitiveValue>(cssValue)) {
auto primitiveValue = downcast<CSSPrimitiveValue>(cssValue.ptr());
switch (primitiveValue->primitiveType()) {
case CSSUnitType::CSS_NUMBER:
return Ref<CSSStyleValue> { CSSNumericFactory::number(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_PERCENTAGE:
return Ref<CSSStyleValue> { CSSNumericFactory::percent(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_EMS:
return Ref<CSSStyleValue> { CSSNumericFactory::em(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_EXS:
return Ref<CSSStyleValue> { CSSNumericFactory::ex(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CHS:
return Ref<CSSStyleValue> { CSSNumericFactory::ch(primitiveValue->doubleValue()) };
// FIXME: Add CSSNumericFactory::ic
case CSSUnitType::CSS_REMS:
return Ref<CSSStyleValue> { CSSNumericFactory::rem(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_LHS:
return Ref<CSSStyleValue> { CSSNumericFactory::lh(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_RLHS:
return Ref<CSSStyleValue> { CSSNumericFactory::rlh(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_VW:
return Ref<CSSStyleValue> { CSSNumericFactory::vw(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_VH:
return Ref<CSSStyleValue> { CSSNumericFactory::vh(primitiveValue->doubleValue()) };
// FIXME: Add CSSNumericFactory::vi & ::vb
case CSSUnitType::CSS_VMIN:
return Ref<CSSStyleValue> { CSSNumericFactory::vmin(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_VMAX:
return Ref<CSSStyleValue> { CSSNumericFactory::vmax(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CM:
return Ref<CSSStyleValue> { CSSNumericFactory::cm(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_MM:
return Ref<CSSStyleValue> { CSSNumericFactory::mm(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_Q:
return Ref<CSSStyleValue> { CSSNumericFactory::q(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_IN:
return Ref<CSSStyleValue> { CSSNumericFactory::in(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_PT:
return Ref<CSSStyleValue> { CSSNumericFactory::pt(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_PC:
return Ref<CSSStyleValue> { CSSNumericFactory::pc(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_PX:
return Ref<CSSStyleValue> { CSSNumericFactory::px(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_DEG:
return Ref<CSSStyleValue> { CSSNumericFactory::deg(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_GRAD:
return Ref<CSSStyleValue> { CSSNumericFactory::grad(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_RAD:
return Ref<CSSStyleValue> { CSSNumericFactory::rad(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_TURN:
return Ref<CSSStyleValue> { CSSNumericFactory::turn(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_S:
return Ref<CSSStyleValue> { CSSNumericFactory::s(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_MS:
return Ref<CSSStyleValue> { CSSNumericFactory::ms(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_HZ:
return Ref<CSSStyleValue> { CSSNumericFactory::hz(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_KHZ:
return Ref<CSSStyleValue> { CSSNumericFactory::kHz(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_DPI:
return Ref<CSSStyleValue> { CSSNumericFactory::dpi(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_DPCM:
return Ref<CSSStyleValue> { CSSNumericFactory::dpcm(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_DPPX:
return Ref<CSSStyleValue> { CSSNumericFactory::dppx(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_FR:
return Ref<CSSStyleValue> { CSSNumericFactory::fr(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQW:
return Ref<CSSStyleValue> { CSSNumericFactory::cqw(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQH:
return Ref<CSSStyleValue> { CSSNumericFactory::cqh(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQI:
return Ref<CSSStyleValue> { CSSNumericFactory::cqi(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQB:
return Ref<CSSStyleValue> { CSSNumericFactory::cqb(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQMIN:
return Ref<CSSStyleValue> { CSSNumericFactory::cqmin(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_CQMAX:
return Ref<CSSStyleValue> { CSSNumericFactory::cqmax(primitiveValue->doubleValue()) };
case CSSUnitType::CSS_STRING: {
auto value = CSSKeywordValue::create(primitiveValue->stringValue());
if (value.hasException())
return value.releaseException();
return Ref<CSSStyleValue> { value.releaseReturnValue() };
}
default:
break;
}
} else if (is<CSSImageValue>(cssValue))
return Ref<CSSStyleValue> { CSSStyleImageValue::create(downcast<CSSImageValue>(cssValue.get()), document) };
else if (is<CSSVariableReferenceValue>(cssValue)) {
return Ref<CSSStyleValue> { CSSUnparsedValue::create(downcast<CSSVariableReferenceValue>(cssValue.get()).data().tokenRange()) };
} else if (is<CSSPendingSubstitutionValue>(cssValue)) {
return Ref<CSSStyleValue> { CSSUnparsedValue::create(downcast<CSSPendingSubstitutionValue>(cssValue.get()).shorthandValue().data().tokenRange()) };
} else if (is<CSSCustomPropertyValue>(cssValue)) {
// FIXME: remove CSSStyleValue::create(WTFMove(cssValue)), add reification control flow
return WTF::switchOn(downcast<CSSCustomPropertyValue>(cssValue.get()).value(), [&](const std::monostate&) {
return ExceptionOr<Ref<CSSStyleValue>> { CSSStyleValue::create(WTFMove(cssValue)) };
}, [&](const Ref<CSSVariableReferenceValue>& value) {
return reifyValue(value, document);
}, [&](Ref<CSSVariableData>& value) {
return reifyValue(CSSVariableReferenceValue::create(WTFMove(value)));
}, [&](const CSSValueID&) {
return ExceptionOr<Ref<CSSStyleValue>> { CSSStyleValue::create(WTFMove(cssValue)) };
}, [&](auto&) {
CSSTokenizer tokenizer(downcast<CSSCustomPropertyValue>(cssValue.get()).customCSSText());
return ExceptionOr<Ref<CSSStyleValue>> { CSSUnparsedValue::create(tokenizer.tokenRange()) };
});
} else if (is<CSSValueList>(cssValue)) {
// Reifying the first value in value list.
// FIXME: Verify this is the expected behavior.
// Refer to LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/the-stylepropertymap/inline/get.html
auto valueList = downcast<CSSValueList>(cssValue.ptr());
if (!valueList->length())
return Exception { TypeError, "The CSSValueList should not be empty."_s };
return reifyValue(*valueList->begin(), document);
}
return CSSStyleValue::create(WTFMove(cssValue));
}
} // namespace WebCore
#endif