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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
// 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 "config.h"
#include "core/css/parser/CSSParserImpl.h"
#include "core/css/CSSKeyframesRule.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/StylePropertySet.h"
#include "core/css/StyleRuleImport.h"
#include "core/css/StyleRuleKeyframe.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/CSSParserValues.h"
#include "core/css/parser/CSSPropertyParser.h"
#include "core/css/parser/CSSSelectorParser.h"
#include "core/css/parser/CSSTokenizer.h"
#include "core/css/parser/MediaQueryParser.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/frame/UseCounter.h"
#include "wtf/BitArray.h"
namespace blink {
CSSParserImpl::CSSParserImpl(const CSSParserContext& context, const String& string, StyleSheetContents* styleSheet)
: m_context(context)
, m_defaultNamespace(starAtom)
, m_styleSheet(styleSheet)
{
CSSTokenizer::tokenize(string, m_tokens);
}
bool CSSParserImpl::parseValue(MutableStylePropertySet* declaration, CSSPropertyID propertyID, const String& string, bool important, const CSSParserContext& context)
{
CSSParserImpl parser(context, string);
CSSRuleSourceData::Type ruleType = CSSRuleSourceData::STYLE_RULE;
if (declaration->cssParserMode() == CSSViewportRuleMode)
ruleType = CSSRuleSourceData::VIEWPORT_RULE;
parser.consumeDeclarationValue(CSSParserTokenRange(parser.m_tokens), propertyID, important, ruleType);
if (parser.m_parsedProperties.isEmpty())
return false;
declaration->addParsedProperties(parser.m_parsedProperties);
return true;
}
static inline void filterProperties(bool important, const WillBeHeapVector<CSSProperty, 256>& input, WillBeHeapVector<CSSProperty, 256>& output, size_t& unusedEntries, BitArray<numCSSProperties>& seenProperties)
{
// Add properties in reverse order so that highest priority definitions are reached first. Duplicate definitions can then be ignored when found.
for (size_t i = input.size(); i--; ) {
const CSSProperty& property = input[i];
if (property.isImportant() != important)
continue;
const unsigned propertyIDIndex = property.id() - firstCSSProperty;
if (seenProperties.get(propertyIDIndex))
continue;
seenProperties.set(propertyIDIndex);
output[--unusedEntries] = property;
}
}
static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet(const WillBeHeapVector<CSSProperty, 256>& parsedProperties, CSSParserMode mode)
{
BitArray<numCSSProperties> seenProperties;
size_t unusedEntries = parsedProperties.size();
WillBeHeapVector<CSSProperty, 256> results(unusedEntries);
filterProperties(true, parsedProperties, results, unusedEntries, seenProperties);
filterProperties(false, parsedProperties, results, unusedEntries, seenProperties);
return ImmutableStylePropertySet::create(results.data() + unusedEntries, results.size() - unusedEntries, mode);
}
PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> CSSParserImpl::parseInlineStyleDeclaration(const String& string, Element* element)
{
Document& document = element->document();
CSSParserContext context = CSSParserContext(document.elementSheet().contents()->parserContext(), UseCounter::getFrom(&document));
CSSParserMode mode = element->isHTMLElement() && !document.inQuirksMode() ? HTMLStandardMode : HTMLQuirksMode;
context.setMode(mode);
CSSParserImpl parser(context, string);
parser.consumeDeclarationList(CSSParserTokenRange(parser.m_tokens), CSSRuleSourceData::STYLE_RULE);
return createStylePropertySet(parser.m_parsedProperties, mode);
}
bool CSSParserImpl::parseDeclaration(MutableStylePropertySet* declaration, const String& string, const CSSParserContext& context)
{
CSSParserImpl parser(context, string);
CSSRuleSourceData::Type ruleType = CSSRuleSourceData::STYLE_RULE;
if (declaration->cssParserMode() == CSSViewportRuleMode)
ruleType = CSSRuleSourceData::VIEWPORT_RULE;
parser.consumeDeclarationList(CSSParserTokenRange(parser.m_tokens), ruleType);
if (parser.m_parsedProperties.isEmpty())
return false;
declaration->addParsedProperties(parser.m_parsedProperties);
return true;
}
PassRefPtrWillBeRawPtr<StyleRuleBase> CSSParserImpl::parseRule(const String& string, const CSSParserContext& context, AllowedRulesType allowedRules)
{
CSSParserImpl parser(context, string);
CSSParserTokenRange range(parser.m_tokens);
range.consumeWhitespaceAndComments();
if (range.atEnd())
return nullptr; // Parse error, empty rule
RefPtrWillBeRawPtr<StyleRuleBase> rule;
if (range.peek().type() == AtKeywordToken)
rule = parser.consumeAtRule(range, allowedRules);
else
rule = parser.consumeQualifiedRule(range, allowedRules);
if (!rule)
return nullptr; // Parse error, failed to consume rule
range.consumeWhitespaceAndComments();
if (!rule || !range.atEnd())
return nullptr; // Parse error, trailing garbage
return rule;
}
void CSSParserImpl::parseStyleSheet(const String& string, const CSSParserContext& context, StyleSheetContents* styleSheet)
{
CSSParserImpl parser(context, string, styleSheet);
WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> rules = parser.consumeRuleList(parser.m_tokens, TopLevelRuleList);
for (const auto& rule : rules)
styleSheet->parserAppendRule(rule);
}
PassOwnPtr<Vector<double>> CSSParserImpl::parseKeyframeKeyList(const String& keyList)
{
Vector<CSSParserToken> tokens;
CSSTokenizer::tokenize(keyList, tokens);
return consumeKeyframeKeyList(tokens);
}
WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> CSSParserImpl::consumeRuleList(CSSParserTokenRange range, RuleListType ruleListType)
{
WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> result;
AllowedRulesType allowedRules;
switch (ruleListType) {
case TopLevelRuleList:
allowedRules = AllowCharsetRules;
break;
case RegularRuleList:
allowedRules = RegularRules;
break;
case KeyframesRuleList:
allowedRules = KeyframeRules;
break;
default:
ASSERT_NOT_REACHED();
}
while (!range.atEnd()) {
switch (range.peek().type()) {
case WhitespaceToken:
case CommentToken:
range.consumeWhitespaceAndComments();
break;
case AtKeywordToken:
if (PassRefPtrWillBeRawPtr<StyleRuleBase> rule = consumeAtRule(range, allowedRules))
result.append(rule);
break;
default:
// FIXME: TopLevelRuleList should skip <CDO-token> and <CDC-token>
if (PassRefPtrWillBeRawPtr<StyleRuleBase> rule = consumeQualifiedRule(range, allowedRules))
result.append(rule);
break;
}
}
return result;
}
PassRefPtrWillBeRawPtr<StyleRuleBase> CSSParserImpl::consumeAtRule(CSSParserTokenRange& range, AllowedRulesType& allowedRules)
{
ASSERT(range.peek().type() == AtKeywordToken);
const String& name = range.consume().value();
const CSSParserToken* preludeStart = &range.peek();
while (!range.atEnd() && range.peek().type() != LeftBraceToken && range.peek().type() != SemicolonToken)
range.consumeComponentValue();
CSSParserTokenRange prelude = range.makeSubRange(preludeStart, &range.peek());
if (range.atEnd() || range.peek().type() == SemicolonToken) {
range.consume();
if (allowedRules == AllowCharsetRules && equalIgnoringCase(name, "charset")) {
// @charset is actually parsed before we get into the CSS parser.
// In theory we should validate the prelude is a string, but we don't
// have error logging yet so it doesn't matter.
return nullptr;
}
if (allowedRules <= AllowImportRules && equalIgnoringCase(name, "import")) {
allowedRules = AllowImportRules;
return consumeImportRule(prelude);
}
if (allowedRules <= AllowNamespaceRules && equalIgnoringCase(name, "namespace")) {
allowedRules = AllowNamespaceRules;
consumeNamespaceRule(prelude);
return nullptr;
}
return nullptr; // Parse error, unrecognised at-rule without block
}
CSSParserTokenRange block = range.consumeBlock();
if (allowedRules == KeyframeRules)
return nullptr; // Parse error, no at-rules supported inside @keyframes
ASSERT(allowedRules <= RegularRules);
allowedRules = RegularRules;
if (equalIgnoringCase(name, "media"))
return consumeMediaRule(prelude, block);
if (equalIgnoringCase(name, "viewport"))
return consumeViewportRule(prelude, block);
if (equalIgnoringCase(name, "font-face"))
return consumeFontFaceRule(prelude, block);
if (equalIgnoringCase(name, "-webkit-keyframes"))
return consumeKeyframesRule(true, prelude, block);
if (RuntimeEnabledFeatures::cssAnimationUnprefixedEnabled() && equalIgnoringCase(name, "keyframes"))
return consumeKeyframesRule(false, prelude, block);
return nullptr; // Parse error, unrecognised at-rule with block
}
PassRefPtrWillBeRawPtr<StyleRuleBase> CSSParserImpl::consumeQualifiedRule(CSSParserTokenRange& range, AllowedRulesType& allowedRules)
{
const CSSParserToken* preludeStart = &range.peek();
while (!range.atEnd() && range.peek().type() != LeftBraceToken)
range.consumeComponentValue();
if (range.atEnd())
return nullptr; // Parse error, EOF instead of qualified rule block
CSSParserTokenRange prelude = range.makeSubRange(preludeStart, &range.peek());
CSSParserTokenRange block = range.consumeBlock();
if (allowedRules <= RegularRules) {
allowedRules = RegularRules;
return consumeStyleRule(prelude, block);
}
if (allowedRules == KeyframeRules)
return consumeKeyframeStyleRule(prelude, block);
ASSERT_NOT_REACHED();
return nullptr;
}
// This may still consume tokens if it fails
static String consumeStringOrURI(CSSParserTokenRange& range)
{
const CSSParserToken& token = range.peek();
if (token.type() == StringToken || token.type() == UrlToken)
return AtomicString(range.consumeIncludingWhitespaceAndComments().value());
if (token.type() != FunctionToken || !equalIgnoringCase(token.value(), "url"))
return AtomicString();
CSSParserTokenRange contents = range.consumeBlock();
const CSSParserToken& uri = contents.consumeIncludingWhitespaceAndComments();
ASSERT(uri.type() == StringToken);
if (!contents.atEnd())
return AtomicString();
return AtomicString(uri.value());
}
PassRefPtrWillBeRawPtr<StyleRuleImport> CSSParserImpl::consumeImportRule(CSSParserTokenRange prelude)
{
prelude.consumeWhitespaceAndComments();
AtomicString uri(consumeStringOrURI(prelude));
if (uri.isNull())
return nullptr; // Parse error, expected string or URI
return StyleRuleImport::create(uri, MediaQueryParser::parseMediaQuerySet(prelude));
}
void CSSParserImpl::consumeNamespaceRule(CSSParserTokenRange prelude)
{
prelude.consumeWhitespaceAndComments();
AtomicString namespacePrefix;
if (prelude.peek().type() == IdentToken)
namespacePrefix = AtomicString(prelude.consumeIncludingWhitespaceAndComments().value());
AtomicString uri(consumeStringOrURI(prelude));
prelude.consumeWhitespaceAndComments();
if (uri.isNull() || !prelude.atEnd())
return; // Parse error, expected string or URI
m_styleSheet->parserAddNamespace(namespacePrefix, uri);
if (namespacePrefix.isNull())
m_defaultNamespace = uri;
}
PassRefPtrWillBeRawPtr<StyleRuleMedia> CSSParserImpl::consumeMediaRule(CSSParserTokenRange prelude, CSSParserTokenRange block)
{
WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> rules = consumeRuleList(block, RegularRuleList);
return StyleRuleMedia::create(MediaQueryParser::parseMediaQuerySet(prelude), rules);
}
PassRefPtrWillBeRawPtr<StyleRuleViewport> CSSParserImpl::consumeViewportRule(CSSParserTokenRange prelude, CSSParserTokenRange block)
{
// Allow @viewport rules from UA stylesheets even if the feature is disabled.
if (!RuntimeEnabledFeatures::cssViewportEnabled() && !isUASheetBehavior(m_context.mode()))
return nullptr;
prelude.consumeWhitespaceAndComments();
if (!prelude.atEnd())
return nullptr; // Parser error; @viewport prelude should be empty
consumeDeclarationList(block, CSSRuleSourceData::VIEWPORT_RULE);
RefPtrWillBeRawPtr<StyleRuleViewport> rule = StyleRuleViewport::create();
rule->setProperties(createStylePropertySet(m_parsedProperties, m_context.mode()));
m_parsedProperties.clear();
return rule.release();
}
PassRefPtrWillBeRawPtr<StyleRuleFontFace> CSSParserImpl::consumeFontFaceRule(CSSParserTokenRange prelude, CSSParserTokenRange block)
{
prelude.consumeWhitespaceAndComments();
if (!prelude.atEnd())
return nullptr; // Parse error; @font-face prelude should be empty
consumeDeclarationList(block, CSSRuleSourceData::FONT_FACE_RULE);
// FIXME: This logic should be in CSSPropertyParser
// FIXME: Shouldn't we fail if font-family or src aren't specified?
for (unsigned i = 0; i < m_parsedProperties.size(); ++i) {
CSSProperty& property = m_parsedProperties[i];
if (property.id() == CSSPropertyFontVariant && property.value()->isPrimitiveValue()) {
property.wrapValueInCommaSeparatedList();
} else if (property.id() == CSSPropertyFontFamily && (!property.value()->isValueList() || toCSSValueList(property.value())->length() != 1)) {
m_parsedProperties.clear();
return nullptr;
}
}
RefPtrWillBeRawPtr<StyleRuleFontFace> rule = StyleRuleFontFace::create();
rule->setProperties(createStylePropertySet(m_parsedProperties, m_context.mode()));
m_parsedProperties.clear();
if (m_styleSheet)
m_styleSheet->setHasFontFaceRule(true);
return rule.release();
}
PassRefPtrWillBeRawPtr<StyleRuleKeyframes> CSSParserImpl::consumeKeyframesRule(bool webkitPrefixed, CSSParserTokenRange prelude, CSSParserTokenRange block)
{
prelude.consumeWhitespaceAndComments();
const CSSParserToken& nameToken = prelude.consumeIncludingWhitespaceAndComments();
if (!prelude.atEnd())
return nullptr; // Parse error; expected single non-whitespace token in @keyframes header
String name;
if (nameToken.type() == IdentToken) {
name = nameToken.value();
} else if (nameToken.type() == StringToken && webkitPrefixed) {
if (m_context.useCounter())
m_context.useCounter()->count(UseCounter::QuotedKeyframesRule);
name = nameToken.value();
} else {
return nullptr; // Parse error; expected ident token in @keyframes header
}
const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>>& keyframeRules = consumeRuleList(block, KeyframesRuleList);
RefPtrWillBeRawPtr<StyleRuleKeyframes> rule = StyleRuleKeyframes::create();
for (const auto& keyframe : keyframeRules)
rule->parserAppendKeyframe(toStyleRuleKeyframe(keyframe.get()));
rule->setName(name);
rule->setVendorPrefixed(webkitPrefixed);
return rule.release();
}
PassRefPtrWillBeRawPtr<StyleRuleKeyframe> CSSParserImpl::consumeKeyframeStyleRule(CSSParserTokenRange prelude, CSSParserTokenRange block)
{
OwnPtr<Vector<double>> keyList = consumeKeyframeKeyList(prelude);
if (!keyList)
return nullptr;
consumeDeclarationList(block, CSSRuleSourceData::KEYFRAMES_RULE);
RefPtrWillBeRawPtr<StyleRuleKeyframe> rule = StyleRuleKeyframe::create();
rule->setKeys(keyList.release());
rule->setProperties(createStylePropertySet(m_parsedProperties, m_context.mode()));
m_parsedProperties.clear();
return rule.release();
}
PassRefPtrWillBeRawPtr<StyleRule> CSSParserImpl::consumeStyleRule(CSSParserTokenRange prelude, CSSParserTokenRange block)
{
CSSSelectorList selectorList;
CSSSelectorParser::parseSelector(prelude, m_context, m_defaultNamespace, m_styleSheet, selectorList);
if (!selectorList.isValid())
return nullptr; // Parse error, invalid selector list
consumeDeclarationList(block, CSSRuleSourceData::STYLE_RULE);
RefPtrWillBeRawPtr<StyleRule> rule = StyleRule::create();
rule->wrapperAdoptSelectorList(selectorList);
rule->setProperties(createStylePropertySet(m_parsedProperties, m_context.mode()));
m_parsedProperties.clear();
return rule.release();
}
void CSSParserImpl::consumeDeclarationList(CSSParserTokenRange range, CSSRuleSourceData::Type ruleType)
{
ASSERT(m_parsedProperties.isEmpty());
while (!range.atEnd()) {
switch (range.peek().type()) {
case CommentToken:
case WhitespaceToken:
case SemicolonToken:
range.consume();
break;
case IdentToken: {
const CSSParserToken* declarationStart = &range.peek();
while (!range.atEnd() && range.peek().type() != SemicolonToken)
range.consumeComponentValue();
consumeDeclaration(range.makeSubRange(declarationStart, &range.peek()), ruleType);
break;
}
default: // Parser error
// FIXME: The spec allows at-rules in a declaration list
while (!range.atEnd() && range.peek().type() != SemicolonToken)
range.consumeComponentValue();
break;
}
}
}
void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, CSSRuleSourceData::Type ruleType)
{
ASSERT(range.peek().type() == IdentToken);
CSSPropertyID id = range.consume().parseAsCSSPropertyID();
range.consumeWhitespaceAndComments();
if (range.consume().type() != ColonToken)
return; // Parser error
// FIXME: We shouldn't allow !important in @keyframes or @font-face
const CSSParserToken* last = range.end() - 1;
while (last->type() == WhitespaceToken || last->type() == CommentToken)
--last;
if (last->type() == IdentToken && equalIgnoringCase(last->value(), "important")) {
--last;
while (last->type() == WhitespaceToken || last->type() == CommentToken)
--last;
if (last->type() == DelimiterToken && last->delimiter() == '!') {
consumeDeclarationValue(range.makeSubRange(&range.peek(), last), id, true, ruleType);
return;
}
}
consumeDeclarationValue(range.makeSubRange(&range.peek(), range.end()), id, false, ruleType);
}
void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSPropertyID propertyID, bool important, CSSRuleSourceData::Type ruleType)
{
CSSParserValueList valueList(range);
if (!valueList.size())
return; // Parser error
bool inViewport = ruleType == CSSRuleSourceData::VIEWPORT_RULE;
CSSPropertyParser::parseValue(propertyID, important, &valueList, m_context, inViewport, m_parsedProperties, ruleType);
}
PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenRange range)
{
OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>);
while (true) {
range.consumeWhitespaceAndComments();
const CSSParserToken& token = range.consumeIncludingWhitespaceAndComments();
if (token.type() == PercentageToken && token.numericValue() >= 0 && token.numericValue() <= 100)
result->append(token.numericValue() / 100);
else if (token.type() == IdentToken && equalIgnoringCase(token.value(), "from"))
result->append(0);
else if (token.type() == IdentToken && equalIgnoringCase(token.value(), "to"))
result->append(1);
else
return nullptr; // Parser error, invalid value in keyframe selector
if (range.atEnd())
return result.release();
if (range.consume().type() != CommaToken)
return nullptr; // Parser error
}
}
} // namespace blink
|