File: CSSPropertyParserConsumer%2BEasing.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (411 lines) | stat: -rw-r--r-- 14,634 bytes parent folder | download | duplicates (7)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2024 Apple Inc. All rights reserved.
 * Copyright (C) 2024 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 "CSSPropertyParserConsumer+Easing.h"

#include "CSSEasingFunctionValue.h"
#include "CSSParserContext.h"
#include "CSSParserTokenRange.h"
#include "CSSPropertyParserConsumer+Ident.h"
#include "CSSPropertyParserConsumer+IntegerDefinitions.h"
#include "CSSPropertyParserConsumer+MetaConsumer.h"
#include "CSSPropertyParserConsumer+NumberDefinitions.h"
#include "CSSPropertyParserConsumer+PercentageDefinitions.h"
#include "CSSPropertyParserConsumer+Primitives.h"
#include "CSSValueKeywords.h"
#include "StyleEasingFunction.h"
#include "TimingFunction.h"

namespace WebCore {
namespace CSSPropertyParserHelpers {

// MARK: - <steps()>

static std::optional<CSS::EasingFunction> consumeUnresolvedStepsEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // <steps-easing-function> = steps( <integer>, <steps-easing-function-position>? )
    // <steps-easing-function-position> = jump-start | jump-end | jump-none | jump-both | start | end
    //
    // with range constraints, this is:
    //
    // <steps-easing-function> = steps( <integer [1,∞]>, jump-start )
    //                         | steps( <integer [1,∞]>, jump-end )
    //                         | steps( <integer [1,∞]>, jump-both )
    //                         | steps( <integer [1,∞]>, start )
    //                         | steps( <integer [1,∞]>, end )
    //                         | steps( <integer [2,∞]>, jump-none )
    // https://drafts.csswg.org/css-easing-2/#funcdef-steps

    ASSERT(range.peek().functionId() == CSSValueSteps);
    auto rangeCopy = range;
    auto args = consumeFunction(rangeCopy);

    // Stash args so we can re-parse if we get `jump-none`.
    auto stashedArgs = args;

    auto steps = MetaConsumer<CSS::Integer<CSS::Range{1,CSS::Range::infinity}>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!steps)
        return { };

    std::optional<CSS::StepsEasingParameters> parameters;

    if (consumeCommaIncludingWhitespace(args)) {
        switch (args.consumeIncludingWhitespace().id()) {
        case CSSValueJumpStart:
            parameters = { CSS::StepsEasingParameters::JumpStart { WTFMove(*steps) } };
            break;
        case CSSValueJumpEnd:
            parameters = { CSS::StepsEasingParameters::JumpEnd { WTFMove(*steps) } };
            break;
        case CSSValueJumpNone: {
            // "The first parameter specifies the number of intervals in the function. It must be a
            //  positive integer greater than 0 unless the second parameter is jump-none in which
            //  case it must be a positive integer greater than 1."

            // Re-parse `steps` to account for different type requirement.
            auto stepsJumpNone = MetaConsumer<CSS::Integer<CSS::Range{2,CSS::Range::infinity}>>::consume(stashedArgs, context, { }, { .parserMode = context.mode });
            if (!stepsJumpNone)
                return { };

            parameters = { CSS::StepsEasingParameters::JumpNone { WTFMove(*stepsJumpNone) } };
            break;
        }

        case CSSValueJumpBoth:
            parameters = { CSS::StepsEasingParameters::JumpBoth { WTFMove(*steps) } };
            break;
        case CSSValueStart:
            parameters = { CSS::StepsEasingParameters::Start { WTFMove(*steps) } };
            break;
        case CSSValueEnd:
            parameters = { CSS::StepsEasingParameters::End { WTFMove(*steps) } };
            break;
        default:
            return { };
        }
    } else
        parameters = { CSS::StepsEasingParameters::End { WTFMove(*steps) } };

    if (!args.atEnd())
        return { };

    range = rangeCopy;

    return CSS::EasingFunction {
        CSS::StepsEasingFunction {
            .parameters = WTFMove(*parameters)
        }
    };
}

// MARK: - <linear()>

static std::optional<CSS::LinearEasingParameters::Stop::Length> consumeUnresolvedLinearEasingFunctionStopLength(CSSParserTokenRange& args, const CSSParserContext& context)
{
    // <linear-easing-function-stop-length> = <percentage>{0,2}

    auto input = MetaConsumer<CSS::Percentage<>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!input)
        return { };
    auto extra = MetaConsumer<CSS::Percentage<>>::consume(args, context, { }, { .parserMode = context.mode });

    return CSS::LinearEasingParameters::Stop::Length {
        .input = WTFMove(*input),
        .extra = WTFMove(extra)
    };
}

static std::optional<CSS::LinearEasingParameters::Stop> consumeUnresolvedLinearEasingFunctionStop(CSSParserTokenRange& args, const CSSParserContext& context)
{
    // <linear-easing-function-stop> = <number> && <percentage>{0,2}

    auto output = MetaConsumer<CSS::Number<>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!output)
        return { };
    auto input = consumeUnresolvedLinearEasingFunctionStopLength(args, context);

    return CSS::LinearEasingParameters::Stop {
        .output = WTFMove(*output),
        .input = WTFMove(input)
    };
}

static std::optional<CSS::EasingFunction> consumeUnresolvedLinearEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // <linear()> = linear( [ <number> && <percentage>{0,2} ]# )
    // https://drafts.csswg.org/css-easing-2/#funcdef-linear

    ASSERT(range.peek().functionId() == CSSValueLinear);
    auto rangeCopy = range;
    auto args = consumeFunction(rangeCopy);

    Vector<CSS::LinearEasingParameters::Stop> stops;

    while (true) {
        auto stop = consumeUnresolvedLinearEasingFunctionStop(args, context);
        if (!stop)
            break;

        stops.append(WTFMove(*stop));

        if (!consumeCommaIncludingWhitespace(args))
            break;
    }

    if (!args.atEnd() || stops.size() < 2)
        return { };

    range = rangeCopy;

    return CSS::EasingFunction {
        CSS::LinearEasingFunction {
            .parameters = {
                .stops = { WTFMove(stops) }
            }
        }
    };
}

// MARK: - <cubic-bezier()>

static std::optional<CSS::EasingFunction> consumeUnresolvedCubicBezierEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // <cubic-bezier()> = cubic-bezier( [ <number [0,1]>, <number> ]#{2} )
    // https://drafts.csswg.org/css-easing-2/#funcdef-cubic-bezier

    ASSERT(range.peek().functionId() == CSSValueCubicBezier);
    auto rangeCopy = range;
    auto args = consumeFunction(rangeCopy);

    auto x1 = MetaConsumer<CSS::Number<CSS::ClosedUnitRange>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!x1)
        return { };
    if (!consumeCommaIncludingWhitespace(args))
        return { };
    auto y1 = MetaConsumer<CSS::Number<>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!y1)
        return { };
    if (!consumeCommaIncludingWhitespace(args))
        return { };
    auto x2 = MetaConsumer<CSS::Number<CSS::ClosedUnitRange>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!x2)
        return { };
    if (!consumeCommaIncludingWhitespace(args))
        return { };
    auto y2 = MetaConsumer<CSS::Number<>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!y2)
        return { };

    if (!args.atEnd())
        return { };

    range = rangeCopy;

    return CSS::EasingFunction {
        CSS::CubicBezierEasingFunction {
            .parameters = {
                .value = {
                    CSS::CubicBezierEasingParameters::Coordinate { WTFMove(*x1), WTFMove(*y1) },
                    CSS::CubicBezierEasingParameters::Coordinate { WTFMove(*x2), WTFMove(*y2) },
                }
            }
        }
    };
}

// MARK: - <spring()>

static std::optional<CSS::EasingFunction> consumeUnresolvedSpringEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // <spring()> = spring( <number [>0,∞]> <number [>0,∞]> <number [0,∞]> <number> )
    // Non-standard

    ASSERT(range.peek().functionId() == CSSValueSpring);

    if (!context.springTimingFunctionEnabled)
        return { };

    auto rangeCopy = range;
    auto args = consumeFunction(rangeCopy);

    auto mass = MetaConsumer<CSS::Number<CSS::SpringEasingParameters::Positive>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!mass)
        return { };
    auto stiffness = MetaConsumer<CSS::Number<CSS::SpringEasingParameters::Positive>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!stiffness)
        return { };
    auto damping = MetaConsumer<CSS::Number<CSS::Nonnegative>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!damping)
        return { };
    auto initialVelocity = MetaConsumer<CSS::Number<>>::consume(args, context, { }, { .parserMode = context.mode });
    if (!initialVelocity)
        return { };

    if (!args.atEnd())
        return { };

    range = rangeCopy;

    return CSS::EasingFunction {
        CSS::SpringEasingFunction {
            .parameters = {
                .mass = WTFMove(*mass),
                .stiffness = WTFMove(*stiffness),
                .damping = WTFMove(*damping),
                .initialVelocity = WTFMove(*initialVelocity),
            }
        }
    };
}

// MARK: - <easing-function>

std::optional<CSS::EasingFunction> consumeUnresolvedEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // <easing-function> = linear | ease | ease-in | ease-out | ease-in-out | step-start | step-end | <linear()> | <cubic-bezier()> | <steps()>
    // NOTE: also includes non-standard <spring()>.
    // https://drafts.csswg.org/css-easing/#typedef-easing-function

    switch (range.peek().id()) {
    case CSSValueLinear:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction { CSS::Keyword::Linear { } };
    case CSSValueEase:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction { CSS::Keyword::Ease { } };
    case CSSValueEaseIn:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction { CSS::Keyword::EaseIn { } };
    case CSSValueEaseOut:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction { CSS::Keyword::EaseOut { } };
    case CSSValueEaseInOut:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction { CSS::Keyword::EaseInOut { } };

    case CSSValueStepStart:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction {
            CSS::StepsEasingFunction {
                .parameters = { CSS::StepsEasingParameters::Start { CSS::Integer<CSS::Range{1,CSS::Range::infinity}> { 1 } } }
            }
        };

    case CSSValueStepEnd:
        range.consumeIncludingWhitespace();
        return CSS::EasingFunction {
            CSS::StepsEasingFunction {
                .parameters = { CSS::StepsEasingParameters::End { CSS::Integer<CSS::Range{1,CSS::Range::infinity}> { 1 } } }
            }
        };

    default:
        break;
    }

    switch (range.peek().functionId()) {
    case CSSValueLinear:
        return consumeUnresolvedLinearEasingFunction(range, context);

    case CSSValueCubicBezier:
        return consumeUnresolvedCubicBezierEasingFunction(range, context);

    case CSSValueSteps:
        return consumeUnresolvedStepsEasingFunction(range, context);

    case CSSValueSpring:
        return consumeUnresolvedSpringEasingFunction(range, context);

    default:
        break;
    }

    return { };
}

RefPtr<CSSValue> consumeEasingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
    // Avoid allocation of a CSSEasingFunctionValue when the result is a just a value ID.
    switch (range.peek().id()) {
    case CSSValueLinear:
    case CSSValueEase:
    case CSSValueEaseIn:
    case CSSValueEaseOut:
    case CSSValueEaseInOut:
        return consumeIdent(range);
    default:
        break;
    }

    if (auto value = consumeUnresolvedEasingFunction(range, context))
        return CSSEasingFunctionValue::create(WTFMove(*value));
    return { };
}

RefPtr<TimingFunction> parseEasingFunctionDeprecated(const String& string, const CSSParserContext& context)
{
    CSSTokenizer tokenizer(string);
    CSSParserTokenRange range(tokenizer.tokenRange());

    // Handle leading whitespace.
    range.consumeWhitespace();

    auto result = consumeUnresolvedEasingFunction(range, context);
    if (!result)
        return { };

    // Handle trailing whitespace.
    range.consumeWhitespace();

    if (!range.atEnd())
        return { };

    return Style::createTimingFunctionDeprecated(*result);
}

RefPtr<TimingFunction> parseEasingFunction(const String& string, const CSSParserContext& context, const CSSToLengthConversionData& conversionData)
{
    CSSTokenizer tokenizer(string);
    CSSParserTokenRange range(tokenizer.tokenRange());

    // Handle leading whitespace.
    range.consumeWhitespace();

    auto result = consumeUnresolvedEasingFunction(range, context);
    if (!result)
        return { };

    // Handle trailing whitespace.
    range.consumeWhitespace();

    if (!range.atEnd())
        return { };

    return Style::createTimingFunction(*result, conversionData);
}

} // namespace CSSPropertyParserHelpers
} // namespace WebCore