File: CSSCustomPropertyValue.cpp

package info (click to toggle)
webkit2gtk 2.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 445,780 kB
  • sloc: cpp: 3,798,013; javascript: 197,914; ansic: 161,337; python: 49,141; asm: 21,990; ruby: 18,540; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 298
file content (187 lines) | stat: -rw-r--r-- 6,661 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2016-2021 Apple Inc. All rights reserved.
 * 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,
 * 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 "CSSSerializationContext.h"
#include <wtf/NeverDestroyed.h>

namespace WebCore {

Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createEmpty(const AtomString& name)
{
    static NeverDestroyed<Ref<CSSVariableData>> empty { CSSVariableData::create({ }) };
    return createSyntaxAll(name, Ref { empty.get() });
}

Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createUnresolved(const AtomString& name, Ref<CSSVariableReferenceValue>&& value)
{
    return adoptRef(*new CSSCustomPropertyValue(name, VariantValue { WTF::InPlaceType<Ref<CSSVariableReferenceValue>>, WTFMove(value) }));
}

Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createSyntaxAll(const AtomString& name, Ref<CSSVariableData>&& value)
{
    return adoptRef(*new CSSCustomPropertyValue(name, VariantValue { WTF::InPlaceType<Ref<CSSVariableData>>, WTFMove(value) }));
}

Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createWithCSSWideKeyword(const AtomString& name, CSSWideKeyword keyword)
{
    return adoptRef(*new CSSCustomPropertyValue(name, VariantValue { keyword }));
}

bool CSSCustomPropertyValue::isVariableReference() const
{
    return std::holds_alternative<Ref<CSSVariableReferenceValue>>(m_value);
}

bool CSSCustomPropertyValue::isVariableData() const
{
    return std::holds_alternative<Ref<CSSVariableData>>(m_value);
}

bool CSSCustomPropertyValue::isCSSWideKeyword() const
{
    return std::holds_alternative<CSSWideKeyword>(m_value);
}

std::optional<CSSWideKeyword> CSSCustomPropertyValue::tryCSSWideKeyword() const
{
    if (auto* keyword = std::get_if<CSSWideKeyword>(&m_value))
        return *keyword;
    return { };
}

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 Ref<CSSVariableReferenceValue>& value) {
            return arePointingToEqualData(value, std::get<Ref<CSSVariableReferenceValue>>(other.m_value));
        },
        [&](const Ref<CSSVariableData>& value) {
            return arePointingToEqualData(value, std::get<Ref<CSSVariableData>>(other.m_value));
        },
        [&](const CSSWideKeyword& keyword) {
            return keyword == std::get<CSSWideKeyword>(other.m_value);
        }
    );
}

String CSSCustomPropertyValue::customCSSText(const CSS::SerializationContext& context) const
{
    auto serialize = [&] {
        return WTF::switchOn(m_value,
            [&](const Ref<CSSVariableReferenceValue>& value) {
                return value->cssText(context);
            },
            [&](const Ref<CSSVariableData>& value) {
                return value->serialize();
            },
            [&](const CSSWideKeyword& value) {
                return nameStringForSerialization(toValueID(value)).string();
            }
        );
    };

    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 Ref<CSSVariableReferenceValue>&) -> const Vector<CSSParserToken>& {
            ASSERT_NOT_REACHED();
            return emptyTokens;
        },
        [&](const Ref<CSSVariableData>& value) -> const Vector<CSSParserToken>& {
            return value->tokens();
        },
        [&](const CSSWideKeyword&) -> const Vector<CSSParserToken>& {
            // Do nothing.
            return emptyTokens;
        }
    );
}

Ref<const CSSVariableData> CSSCustomPropertyValue::asVariableData() const
{
    return WTF::switchOn(m_value,
        [&](const Ref<CSSVariableReferenceValue>& value) -> Ref<const CSSVariableData> {
            return value->data();
        },
        [&](const Ref<CSSVariableData>& value) -> Ref<const CSSVariableData> {
            return value.get();
        },
        [&](const CSSWideKeyword&) -> Ref<const CSSVariableData> {
            return CSSVariableData::create(tokens());
        }
    );
}

bool CSSCustomPropertyValue::isCurrentColor() const
{
    // FIXME: Registered properties?
    auto tokenRange = switchOn(m_value,
        [&](const Ref<CSSVariableReferenceValue>& variableReferenceValue) {
            return variableReferenceValue->data().tokenRange();
        },
        [&](const Ref<CSSVariableData>& data) {
            return data->tokenRange();
        },
        [&](const CSSWideKeyword&) {
            return CSSParserTokenRange { };
        }
    );

    if (tokenRange.atEnd())
        return false;

    auto token = tokenRange.consumeIncludingWhitespace();
    if (!tokenRange.atEnd())
        return false;

    // FIXME: This should probably check all tokens.
    return token.id() == CSSValueCurrentcolor;
}

IterationStatus CSSCustomPropertyValue::customVisitChildren(NOESCAPE const Function<IterationStatus(CSSValue&)>& func) const
{
    if (auto* value = std::get_if<Ref<CSSVariableReferenceValue>>(&m_value)) {
        if (func(*value) == IterationStatus::Done)
            return IterationStatus::Done;
    }
    return IterationStatus::Continue;
}

} // namespace WebCore