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[4]; | |
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); | |
Timothy Loh
2015/10/27 23:57:39
consumeNumberRaw probably works here
rwlbuis
2015/10/28 02:59:32
Good idea! Done. BTW I see now that this is very u
| |
394 if (!alphaValue) | |
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 colorArray[3] = static_cast<int>(clampTo<double>(alphaValue->getDoubleVa lue(), 0.0, 1.0) * nextafter(256.0, 0.0)); | |
Timothy Loh
2015/10/27 23:57:39
I'd rather this was just a variable scoped to this
| |
399 result = makeRGBA(colorArray[0], colorArray[1], colorArray[2], colorArra y[3]); | |
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[4]; | |
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 if (parseAlpha) { | |
425 if (!consumeCommaIncludingWhitespace(args)) | |
426 return false; | |
427 hslValue = consumeNumber(args, ValueRangeAll); | |
Timothy Loh
2015/10/27 23:57:39
consumeNumberRaw here too
| |
428 if (!hslValue) | |
429 return false; | |
430 colorArray[3] = clampTo<double>(hslValue->getDoubleValue(), 0.0, 1.0); | |
431 } else { | |
432 colorArray[3] = 1.0; | |
433 } | |
434 result = makeRGBAFromHSLA(colorArray[0], colorArray[1], colorArray[2], color Array[3]); | |
435 return args.atEnd(); | |
436 } | |
437 | |
438 static bool parseHexColor(CSSParserTokenRange& range, RGBA32& result, bool accep tQuirkyColors) | |
439 { | |
440 const CSSParserToken& token = range.peek(); | |
441 String color; | |
442 if (acceptQuirkyColors) { | |
443 if (token.type() == NumberToken && token.numericValueType() == IntegerVa lueType | |
444 && token.numericValue() >= 0. && token.numericValue() < 1000000.) { // e.g. 112233 | |
445 color = String::format("%06d", static_cast<int>(token.numericValue() )); | |
446 } else if (token.type() == DimensionToken) { // e.g. 0001FF | |
447 color = String::number(static_cast<int>(token.numericValue())) + Str ing(token.value()); | |
448 if (color.length() > 6) | |
449 return false; | |
450 while (color.length() < 6) | |
451 color = "0" + color; | |
452 } else if (token.type() == IdentToken) { // e.g. FF0000 | |
453 color = token.value(); | |
454 } | |
455 } | |
456 if (token.type() == HashToken) | |
457 color = token.value(); | |
458 if (!Color::parseHexColor(color, result)) | |
459 return false; | |
460 range.consumeIncludingWhitespace(); | |
461 return true; | |
462 } | |
463 | |
464 static bool parseColorFunction(CSSParserTokenRange& range, RGBA32& result) | |
465 { | |
466 CSSValueID functionId = range.peek().functionId(); | |
467 if (functionId < CSSValueRgb || functionId > CSSValueHsla) | |
468 return false; | |
469 CSSParserTokenRange colorRange = range; | |
470 if ((functionId <= CSSValueRgba && !parseRGBParameters(colorRange, result, f unctionId == CSSValueRgba)) | |
471 || (functionId >= CSSValueHsl && !parseHSLParameters(colorRange, result, functionId == CSSValueHsla))) | |
472 return false; | |
473 range = colorRange; | |
474 return true; | |
475 } | |
476 | |
477 static PassRefPtrWillBeRawPtr<CSSValue> consumeColor(CSSParserTokenRange& range, const CSSParserContext& context, bool acceptQuirkyColors = false) | |
478 { | |
479 CSSValueID id = range.peek().id(); | |
480 if (CSSPropertyParser::isColorKeyword(id)) { | |
481 if (!isValueAllowedInMode(id, context.mode())) | |
482 return nullptr; | |
483 return consumeIdent(range); | |
484 } | |
485 RGBA32 color = Color::transparent; | |
486 if (!parseHexColor(range, color, acceptQuirkyColors) && !parseColorFunction( range, color)) | |
487 return nullptr; | |
488 return cssValuePool().createColorValue(color); | |
489 } | |
490 | |
361 static inline bool isCSSWideKeyword(const CSSValueID& id) | 491 static inline bool isCSSWideKeyword(const CSSValueID& id) |
362 { | 492 { |
363 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset || id == CSSValueDefault; | 493 return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset || id == CSSValueDefault; |
364 } | 494 } |
365 | 495 |
366 // Methods for consuming non-shorthand properties starts here. | 496 // Methods for consuming non-shorthand properties starts here. |
367 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r ange) | 497 static PassRefPtrWillBeRawPtr<CSSValue> consumeWillChange(CSSParserTokenRange& r ange) |
368 { | 498 { |
369 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); | 499 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); |
370 if (range.peek().id() == CSSValueAuto) { | 500 if (range.peek().id() == CSSValueAuto) { |
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1294 case CSSPropertyAnimationIterationCount: | 1424 case CSSPropertyAnimationIterationCount: |
1295 case CSSPropertyAnimationName: | 1425 case CSSPropertyAnimationName: |
1296 case CSSPropertyAnimationPlayState: | 1426 case CSSPropertyAnimationPlayState: |
1297 case CSSPropertyTransitionProperty: | 1427 case CSSPropertyTransitionProperty: |
1298 case CSSPropertyAnimationTimingFunction: | 1428 case CSSPropertyAnimationTimingFunction: |
1299 case CSSPropertyTransitionTimingFunction: | 1429 case CSSPropertyTransitionTimingFunction: |
1300 return consumeAnimationPropertyList(property, m_range, m_context, unreso lvedProperty == CSSPropertyAliasWebkitAnimationName); | 1430 return consumeAnimationPropertyList(property, m_range, m_context, unreso lvedProperty == CSSPropertyAliasWebkitAnimationName); |
1301 case CSSPropertyOrphans: | 1431 case CSSPropertyOrphans: |
1302 case CSSPropertyWidows: | 1432 case CSSPropertyWidows: |
1303 return consumeWidowsOrOrphans(m_range); | 1433 return consumeWidowsOrOrphans(m_range); |
1434 case CSSPropertyWebkitTextFillColor: | |
1435 case CSSPropertyWebkitTapHighlightColor: | |
1436 return consumeColor(m_range, m_context); | |
1437 case CSSPropertyColor: | |
1438 return consumeColor(m_range, m_context, inQuirksMode()); | |
1304 default: | 1439 default: |
1305 return nullptr; | 1440 return nullptr; |
1306 } | 1441 } |
1307 } | 1442 } |
1308 | 1443 |
1309 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse rTokenRange& range) | 1444 static PassRefPtrWillBeRawPtr<CSSValueList> consumeFontFaceUnicodeRange(CSSParse rTokenRange& range) |
1310 { | 1445 { |
1311 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); | 1446 RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createCommaSeparated (); |
1312 | 1447 |
1313 do { | 1448 do { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1724 m_currentShorthand = oldShorthand; | 1859 m_currentShorthand = oldShorthand; |
1725 return consumeColumns(important); | 1860 return consumeColumns(important); |
1726 } | 1861 } |
1727 default: | 1862 default: |
1728 m_currentShorthand = oldShorthand; | 1863 m_currentShorthand = oldShorthand; |
1729 return false; | 1864 return false; |
1730 } | 1865 } |
1731 } | 1866 } |
1732 | 1867 |
1733 } // namespace blink | 1868 } // namespace blink |
OLD | NEW |