1 | // Copyright 2014 The Chromium Authors. All rights reserved.
|
---|
2 | // Copyright (C) 2016-2021 Apple Inc. All rights reserved.
|
---|
3 | //
|
---|
4 | // Redistribution and use in source and binary forms, with or without
|
---|
5 | // modification, are permitted provided that the following conditions are
|
---|
6 | // met:
|
---|
7 | //
|
---|
8 | // * Redistributions of source code must retain the above copyright
|
---|
9 | // notice, this list of conditions and the following disclaimer.
|
---|
10 | // * Redistributions in binary form must reproduce the above
|
---|
11 | // copyright notice, this list of conditions and the following disclaimer
|
---|
12 | // in the documentation and/or other materials provided with the
|
---|
13 | // distribution.
|
---|
14 | // * Neither the name of Google Inc. nor the names of its
|
---|
15 | // contributors may be used to endorse or promote products derived from
|
---|
16 | // this software without specific prior written permission.
|
---|
17 | //
|
---|
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 |
|
---|
30 | #pragma once
|
---|
31 |
|
---|
32 | #include "CSSPrimitiveValue.h"
|
---|
33 | #include <wtf/text/StringView.h>
|
---|
34 |
|
---|
35 | namespace WebCore {
|
---|
36 |
|
---|
37 | enum CSSParserTokenType {
|
---|
38 | IdentToken = 0,
|
---|
39 | FunctionToken,
|
---|
40 | AtKeywordToken,
|
---|
41 | HashToken,
|
---|
42 | UrlToken,
|
---|
43 | BadUrlToken,
|
---|
44 | DelimiterToken,
|
---|
45 | NumberToken,
|
---|
46 | PercentageToken,
|
---|
47 | DimensionToken,
|
---|
48 | IncludeMatchToken,
|
---|
49 | DashMatchToken,
|
---|
50 | PrefixMatchToken,
|
---|
51 | SuffixMatchToken,
|
---|
52 | SubstringMatchToken,
|
---|
53 | ColumnToken,
|
---|
54 | WhitespaceToken,
|
---|
55 | CDOToken,
|
---|
56 | CDCToken,
|
---|
57 | ColonToken,
|
---|
58 | SemicolonToken,
|
---|
59 | CommaToken,
|
---|
60 | LeftParenthesisToken,
|
---|
61 | RightParenthesisToken,
|
---|
62 | LeftBracketToken,
|
---|
63 | RightBracketToken,
|
---|
64 | LeftBraceToken,
|
---|
65 | RightBraceToken,
|
---|
66 | StringToken,
|
---|
67 | BadStringToken,
|
---|
68 | EOFToken,
|
---|
69 | CommentToken,
|
---|
70 | LastCSSParserTokenType = CommentToken,
|
---|
71 | };
|
---|
72 |
|
---|
73 | constexpr std::underlying_type_t<CSSParserTokenType> numberOfCSSParserTokenTypes = LastCSSParserTokenType + 1;
|
---|
74 |
|
---|
75 | enum NumericSign {
|
---|
76 | NoSign,
|
---|
77 | PlusSign,
|
---|
78 | MinusSign,
|
---|
79 | };
|
---|
80 |
|
---|
81 | enum NumericValueType {
|
---|
82 | IntegerValueType,
|
---|
83 | NumberValueType,
|
---|
84 | };
|
---|
85 |
|
---|
86 | enum HashTokenType {
|
---|
87 | HashTokenId,
|
---|
88 | HashTokenUnrestricted,
|
---|
89 | };
|
---|
90 |
|
---|
91 | class CSSParserToken {
|
---|
92 | WTF_MAKE_FAST_ALLOCATED;
|
---|
93 | public:
|
---|
94 | enum BlockType {
|
---|
95 | NotBlock,
|
---|
96 | BlockStart,
|
---|
97 | BlockEnd,
|
---|
98 | };
|
---|
99 |
|
---|
100 | CSSParserToken(CSSParserTokenType, BlockType = NotBlock);
|
---|
101 | CSSParserToken(CSSParserTokenType, StringView, BlockType = NotBlock);
|
---|
102 |
|
---|
103 | CSSParserToken(CSSParserTokenType, UChar); // for DelimiterToken
|
---|
104 | CSSParserToken(double, NumericValueType, NumericSign, StringView originalText); // for NumberToken
|
---|
105 |
|
---|
106 | CSSParserToken(HashTokenType, StringView);
|
---|
107 |
|
---|
108 | static CSSUnitType stringToUnitType(StringView);
|
---|
109 |
|
---|
110 | bool operator==(const CSSParserToken& other) const;
|
---|
111 | bool operator!=(const CSSParserToken& other) const { return !(*this == other); }
|
---|
112 |
|
---|
113 | // Converts NumberToken to DimensionToken.
|
---|
114 | void convertToDimensionWithUnit(StringView);
|
---|
115 |
|
---|
116 | // Converts NumberToken to PercentageToken.
|
---|
117 | void convertToPercentage();
|
---|
118 |
|
---|
119 | CSSParserTokenType type() const { return static_cast<CSSParserTokenType>(m_type); }
|
---|
120 | StringView value() const
|
---|
121 | {
|
---|
122 | if (m_valueIs8Bit)
|
---|
123 | return StringView(static_cast<const LChar*>(m_valueDataCharRaw), m_valueLength);
|
---|
124 | return StringView(static_cast<const UChar*>(m_valueDataCharRaw), m_valueLength);
|
---|
125 | }
|
---|
126 |
|
---|
127 | UChar delimiter() const;
|
---|
128 | NumericSign numericSign() const;
|
---|
129 | NumericValueType numericValueType() const;
|
---|
130 | double numericValue() const;
|
---|
131 | StringView originalText() const;
|
---|
132 | HashTokenType getHashTokenType() const { ASSERT(m_type == HashToken); return m_hashTokenType; }
|
---|
133 | BlockType getBlockType() const { return static_cast<BlockType>(m_blockType); }
|
---|
134 | CSSUnitType unitType() const { return static_cast<CSSUnitType>(m_unit); }
|
---|
135 | StringView unitString() const;
|
---|
136 | CSSValueID id() const;
|
---|
137 | CSSValueID functionId() const;
|
---|
138 |
|
---|
139 | bool hasStringBacking() const;
|
---|
140 |
|
---|
141 | CSSPropertyID parseAsCSSPropertyID() const;
|
---|
142 |
|
---|
143 | void serialize(StringBuilder&, const CSSParserToken* nextToken = nullptr) const;
|
---|
144 |
|
---|
145 | CSSParserToken copyWithUpdatedString(StringView) const;
|
---|
146 |
|
---|
147 | private:
|
---|
148 | void initValueFromStringView(StringView string)
|
---|
149 | {
|
---|
150 | m_valueLength = string.length();
|
---|
151 | m_valueIs8Bit = string.is8Bit();
|
---|
152 | m_valueDataCharRaw = m_valueIs8Bit ? static_cast<const void*>(string.characters8()) : static_cast<const void*>(string.characters16());
|
---|
153 | }
|
---|
154 | unsigned m_type : 6; // CSSParserTokenType
|
---|
155 | unsigned m_blockType : 2; // BlockType
|
---|
156 | unsigned m_numericValueType : 1; // NumericValueType
|
---|
157 | unsigned m_numericSign : 2; // NumericSign
|
---|
158 | unsigned m_unit : 7; // CSSUnitType
|
---|
159 | unsigned m_nonUnitPrefixLength : 4; // Only for DimensionType, only needs to be long enough for UnicodeRange parsing.
|
---|
160 |
|
---|
161 | // m_value... is an unpacked StringView so that we can pack it
|
---|
162 | // tightly with the rest of this object for a smaller object size.
|
---|
163 | bool m_valueIs8Bit : 1;
|
---|
164 | unsigned m_valueLength;
|
---|
165 | const void* m_valueDataCharRaw; // Either LChar* or UChar*.
|
---|
166 |
|
---|
167 | union {
|
---|
168 | UChar m_delimiter;
|
---|
169 | HashTokenType m_hashTokenType;
|
---|
170 | double m_numericValue;
|
---|
171 | mutable int m_id;
|
---|
172 | };
|
---|
173 | };
|
---|
174 |
|
---|
175 | } // namespace WebCore
|
---|