OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/css/parser/CSSPropertyParser.h" | 6 #include "core/css/parser/CSSPropertyParser.h" |
7 | 7 |
8 #include "core/StylePropertyShorthand.h" | 8 #include "core/StylePropertyShorthand.h" |
9 #include "core/css/CSSCalculationValue.h" | 9 #include "core/css/CSSCalculationValue.h" |
10 #include "core/css/CSSCustomIdentValue.h" | 10 #include "core/css/CSSCustomIdentValue.h" |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 return nullptr; | 351 return nullptr; |
352 } | 352 } |
353 CalcParser calcParser(range, valueRange); | 353 CalcParser calcParser(range, valueRange); |
354 if (const CSSCalcValue* calculation = calcParser.value()) { | 354 if (const CSSCalcValue* calculation = calcParser.value()) { |
355 if (calculation->category() == CalcTime) | 355 if (calculation->category() == CalcTime) |
356 return calcParser.consumeValue(); | 356 return calcParser.consumeValue(); |
357 } | 357 } |
358 return nullptr; | 358 return nullptr; |
359 } | 359 } |
360 | 360 |
| 361 static int clampRGBComponent(const CSSPrimitiveValue& value) |
| 362 { |
| 363 double result = value.getDoubleValue(); |
| 364 // TODO(timloh): Multiply by 2.55 and round instead of floor. |
| 365 if (value.isPercentage()) |
| 366 result *= 2.56; |
| 367 return clampTo<int>(result, 0, 255); |
| 368 } |
| 369 |
| 370 static bool parseRGBParameters(CSSParserTokenRange& range, RGBA32& result, bool
parseAlpha) |
| 371 { |
| 372 ASSERT(range.peek().functionId() == CSSValueRgb || range.peek().functionId()
== CSSValueRgba); |
| 373 CSSParserTokenRange args = consumeFunction(range); |
| 374 RefPtrWillBeRawPtr<CSSPrimitiveValue> colorParameter = consumeInteger(args); |
| 375 if (!colorParameter) |
| 376 colorParameter = consumePercent(args, ValueRangeAll); |
| 377 if (!colorParameter) |
| 378 return false; |
| 379 const bool isPercent = colorParameter->isPercentage(); |
| 380 int colorArray[3]; |
| 381 colorArray[0] = clampRGBComponent(*colorParameter); |
| 382 for (int i = 1; i < 3; i++) { |
| 383 if (!consumeCommaIncludingWhitespace(args)) |
| 384 return false; |
| 385 colorParameter = isPercent ? consumePercent(args, ValueRangeAll) : consu
meInteger(args); |
| 386 if (!colorParameter) |
| 387 return false; |
| 388 colorArray[i] = clampRGBComponent(*colorParameter); |
| 389 } |
| 390 if (parseAlpha) { |
| 391 if (!consumeCommaIncludingWhitespace(args)) |
| 392 return false; |
| 393 double alpha; |
| 394 if (!consumeNumberRaw(args, alpha)) |
| 395 return false; |
| 396 // Convert the floating pointer number of alpha to an integer in the ran
ge [0, 256), |
| 397 // with an equal distribution across all 256 values. |
| 398 int alphaComponent = static_cast<int>(clampTo<double>(alpha, 0.0, 1.0) *
nextafter(256.0, 0.0)); |
| 399 result = makeRGBA(colorArray[0], colorArray[1], colorArray[2], alphaComp
onent); |
| 400 } else { |
| 401 result = makeRGB(colorArray[0], colorArray[1], colorArray[2]); |
| 402 } |
| 403 return args.atEnd(); |
| 404 } |
| 405 |
| 406 static bool parseHSLParameters(CSSParserTokenRange& range, RGBA32& result, bool
parseAlpha) |
| 407 { |
| 408 ASSERT(range.peek().functionId() == CSSValueHsl || range.peek().functionId()
== CSSValueHsla); |
| 409 CSSParserTokenRange args = consumeFunction(range); |
| 410 RefPtrWillBeRawPtr<CSSPrimitiveValue> hslValue = consumeNumber(args, ValueRa
ngeAll); |
| 411 if (!hslValue) |
| 412 return false; |
| 413 double colorArray[3]; |
| 414 colorArray[0] = (((hslValue->getIntValue() % 360) + 360) % 360) / 360.0; |
| 415 for (int i = 1; i < 3; i++) { |
| 416 if (!consumeCommaIncludingWhitespace(args)) |
| 417 return false; |
| 418 hslValue = consumePercent(args, ValueRangeAll); |
| 419 if (!hslValue) |
| 420 return false; |
| 421 double doubleValue = hslValue->getDoubleValue(); |
| 422 colorArray[i] = clampTo<double>(doubleValue, 0.0, 100.0) / 100.0; // Nee
ds to be value between 0 and 1.0. |
| 423 } |
| 424 double alpha = 1.0; |
| 425 if (parseAlpha) { |
| 426 if (!consumeCommaIncludingWhitespace(args)) |
| 427 return false; |
| 428 if (!consumeNumberRaw(args, alpha)) |
| 429 return false; |
| 430 alpha = clampTo<double>(alpha, 0.0, 1.0); |
| 431 } |
| 432 result = makeRGBAFromHSLA(colorArray[0], colorArray[1], colorArray[2], alpha
); |
| 433 return args.atEnd(); |
| 434 } |
| 435 |
| 436 static bool parseHexColor(CSSParserTokenRange& range, RGBA32& result, bool accep
tQuirkyColors) |
| 437 { |
| 438 const CSSParserToken& token = range.peek(); |
| 439 String color; |
| 440 if (acceptQuirkyColors) { |
| 441 if (token.type() == NumberToken && token.numericValueType() == IntegerVa
lueType |
| 442 && token.numericValue() >= 0. && token.numericValue() < 1000000.) {
// e.g. 112233 |
| 443 color = String::format("%06d", static_cast<int>(token.numericValue()
)); |
| 444 } else if (token.type() == DimensionToken) { // e.g. 0001FF |
| 445 color = String::number(static_cast<int>(token.numericValue())) + Str
ing(token.value()); |
| 446 if (color.length() > 6) |
| 447 return false; |
| 448 while (color.length() < 6) |
| 449 color = "0" + color; |
| 450 } else if (token.type() == IdentToken) { // e.g. FF0000 |
| 451 color = token.value(); |
| 452 } |
| 453 } |
| 454 if (token.type() == HashToken) |
| 455 color = token.value(); |
| 456 if (!Color::parseHexColor(color, result)) |
| 457 return false; |
| 458 range.consumeIncludingWhitespace(); |
| 459 return true; |
| 460 } |
| 461 |
| 462 static bool parseColorFunction(CSSParserTokenRange& range, RGBA32& result) |
| 463 { |
| 464 CSSValueID functionId = range.peek().functionId(); |
| 465 if (functionId < CSSValueRgb || functionId > CSSValueHsla) |
| 466 return false; |
| 467 CSSParserTokenRange colorRange = range; |
| 468 if ((functionId <= CSSValueRgba && !parseRGBParameters(colorRange, result, f
unctionId == CSSValueRgba)) |
| 469 || (functionId >= CSSValueHsl && !parseHSLParameters(colorRange, result,
functionId == CSSValueHsla))) |
| 470 return false; |
| 471 range = colorRange; |
| 472 return true; |
| 473 } |
| 474 |
| 475 static PassRefPtrWillBeRawPtr<CSSValue> consumeColor(CSSParserTokenRange& range,
const CSSParserContext& context, bool acceptQuirkyColors = false) |
| 476 { |
| 477 CSSValueID id = range.peek().id(); |
| 478 if (CSSPropertyParser::isColorKeyword(id)) { |
| 479 if (!isValueAllowedInMode(id, context.mode())) |
| 480 return nullptr; |
| 481 return consumeIdent(range); |
| 482 } |
| 483 RGBA32 color = Color::transparent; |
| 484 if (!parseHexColor(range, color, acceptQuirkyColors) && !parseColorFunction(
range, color)) |
| 485 return nullptr; |
| 486 return cssValuePool().createColorValue(color); |
| 487 } |
| 488 |
361 static inline bool isCSSWideKeyword(const CSSValueID& id) | 489 static inline bool isCSSWideKeyword(const CSSValueID& id) |
362 { | 490 { |
363 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset
|| id == CSSValueDefault; | 491 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset
|| id == CSSValueDefault; |
364 } | 492 } |
365 | 493 |
366 // Methods for consuming non-shorthand properties starts here. | 494 // Methods for consuming non-shorthand properties starts here. |
367 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r
ange) | 495 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r
ange) |
368 { | 496 { |
369 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated
(); | 497 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated
(); |
370 if (range.peek().id() == CSSValueAuto) { | 498 if (range.peek().id() == CSSValueAuto) { |
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1294 case CSSPropertyAnimationIterationCount: | 1422 case CSSPropertyAnimationIterationCount: |
1295 case CSSPropertyAnimationName: | 1423 case CSSPropertyAnimationName: |
1296 case CSSPropertyAnimationPlayState: | 1424 case CSSPropertyAnimationPlayState: |
1297 case CSSPropertyTransitionProperty: | 1425 case CSSPropertyTransitionProperty: |
1298 case CSSPropertyAnimationTimingFunction: | 1426 case CSSPropertyAnimationTimingFunction: |
1299 case CSSPropertyTransitionTimingFunction: | 1427 case CSSPropertyTransitionTimingFunction: |
1300 return consumeAnimationPropertyList(property, m_range, m_context, unreso
lvedProperty == CSSPropertyAliasWebkitAnimationName); | 1428 return consumeAnimationPropertyList(property, m_range, m_context, unreso
lvedProperty == CSSPropertyAliasWebkitAnimationName); |
1301 case CSSPropertyOrphans: | 1429 case CSSPropertyOrphans: |
1302 case CSSPropertyWidows: | 1430 case CSSPropertyWidows: |
1303 return consumeWidowsOrOrphans(m_range); | 1431 return consumeWidowsOrOrphans(m_range); |
| 1432 case CSSPropertyWebkitTextFillColor: |
| 1433 case CSSPropertyWebkitTapHighlightColor: |
| 1434 return consumeColor(m_range, m_context); |
| 1435 case CSSPropertyColor: |
| 1436 return consumeColor(m_range, m_context, inQuirksMode()); |
1304 default: | 1437 default: |
1305 return nullptr; | 1438 return nullptr; |
1306 } | 1439 } |
1307 } | 1440 } |
1308 | 1441 |
1309 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse
rTokenRange& range) | 1442 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse
rTokenRange& range) |
1310 { | 1443 { |
1311 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated
(); | 1444 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated
(); |
1312 | 1445 |
1313 do { | 1446 do { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1724 m_currentShorthand = oldShorthand; | 1857 m_currentShorthand = oldShorthand; |
1725 return consumeColumns(important); | 1858 return consumeColumns(important); |
1726 } | 1859 } |
1727 default: | 1860 default: |
1728 m_currentShorthand = oldShorthand; | 1861 m_currentShorthand = oldShorthand; |
1729 return false; | 1862 return false; |
1730 } | 1863 } |
1731 } | 1864 } |
1732 | 1865 |
1733 } // namespace blink | 1866 } // namespace blink |
OLD | NEW |