source: webkit/trunk/Source/WebCore/css/parser/CSSParserToken.h

Last change on this file was 292150, checked in by [email protected], 3 years ago

Implement units for CSS Typed OM
https://bugs.webkit.org/show_bug.cgi?id=238532

Patch by Alex Christensen <[email protected]> on 2022-03-31
Reviewed by Simon Fraser.

LayoutTests/imported/w3c:

  • web-platform-tests/css/css-typed-om/stylevalue-serialization/cssUnitValue.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/add-two-types.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/arithmetic.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/create-a-type.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathInvert-type-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathNegate-type-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathValue.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssUnitValue-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssnumericvalue-multiply-two-types.tentative-expected.txt:
  • web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/to.tentative-expected.txt:

Source/WebCore:

This feature is off by default and this part is covered by WPT tests.

  • css/typedom/CSSNumericFactory.h:
  • css/typedom/CSSNumericValue.cpp:

(WebCore::negate):
(WebCore::invert):
(WebCore::operationOnValuesOfSameUnit):
(WebCore::CSSNumericValue::addInternal):
(WebCore::CSSNumericValue::add):
(WebCore::CSSNumericValue::sub):
(WebCore::CSSNumericValue::multiplyInternal):
(WebCore::CSSNumericValue::mul):
(WebCore::CSSNumericValue::to):
(WebCore::CSSNumericValue::toSum):
(WebCore::CSSNumericValue::type): Deleted.

  • css/typedom/CSSNumericValue.h:

(WebCore::CSSNumericValue::type const):
(WebCore::CSSNumericValue::CSSNumericValue):

  • css/typedom/CSSUnitValue.cpp:

(WebCore::numericType):
(WebCore::parseUnit):
(WebCore::CSSUnitValue::unit const):
(WebCore::CSSUnitValue::unitSerialization const):
(WebCore::CSSUnitValue::create):
(WebCore::CSSUnitValue::CSSUnitValue):

  • css/typedom/CSSUnitValue.h:
  • css/typedom/numeric/CSSMathInvert.cpp:

(WebCore::negatedType):
(WebCore::CSSMathInvert::CSSMathInvert):

  • css/typedom/numeric/CSSMathNegate.cpp:

(WebCore::copyType):
(WebCore::CSSMathNegate::CSSMathNegate):

  • css/typedom/numeric/CSSMathProduct.cpp:

(WebCore::multiplyTypes):
(WebCore::CSSMathProduct::create):
(WebCore::CSSMathProduct::CSSMathProduct):
(WebCore::CSSMathProduct::values const): Deleted.

  • css/typedom/numeric/CSSMathProduct.h:
  • css/typedom/numeric/CSSMathSum.cpp:

(WebCore::addTypes):
(WebCore::CSSMathSum::create):
(WebCore::CSSMathSum::CSSMathSum):

  • css/typedom/numeric/CSSMathSum.h:
  • css/typedom/numeric/CSSMathValue.h:

(WebCore::CSSMathValue::CSSMathValue):

  • css/typedom/numeric/CSSNumericArray.cpp:

(WebCore::CSSNumericArray::create):

  • css/typedom/numeric/CSSNumericBaseType.h:

(WebCore::eachBaseType):
(WebCore::debugString):
(): Deleted.

  • css/typedom/numeric/CSSNumericType.h:

(WebCore::CSSNumericType::operator== const):
(WebCore::CSSNumericType::valueForType):
(WebCore::CSSNumericType::applyPercentHint):
(WebCore::CSSNumericType::debugString const):
(): Deleted.

  • css/typedom/numeric/CSSNumericType.idl:
File size: 5.7 KB
Line 
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
35namespace WebCore {
36
37enum 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
73constexpr std::underlying_type_t<CSSParserTokenType> numberOfCSSParserTokenTypes = LastCSSParserTokenType + 1;
74
75enum NumericSign {
76 NoSign,
77 PlusSign,
78 MinusSign,
79};
80
81enum NumericValueType {
82 IntegerValueType,
83 NumberValueType,
84};
85
86enum HashTokenType {
87 HashTokenId,
88 HashTokenUnrestricted,
89};
90
91class CSSParserToken {
92 WTF_MAKE_FAST_ALLOCATED;
93public:
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
147private:
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
Note: See TracBrowser for help on using the repository browser.