File: StyleProperties.h

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 (170 lines) | stat: -rw-r--r-- 6,413 bytes parent folder | download | duplicates (6)
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
/**
 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2004-2023 Apple Inc. All rights reserved.
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#pragma once

#include "CSSProperty.h"

namespace WebCore {

class CachedResource;
class Color;
class ImmutableStyleProperties;
class MutableStyleProperties;

enum CSSValueID : uint16_t;
enum CSSParserMode : uint8_t;

namespace CSS {
struct SerializationContext;
}

DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(StyleProperties);
class StyleProperties : public RefCounted<StyleProperties> {
    WTF_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(StyleProperties);
public:
    // Override RefCounted's deref() to ensure operator delete is called on
    // the appropriate subclass type.
    inline void deref() const;

    class PropertyReference {
    public:
        PropertyReference(const StylePropertyMetadata& metadata, const CSSValue* value)
            : m_metadata(metadata)
            , m_value(value)
        { }

        CSSPropertyID id() const { return static_cast<CSSPropertyID>(m_metadata.m_propertyID); }
        CSSPropertyID shorthandID() const { return m_metadata.shorthandID(); }

        bool isImportant() const { return m_metadata.m_important; }
        bool isImplicit() const { return m_metadata.m_implicit; }

        String cssName() const;
        String cssText(const CSS::SerializationContext&) const;

        const CSSValue* value() const { return m_value; }
        // FIXME: We should try to remove this mutable overload.
        CSSValue* value() { return const_cast<CSSValue*>(m_value); }
        Ref<CSSValue> protectedValue() const { return const_cast<CSSValue&>(*m_value); }

        // FIXME: Remove this.
        CSSProperty toCSSProperty() const { return CSSProperty(id(), protectedValue(), isImportant() ? IsImportant::Yes : IsImportant::No, m_metadata.m_isSetFromShorthand, m_metadata.m_indexInShorthandsVector, isImplicit()); }

    private:
        const StylePropertyMetadata& m_metadata;
        const CSSValue* m_value;
    };

    template<typename T>
    struct Iterator {
        using iterator_category = std::forward_iterator_tag;
        using value_type = PropertyReference;
        using difference_type = ptrdiff_t;
        using pointer = PropertyReference;
        using reference = PropertyReference;

        Iterator(const T& properties)
            : properties { properties }
        {
        }

        PropertyReference operator*() const { return properties.propertyAt(index); }
        Iterator& operator++() { ++index; return *this; }
        bool operator==(std::nullptr_t) const { return index >= properties.propertyCount(); }

    private:
        const T& properties;
        unsigned index { 0 };
    };

    inline unsigned propertyCount() const;
    inline bool isEmpty() const;
    inline PropertyReference propertyAt(unsigned) const;

    Iterator<StyleProperties> begin() const { return { *this }; }
    static constexpr std::nullptr_t end() { return nullptr; }
    inline unsigned size() const;

    WEBCORE_EXPORT RefPtr<CSSValue> getPropertyCSSValue(CSSPropertyID) const;
    WEBCORE_EXPORT String getPropertyValue(CSSPropertyID) const;

    WEBCORE_EXPORT std::optional<Color> propertyAsColor(CSSPropertyID) const;
    WEBCORE_EXPORT std::optional<CSSValueID> propertyAsValueID(CSSPropertyID) const;

    bool propertyIsImportant(CSSPropertyID) const;
    String getPropertyShorthand(CSSPropertyID) const;
    bool isPropertyImplicit(CSSPropertyID) const;

    RefPtr<CSSValue> getCustomPropertyCSSValue(const String& propertyName) const;
    String getCustomPropertyValue(const String& propertyName) const;
    bool customPropertyIsImportant(const String& propertyName) const;

    Ref<MutableStyleProperties> copyBlockProperties() const;

    CSSParserMode cssParserMode() const { return static_cast<CSSParserMode>(m_cssParserMode); }

    WEBCORE_EXPORT Ref<MutableStyleProperties> mutableCopy() const;
    Ref<ImmutableStyleProperties> immutableCopyIfNeeded() const;

    Ref<MutableStyleProperties> copyProperties(std::span<const CSSPropertyID>) const;
    
    String asText(const CSS::SerializationContext&) const;
    AtomString asTextAtom(const CSS::SerializationContext&) const;

    bool hasCSSOMWrapper() const;
    bool isMutable() const { return m_isMutable; }

    bool traverseSubresources(NOESCAPE const Function<bool(const CachedResource&)>& handler) const;
    bool mayDependOnBaseURL() const;

    static unsigned averageSizeInBytes();

#ifndef NDEBUG
    void showStyle();
#endif

    bool propertyMatches(CSSPropertyID, const CSSValue*) const;

    inline int findPropertyIndex(CSSPropertyID) const;
    inline int findCustomPropertyIndex(StringView propertyName) const;

protected:
    inline explicit StyleProperties(CSSParserMode);
    inline StyleProperties(CSSParserMode, unsigned immutableArraySize);

    unsigned m_cssParserMode : 3;
    mutable unsigned m_isMutable : 1 { true };
    unsigned m_arraySize : 28 { 0 };

private:
    StringBuilder asTextInternal(const CSS::SerializationContext&) const;
    String serializeLonghandValue(const CSS::SerializationContext&, CSSPropertyID) const;
    String serializeShorthandValue(const CSS::SerializationContext&, CSSPropertyID) const;
};

String serializeLonghandValue(const CSS::SerializationContext&, CSSPropertyID, const CSSValue&);
inline String serializeLonghandValue(const CSS::SerializationContext&, CSSPropertyID, const CSSValue*);

inline CSSValueID longhandValueID(CSSPropertyID, const CSSValue&);
inline std::optional<CSSValueID> longhandValueID(CSSPropertyID, const CSSValue*);

} // namespace WebCore