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 parseColorParameters(CSSParserTokenRange& range, int* colorArray, bo ol parseAlpha) | |
Timothy Loh
2015/10/27 03:52:21
parseColorParameters -> parseRGBParameters?
IMO t
rwlbuis
2015/10/27 20:07:50
Done.
| |
371 { | |
372 CSSParserTokenRange args = consumeFunction(range); | |
373 // Get the first value and its type. | |
374 RefPtrWillBeRawPtr<CSSPrimitiveValue> colorParameter = consumeInteger(args); | |
375 if (!colorParameter) | |
376 colorParameter = consumePercent(args, ValueRangeAll); | |
377 if (!colorParameter) | |
378 return false; | |
379 | |
380 const bool isPercent = colorParameter->isPercentage(); | |
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 RefPtrWillBeRawPtr<CSSPrimitiveValue> alphaValue = consumeNumber(args, V alueRangeAll); | |
394 if (!alphaValue) | |
395 return false; | |
396 double doubleValue = alphaValue->getDoubleValue(); | |
397 // Convert the floating pointer number of alpha to an integer in the ran ge [0, 256), | |
398 // with an equal distribution across all 256 values. | |
399 colorArray[3] = static_cast<int>(clampTo<double>(doubleValue, 0.0, 1.0) * nextafter(256.0, 0.0)); | |
400 } | |
401 return args.atEnd(); | |
402 } | |
403 | |
404 static bool parseHSLParameters(CSSParserTokenRange& range, double* colorArray, b ool parseAlpha) | |
405 { | |
406 CSSParserTokenRange args = consumeFunction(range); | |
407 RefPtrWillBeRawPtr<CSSPrimitiveValue> hslValue = consumeNumber(args, ValueRa ngeAll); | |
408 if (!hslValue) | |
409 return false; | |
410 colorArray[0] = (((hslValue->getIntValue() % 360) + 360) % 360) / 360.0; | |
411 for (int i = 1; i < 3; i++) { | |
412 if (!consumeCommaIncludingWhitespace(args)) | |
413 return false; | |
414 hslValue = consumePercent(args, ValueRangeAll); | |
415 if (!hslValue) | |
416 return false; | |
417 double doubleValue = hslValue->getDoubleValue(); | |
418 colorArray[i] = clampTo<double>(doubleValue, 0.0, 100.0) / 100.0; // Nee ds to be value between 0 and 1.0. | |
419 } | |
420 if (parseAlpha) { | |
421 if (!consumeCommaIncludingWhitespace(args)) | |
422 return false; | |
423 hslValue = consumeNumber(args, ValueRangeAll); | |
424 if (!hslValue) | |
425 return false; | |
426 double doubleValue = hslValue->getDoubleValue(); | |
427 colorArray[3] = clampTo<double>(doubleValue, 0.0, 1.0); | |
428 } | |
429 return args.atEnd(); | |
430 } | |
431 | |
432 static bool parseColorFromValue(CSSParserTokenRange& range, RGBA32& result, bool acceptQuirkyColors) | |
433 { | |
434 const CSSParserToken& token = range.peek(); | |
435 String color; | |
436 if (acceptQuirkyColors) { | |
437 if (token.type() == NumberToken && token.numericValueType() == IntegerVa lueType | |
438 && token.numericValue() >= 0. && token.numericValue() < 1000000.) { // e.g. 112233 | |
439 color = String::format("%06d", static_cast<int>(token.numericValue() )); | |
440 } else if (token.type() == DimensionToken) { // e.g. 0001FF | |
441 color = String::number(static_cast<int>(token.numericValue())) + Str ing(token.value()); | |
442 if (color.length() > 6) | |
443 return false; | |
444 while (color.length() < 6) | |
445 color = "0" + color; | |
446 } else if (token.type() == IdentToken) { // e.g. FF0000 | |
447 color = token.value(); | |
448 } | |
449 } | |
450 if (token.type() == HashToken) | |
451 color = token.value(); | |
452 if (!color.isNull()) { | |
453 if (!Color::parseHexColor(color, result)) | |
454 return false; | |
455 range.consumeIncludingWhitespace(); | |
456 return true; | |
457 } | |
458 if (token.type() != FunctionToken) | |
459 return false; | |
460 CSSParserTokenRange colorRange = range; | |
461 if (token.functionId() == CSSValueRgb) { | |
462 int colorValues[3]; | |
463 if (!parseColorParameters(colorRange, colorValues, false)) | |
464 return false; | |
465 result = makeRGB(colorValues[0], colorValues[1], colorValues[2]); | |
466 } else if (token.functionId() == CSSValueRgba) { | |
467 int colorValues[4]; | |
468 if (!parseColorParameters(colorRange, colorValues, true)) | |
469 return false; | |
470 result = makeRGBA(colorValues[0], colorValues[1], colorValues[2], colorV alues[3]); | |
471 } else if (token.functionId() == CSSValueHsl) { | |
472 double colorValues[3]; | |
473 if (!parseHSLParameters(colorRange, colorValues, false)) | |
474 return false; | |
475 result = makeRGBAFromHSLA(colorValues[0], colorValues[1], colorValues[2] , 1.0); | |
476 } else if (token.functionId() == CSSValueHsla) { | |
477 double colorValues[4]; | |
478 if (!parseHSLParameters(colorRange, colorValues, true)) | |
479 return false; | |
480 result = makeRGBAFromHSLA(colorValues[0], colorValues[1], colorValues[2] , colorValues[3]); | |
481 } else { | |
482 return false; | |
483 } | |
484 | |
485 range = colorRange; | |
486 return true; | |
487 } | |
488 | |
489 static PassRefPtrWillBeRawPtr<CSSValue> consumeColor(CSSParserTokenRange& range, const CSSParserContext& context, bool acceptQuirkyColors = false) | |
490 { | |
491 CSSValueID id = range.peek().id(); | |
492 if (CSSPropertyParser::isColorKeyword(id)) { | |
493 if (!isValueAllowedInMode(id, context.mode())) | |
494 return nullptr; | |
495 return consumeIdent(range); | |
496 } | |
497 RGBA32 color = Color::transparent; | |
498 if (!parseColorFromValue(range, color, acceptQuirkyColors)) | |
Timothy Loh
2015/10/27 03:52:21
I don't think the current split makes sense here.
rwlbuis
2015/10/27 20:07:50
Sounds good, done.
| |
499 return nullptr; | |
500 return cssValuePool().createColorValue(color); | |
501 } | |
502 | |
361 static inline bool isCSSWideKeyword(const CSSValueID& id) | 503 static inline bool isCSSWideKeyword(const CSSValueID& id) |
362 { | 504 { |
363 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset || id == CSSValueDefault; | 505 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset || id == CSSValueDefault; |
364 } | 506 } |
365 | 507 |
366 // Methods for consuming non-shorthand properties starts here. | 508 // Methods for consuming non-shorthand properties starts here. |
367 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r ange) | 509 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r ange) |
368 { | 510 { |
369 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); | 511 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); |
370 if (range.peek().id() == CSSValueAuto) { | 512 if (range.peek().id() == CSSValueAuto) { |
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1294 case CSSPropertyAnimationIterationCount: | 1436 case CSSPropertyAnimationIterationCount: |
1295 case CSSPropertyAnimationName: | 1437 case CSSPropertyAnimationName: |
1296 case CSSPropertyAnimationPlayState: | 1438 case CSSPropertyAnimationPlayState: |
1297 case CSSPropertyTransitionProperty: | 1439 case CSSPropertyTransitionProperty: |
1298 case CSSPropertyAnimationTimingFunction: | 1440 case CSSPropertyAnimationTimingFunction: |
1299 case CSSPropertyTransitionTimingFunction: | 1441 case CSSPropertyTransitionTimingFunction: |
1300 return consumeAnimationPropertyList(property, m_range, m_context, unreso lvedProperty == CSSPropertyAliasWebkitAnimationName); | 1442 return consumeAnimationPropertyList(property, m_range, m_context, unreso lvedProperty == CSSPropertyAliasWebkitAnimationName); |
1301 case CSSPropertyOrphans: | 1443 case CSSPropertyOrphans: |
1302 case CSSPropertyWidows: | 1444 case CSSPropertyWidows: |
1303 return consumeWidowsOrOrphans(m_range); | 1445 return consumeWidowsOrOrphans(m_range); |
1446 case CSSPropertyWebkitTextFillColor: | |
1447 case CSSPropertyWebkitTapHighlightColor: | |
1448 return consumeColor(m_range, m_context); | |
1449 case CSSPropertyColor: | |
1450 return consumeColor(m_range, m_context, inQuirksMode()); | |
1304 default: | 1451 default: |
1305 return nullptr; | 1452 return nullptr; |
1306 } | 1453 } |
1307 } | 1454 } |
1308 | 1455 |
1309 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse rTokenRange& range) | 1456 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse rTokenRange& range) |
1310 { | 1457 { |
1311 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); | 1458 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); |
1312 | 1459 |
1313 do { | 1460 do { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1724 m_currentShorthand = oldShorthand; | 1871 m_currentShorthand = oldShorthand; |
1725 return consumeColumns(important); | 1872 return consumeColumns(important); |
1726 } | 1873 } |
1727 default: | 1874 default: |
1728 m_currentShorthand = oldShorthand; | 1875 m_currentShorthand = oldShorthand; |
1729 return false; | 1876 return false; |
1730 } | 1877 } |
1731 } | 1878 } |
1732 | 1879 |
1733 } // namespace blink | 1880 } // namespace blink |
OLD | NEW |