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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/parser/CSSParserToken.h"
#include "core/css/CSSMarkup.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/parser/CSSPropertyParser.h"
#include "wtf/HashMap.h"
#include "wtf/text/StringBuilder.h"
#include <limits.h>
namespace blink {
CSSParserToken::CSSParserToken(CSSParserTokenType type, BlockType blockType)
: m_type(type), m_blockType(blockType) {}
// Just a helper used for Delimiter tokens.
CSSParserToken::CSSParserToken(CSSParserTokenType type, UChar c)
: m_type(type), m_blockType(NotBlock), m_delimiter(c) {
ASSERT(m_type == DelimiterToken);
}
CSSParserToken::CSSParserToken(CSSParserTokenType type,
StringView value,
BlockType blockType)
: m_type(type), m_blockType(blockType) {
initValueFromStringView(value);
m_id = -1;
}
CSSParserToken::CSSParserToken(CSSParserTokenType type,
double numericValue,
NumericValueType numericValueType,
NumericSign sign)
: m_type(type),
m_blockType(NotBlock),
m_numericValueType(numericValueType),
m_numericSign(sign),
m_unit(static_cast<unsigned>(CSSPrimitiveValue::UnitType::Number)) {
ASSERT(type == NumberToken);
m_numericValue =
clampTo<double>(numericValue, -std::numeric_limits<float>::max(),
std::numeric_limits<float>::max());
}
CSSParserToken::CSSParserToken(CSSParserTokenType type,
UChar32 start,
UChar32 end)
: m_type(UnicodeRangeToken), m_blockType(NotBlock) {
DCHECK_EQ(type, UnicodeRangeToken);
m_unicodeRange.start = start;
m_unicodeRange.end = end;
}
CSSParserToken::CSSParserToken(HashTokenType type, StringView value)
: m_type(HashToken), m_blockType(NotBlock), m_hashTokenType(type) {
initValueFromStringView(value);
}
void CSSParserToken::convertToDimensionWithUnit(StringView unit) {
ASSERT(m_type == NumberToken);
m_type = DimensionToken;
initValueFromStringView(unit);
m_unit = static_cast<unsigned>(CSSPrimitiveValue::stringToUnitType(unit));
}
void CSSParserToken::convertToPercentage() {
ASSERT(m_type == NumberToken);
m_type = PercentageToken;
m_unit = static_cast<unsigned>(CSSPrimitiveValue::UnitType::Percentage);
}
UChar CSSParserToken::delimiter() const {
ASSERT(m_type == DelimiterToken);
return m_delimiter;
}
NumericSign CSSParserToken::numericSign() const {
// This is valid for DimensionToken and PercentageToken, but only used
// in <an+b> parsing on NumberTokens.
ASSERT(m_type == NumberToken);
return static_cast<NumericSign>(m_numericSign);
}
NumericValueType CSSParserToken::numericValueType() const {
ASSERT(m_type == NumberToken || m_type == PercentageToken ||
m_type == DimensionToken);
return static_cast<NumericValueType>(m_numericValueType);
}
double CSSParserToken::numericValue() const {
ASSERT(m_type == NumberToken || m_type == PercentageToken ||
m_type == DimensionToken);
return m_numericValue;
}
CSSPropertyID CSSParserToken::parseAsUnresolvedCSSPropertyID() const {
ASSERT(m_type == IdentToken);
return unresolvedCSSPropertyID(value());
}
CSSValueID CSSParserToken::id() const {
if (m_type != IdentToken)
return CSSValueInvalid;
if (m_id < 0)
m_id = cssValueKeywordID(value());
return static_cast<CSSValueID>(m_id);
}
CSSValueID CSSParserToken::functionId() const {
if (m_type != FunctionToken)
return CSSValueInvalid;
if (m_id < 0)
m_id = cssValueKeywordID(value());
return static_cast<CSSValueID>(m_id);
}
bool CSSParserToken::hasStringBacking() const {
CSSParserTokenType tokenType = type();
return tokenType == IdentToken || tokenType == FunctionToken ||
tokenType == AtKeywordToken || tokenType == HashToken ||
tokenType == UrlToken || tokenType == DimensionToken ||
tokenType == StringToken;
}
CSSParserToken CSSParserToken::copyWithUpdatedString(
const StringView& string) const {
CSSParserToken copy(*this);
copy.initValueFromStringView(string);
return copy;
}
bool CSSParserToken::valueDataCharRawEqual(const CSSParserToken& other) const {
if (m_valueLength != other.m_valueLength)
return false;
if (m_valueDataCharRaw == other.m_valueDataCharRaw &&
m_valueIs8Bit == other.m_valueIs8Bit)
return true;
if (m_valueIs8Bit) {
return other.m_valueIs8Bit
? equal(static_cast<const LChar*>(m_valueDataCharRaw),
static_cast<const LChar*>(other.m_valueDataCharRaw),
m_valueLength)
: equal(static_cast<const LChar*>(m_valueDataCharRaw),
static_cast<const UChar*>(other.m_valueDataCharRaw),
m_valueLength);
} else {
return other.m_valueIs8Bit
? equal(static_cast<const UChar*>(m_valueDataCharRaw),
static_cast<const LChar*>(other.m_valueDataCharRaw),
m_valueLength)
: equal(static_cast<const UChar*>(m_valueDataCharRaw),
static_cast<const UChar*>(other.m_valueDataCharRaw),
m_valueLength);
}
}
bool CSSParserToken::operator==(const CSSParserToken& other) const {
if (m_type != other.m_type)
return false;
switch (m_type) {
case DelimiterToken:
return delimiter() == other.delimiter();
case HashToken:
if (m_hashTokenType != other.m_hashTokenType)
return false;
// fallthrough
case IdentToken:
case FunctionToken:
case StringToken:
case UrlToken:
return valueDataCharRawEqual(other);
case DimensionToken:
if (!valueDataCharRawEqual(other))
return false;
// fallthrough
case NumberToken:
case PercentageToken:
return m_numericSign == other.m_numericSign &&
m_numericValue == other.m_numericValue &&
m_numericValueType == other.m_numericValueType;
case UnicodeRangeToken:
return m_unicodeRange.start == other.m_unicodeRange.start &&
m_unicodeRange.end == other.m_unicodeRange.end;
default:
return true;
}
}
void CSSParserToken::serialize(StringBuilder& builder) const {
// This is currently only used for @supports CSSOM. To keep our implementation
// simple we handle some of the edge cases incorrectly (see comments below).
switch (type()) {
case IdentToken:
serializeIdentifier(value().toString(), builder);
break;
case FunctionToken:
serializeIdentifier(value().toString(), builder);
return builder.append('(');
case AtKeywordToken:
builder.append('@');
serializeIdentifier(value().toString(), builder);
break;
case HashToken:
builder.append('#');
serializeIdentifier(value().toString(), builder,
(getHashTokenType() == HashTokenUnrestricted));
break;
case UrlToken:
builder.append("url(");
serializeIdentifier(value().toString(), builder);
return builder.append(')');
case DelimiterToken:
if (delimiter() == '\\')
return builder.append("\\\n");
return builder.append(delimiter());
case NumberToken:
// These won't properly preserve the NumericValueType flag
return builder.appendNumber(numericValue());
case PercentageToken:
builder.appendNumber(numericValue());
return builder.append('%');
case DimensionToken:
// This will incorrectly serialize e.g. 4e3e2 as 4000e2
builder.appendNumber(numericValue());
serializeIdentifier(value().toString(), builder);
break;
case UnicodeRangeToken:
return builder.append(
String::format("U+%X-%X", unicodeRangeStart(), unicodeRangeEnd()));
case StringToken:
return serializeString(value().toString(), builder);
case IncludeMatchToken:
return builder.append("~=");
case DashMatchToken:
return builder.append("|=");
case PrefixMatchToken:
return builder.append("^=");
case SuffixMatchToken:
return builder.append("$=");
case SubstringMatchToken:
return builder.append("*=");
case ColumnToken:
return builder.append("||");
case CDOToken:
return builder.append("<!--");
case CDCToken:
return builder.append("-->");
case BadStringToken:
return builder.append("'\n");
case BadUrlToken:
return builder.append("url(()");
case WhitespaceToken:
return builder.append(' ');
case ColonToken:
return builder.append(':');
case SemicolonToken:
return builder.append(';');
case CommaToken:
return builder.append(',');
case LeftParenthesisToken:
return builder.append('(');
case RightParenthesisToken:
return builder.append(')');
case LeftBracketToken:
return builder.append('[');
case RightBracketToken:
return builder.append(']');
case LeftBraceToken:
return builder.append('{');
case RightBraceToken:
return builder.append('}');
case EOFToken:
case CommentToken:
ASSERT_NOT_REACHED();
return;
}
}
} // namespace blink
|