| 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Copyright (C) 2016 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 | UnicodeRangeToken, |
| 55 | WhitespaceToken, |
| 56 | CDOToken, |
| 57 | CDCToken, |
| 58 | ColonToken, |
| 59 | SemicolonToken, |
| 60 | CommaToken, |
| 61 | LeftParenthesisToken, |
| 62 | RightParenthesisToken, |
| 63 | LeftBracketToken, |
| 64 | RightBracketToken, |
| 65 | LeftBraceToken, |
| 66 | RightBraceToken, |
| 67 | StringToken, |
| 68 | BadStringToken, |
| 69 | EOFToken, |
| 70 | , |
| 71 | }; |
| 72 | |
| 73 | enum NumericSign { |
| 74 | NoSign, |
| 75 | PlusSign, |
| 76 | MinusSign, |
| 77 | }; |
| 78 | |
| 79 | enum NumericValueType { |
| 80 | IntegerValueType, |
| 81 | NumberValueType, |
| 82 | }; |
| 83 | |
| 84 | enum HashTokenType { |
| 85 | HashTokenId, |
| 86 | HashTokenUnrestricted, |
| 87 | }; |
| 88 | |
| 89 | class CSSParserToken { |
| 90 | WTF_MAKE_FAST_ALLOCATED; |
| 91 | public: |
| 92 | enum BlockType { |
| 93 | NotBlock, |
| 94 | BlockStart, |
| 95 | BlockEnd, |
| 96 | }; |
| 97 | |
| 98 | CSSParserToken(CSSParserTokenType, BlockType = NotBlock); |
| 99 | CSSParserToken(CSSParserTokenType, StringView, BlockType = NotBlock); |
| 100 | |
| 101 | CSSParserToken(CSSParserTokenType, UChar); // for DelimiterToken |
| 102 | CSSParserToken(CSSParserTokenType, double, NumericValueType, NumericSign); // for NumberToken |
| 103 | CSSParserToken(CSSParserTokenType, UChar32, UChar32); // for UnicodeRangeToken |
| 104 | |
| 105 | CSSParserToken(HashTokenType, StringView); |
| 106 | |
| 107 | bool operator==(const CSSParserToken& other) const; |
| 108 | bool operator!=(const CSSParserToken& other) const { return !(*this == other); } |
| 109 | |
| 110 | // Converts NumberToken to DimensionToken. |
| 111 | void convertToDimensionWithUnit(StringView); |
| 112 | |
| 113 | // Converts NumberToken to PercentageToken. |
| 114 | void convertToPercentage(); |
| 115 | |
| 116 | CSSParserTokenType type() const { return static_cast<CSSParserTokenType>(m_type); } |
| 117 | StringView value() const |
| 118 | { |
| 119 | if (m_valueIs8Bit) |
| 120 | return StringView(static_cast<const LChar*>(m_valueDataCharRaw), m_valueLength); |
| 121 | return StringView(static_cast<const UChar*>(m_valueDataCharRaw), m_valueLength); |
| 122 | } |
| 123 | |
| 124 | UChar delimiter() const; |
| 125 | NumericSign numericSign() const; |
| 126 | NumericValueType numericValueType() const; |
| 127 | double numericValue() const; |
| 128 | HashTokenType getHashTokenType() const { ASSERT(m_type == HashToken); return m_hashTokenType; } |
| 129 | BlockType getBlockType() const { return static_cast<BlockType>(m_blockType); } |
| 130 | CSSPrimitiveValue::UnitType unitType() const { return static_cast<CSSPrimitiveValue::UnitType>(m_unit); } |
| 131 | UChar32 unicodeRangeStart() const { ASSERT(m_type == UnicodeRangeToken); return m_unicodeRange.start; } |
| 132 | UChar32 unicodeRangeEnd() const { ASSERT(m_type == UnicodeRangeToken); return m_unicodeRange.end; } |
| 133 | CSSValueID id() const; |
| 134 | CSSValueID functionId() const; |
| 135 | |
| 136 | bool hasStringBacking() const; |
| 137 | |
| 138 | CSSPropertyID parseAsCSSPropertyID() const; |
| 139 | |
| 140 | void serialize(StringBuilder&) const; |
| 141 | |
| 142 | CSSParserToken copyWithUpdatedString(const StringView&) const; |
| 143 | |
| 144 | private: |
| 145 | void initValueFromStringView(StringView string) |
| 146 | { |
| 147 | m_valueLength = string.length(); |
| 148 | m_valueIs8Bit = string.is8Bit(); |
| 149 | m_valueDataCharRaw = m_valueIs8Bit ? const_cast<void*>(static_cast<const void*>(string.characters8())) : const_cast<void*>(static_cast<const void*>(string.characters16())); |
| 150 | } |
| 151 | unsigned m_type : 6; // CSSParserTokenType |
| 152 | unsigned m_blockType : 2; // BlockType |
| 153 | unsigned m_numericValueType : 1; // NumericValueType |
| 154 | unsigned m_numericSign : 2; // NumericSign |
| 155 | unsigned m_unit : 7; // CSSPrimitiveValue::UnitType |
| 156 | |
| 157 | bool valueDataCharRawEqual(const CSSParserToken& other) const; |
| 158 | |
| 159 | // m_value... is an unpacked StringView so that we can pack it |
| 160 | // tightly with the rest of this object for a smaller object size. |
| 161 | bool m_valueIs8Bit : 1; |
| 162 | unsigned m_valueLength; |
| 163 | void* m_valueDataCharRaw; // Either LChar* or UChar*. |
| 164 | |
| 165 | union { |
| 166 | UChar m_delimiter; |
| 167 | HashTokenType m_hashTokenType; |
| 168 | double m_numericValue; |
| 169 | mutable int m_id; |
| 170 | |
| 171 | struct { |
| 172 | UChar32 start; |
| 173 | UChar32 end; |
| 174 | } m_unicodeRange; |
| 175 | }; |
| 176 | }; |
| 177 | |
| 178 | } // namespace WebCore |
| 179 | |