Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: Source/core/css/parser/CSSPropertyParser.cpp

Issue 1192983003: CSS Custom Properties (Variables) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Remove unnecessary enum Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com) 3 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
5 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> 5 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/) 7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/)
8 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved. 8 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
9 * Copyright (C) 2012 Intel Corporation. All rights reserved. 9 * Copyright (C) 2012 Intel Corporation. All rights reserved.
10 * 10 *
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "core/css/CSSPathValue.h" 44 #include "core/css/CSSPathValue.h"
45 #include "core/css/CSSPrimitiveValueMappings.h" 45 #include "core/css/CSSPrimitiveValueMappings.h"
46 #include "core/css/CSSProperty.h" 46 #include "core/css/CSSProperty.h"
47 #include "core/css/CSSPropertyMetadata.h" 47 #include "core/css/CSSPropertyMetadata.h"
48 #include "core/css/CSSReflectValue.h" 48 #include "core/css/CSSReflectValue.h"
49 #include "core/css/CSSSVGDocumentValue.h" 49 #include "core/css/CSSSVGDocumentValue.h"
50 #include "core/css/CSSShadowValue.h" 50 #include "core/css/CSSShadowValue.h"
51 #include "core/css/CSSTimingFunctionValue.h" 51 #include "core/css/CSSTimingFunctionValue.h"
52 #include "core/css/CSSUnicodeRangeValue.h" 52 #include "core/css/CSSUnicodeRangeValue.h"
53 #include "core/css/CSSValuePool.h" 53 #include "core/css/CSSValuePool.h"
54 #include "core/css/CSSCustomVariableValue.h"
54 #include "core/css/Counter.h" 55 #include "core/css/Counter.h"
55 #include "core/css/HashTools.h" 56 #include "core/css/HashTools.h"
56 #include "core/css/Pair.h" 57 #include "core/css/Pair.h"
57 #include "core/css/Rect.h" 58 #include "core/css/Rect.h"
58 #include "core/css/parser/CSSParserFastPaths.h" 59 #include "core/css/parser/CSSParserFastPaths.h"
59 #include "core/css/parser/CSSParserValues.h" 60 #include "core/css/parser/CSSParserValues.h"
60 #include "core/frame/UseCounter.h" 61 #include "core/frame/UseCounter.h"
61 #include "core/layout/LayoutTheme.h" 62 #include "core/layout/LayoutTheme.h"
62 #include "core/style/GridCoordinate.h" 63 #include "core/style/GridCoordinate.h"
63 #include "core/svg/SVGPathUtilities.h" 64 #include "core/svg/SVGPathUtilities.h"
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 && (!value->fValue // 0 can always be unitless. 229 && (!value->fValue // 0 can always be unitless.
229 || isUnitLessLengthParsingEnabledForMode(cssParserMode) // HTML and SVG attribute values can always be unitless. 230 || isUnitLessLengthParsingEnabledForMode(cssParserMode) // HTML and SVG attribute values can always be unitless.
230 || (cssParserMode == HTMLQuirksMode && (unitflags & FUnitlessQuirk)) ); 231 || (cssParserMode == HTMLQuirksMode && (unitflags & FUnitlessQuirk)) );
231 } 232 }
232 233
233 bool CSSPropertyParser::validUnit(CSSParserValue* value, Units unitflags, CSSPar serMode cssParserMode, ReleaseParsedCalcValueCondition releaseCalc) 234 bool CSSPropertyParser::validUnit(CSSParserValue* value, Units unitflags, CSSPar serMode cssParserMode, ReleaseParsedCalcValueCondition releaseCalc)
234 { 235 {
235 if (isCalculation(value)) 236 if (isCalculation(value))
236 return validCalculationUnit(value, unitflags, releaseCalc); 237 return validCalculationUnit(value, unitflags, releaseCalc);
237 238
239 if (RuntimeEnabledFeatures::cssVariablesEnabled() && isVariableReference(val ue))
240 return true;
alancutter (OOO until 2018) 2015/07/14 06:12:57 Is this ever hit?
241
238 if (unitflags & FNonNeg && value->fValue < 0) 242 if (unitflags & FNonNeg && value->fValue < 0)
239 return false; 243 return false;
240 switch (value->unit) { 244 switch (value->unit) {
241 case CSSPrimitiveValue::CSS_NUMBER: 245 case CSSPrimitiveValue::CSS_NUMBER:
242 if (unitflags & FNumber) 246 if (unitflags & FNumber)
243 return true; 247 return true;
244 if (shouldAcceptUnitLessValues(value, unitflags, cssParserMode)) { 248 if (shouldAcceptUnitLessValues(value, unitflags, cssParserMode)) {
245 value->unit = (unitflags & FLength) ? CSSPrimitiveValue::CSS_PX : CS SPrimitiveValue::CSS_DEG; 249 value->unit = (unitflags & FLength) ? CSSPrimitiveValue::CSS_PX : CS SPrimitiveValue::CSS_DEG;
246 return true; 250 return true;
247 } 251 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 return CSSPrimitiveValue::create(m_parsedCalculation.release()); 299 return CSSPrimitiveValue::create(m_parsedCalculation.release());
296 } 300 }
297 301
298 ASSERT((value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPr imitiveValue::CSS_KHZ) 302 ASSERT((value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPr imitiveValue::CSS_KHZ)
299 || (value->unit >= CSSPrimitiveValue::CSS_TURN && value->unit <= CSSPrim itiveValue::CSS_CHS) 303 || (value->unit >= CSSPrimitiveValue::CSS_TURN && value->unit <= CSSPrim itiveValue::CSS_CHS)
300 || (value->unit >= CSSPrimitiveValue::CSS_VW && value->unit <= CSSPrimit iveValue::CSS_VMAX) 304 || (value->unit >= CSSPrimitiveValue::CSS_VW && value->unit <= CSSPrimit iveValue::CSS_VMAX)
301 || (value->unit >= CSSPrimitiveValue::CSS_DPPX && value->unit <= CSSPrim itiveValue::CSS_DPCM)); 305 || (value->unit >= CSSPrimitiveValue::CSS_DPPX && value->unit <= CSSPrim itiveValue::CSS_DPCM));
302 return cssValuePool().createValue(value->fValue, static_cast<CSSPrimitiveVal ue::UnitType>(value->unit)); 306 return cssValuePool().createValue(value->fValue, static_cast<CSSPrimitiveVal ue::UnitType>(value->unit));
303 } 307 }
304 308
309 inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimit iveVariableReferenceValue(CSSParserValue* value)
310 {
311 ASSERT(value->unit == CSSParserValue::VariableValue);
312 return CSSPrimitiveValue::create(value->variableData);
alancutter (OOO until 2018) 2015/07/14 06:12:57 I think we can inline this. The ASSERT is implicit
313 }
314
305 inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimit iveStringValue(CSSParserValue* value) 315 inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimit iveStringValue(CSSParserValue* value)
306 { 316 {
307 ASSERT(value->unit == CSSPrimitiveValue::CSS_STRING || value->unit == CSSPri mitiveValue::CSS_IDENT); 317 ASSERT(value->unit == CSSPrimitiveValue::CSS_STRING || value->unit == CSSPri mitiveValue::CSS_IDENT);
308 return cssValuePool().createValue(value->string, CSSPrimitiveValue::CSS_STRI NG); 318 return cssValuePool().createValue(value->string, CSSPrimitiveValue::CSS_STRI NG);
309 } 319 }
310 320
311 inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimit iveCustomIdentValue(CSSParserValue* value) 321 inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimit iveCustomIdentValue(CSSParserValue* value)
312 { 322 {
313 ASSERT(value->unit == CSSPrimitiveValue::CSS_STRING || value->unit == CSSPri mitiveValue::CSS_IDENT); 323 ASSERT(value->unit == CSSPrimitiveValue::CSS_STRING || value->unit == CSSPri mitiveValue::CSS_IDENT);
314 return cssValuePool().createValue(value->string, CSSPrimitiveValue::CSS_CUST OM_IDENT); 324 return cssValuePool().createValue(value->string, CSSPrimitiveValue::CSS_CUST OM_IDENT);
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 return false; 444 return false;
435 addExpandedPropertyForValue(propId, cssValuePool().createExplicitInitial Value(), important); 445 addExpandedPropertyForValue(propId, cssValuePool().createExplicitInitial Value(), important);
436 return true; 446 return true;
437 } else if (id == CSSValueUnset) { 447 } else if (id == CSSValueUnset) {
438 if (m_valueList->size() != 1) 448 if (m_valueList->size() != 1)
439 return false; 449 return false;
440 addExpandedPropertyForValue(propId, cssValuePool().createUnsetValue(), i mportant); 450 addExpandedPropertyForValue(propId, cssValuePool().createUnsetValue(), i mportant);
441 return true; 451 return true;
442 } 452 }
443 453
454
444 int num = inShorthand() ? 1 : m_valueList->size(); 455 int num = inShorthand() ? 1 : m_valueList->size();
445 456
457 if (RuntimeEnabledFeatures::cssVariablesEnabled() && isVariableReference(val ue)) {
458 // We don't expand the shorthand here because crazypants.
459 m_parsedProperties.append(CSSProperty(propId, createPrimitiveVariableRef erenceValue(value), important, false, 0, m_implicitShorthand));
460 m_valueList->next();
461 return true;
462 }
463
446 if (CSSParserFastPaths::isKeywordPropertyID(propId)) { 464 if (CSSParserFastPaths::isKeywordPropertyID(propId)) {
447 if (!CSSParserFastPaths::isValidKeywordPropertyAndValue(propId, id)) 465 if (!CSSParserFastPaths::isValidKeywordPropertyAndValue(propId, id))
448 return false; 466 return false;
449 if (m_valueList->next() && !inShorthand()) 467 if (m_valueList->next() && !inShorthand())
450 return false; 468 return false;
451 addProperty(propId, cssValuePool().createIdentifierValue(id), important) ; 469 addProperty(propId, cssValuePool().createIdentifierValue(id), important) ;
452 return true; 470 return true;
453 } 471 }
454 472
455 bool validPrimitive = false; 473 bool validPrimitive = false;
(...skipping 1100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1556 1574
1557 // Properties below are validated inside parseViewportProperty, because we 1575 // Properties below are validated inside parseViewportProperty, because we
1558 // check for parser state. We need to invalidate if someone adds them outsid e 1576 // check for parser state. We need to invalidate if someone adds them outsid e
1559 // a @viewport rule. 1577 // a @viewport rule.
1560 case CSSPropertyMaxZoom: 1578 case CSSPropertyMaxZoom:
1561 case CSSPropertyMinZoom: 1579 case CSSPropertyMinZoom:
1562 case CSSPropertyOrientation: 1580 case CSSPropertyOrientation:
1563 case CSSPropertyUserZoom: 1581 case CSSPropertyUserZoom:
1564 validPrimitive = false; 1582 validPrimitive = false;
1565 break; 1583 break;
1566
1567 case CSSPropertyScrollSnapPointsX: 1584 case CSSPropertyScrollSnapPointsX:
1568 case CSSPropertyScrollSnapPointsY: 1585 case CSSPropertyScrollSnapPointsY:
1569 parsedValue = parseScrollSnapPoints(); 1586 parsedValue = parseScrollSnapPoints();
1570 break; 1587 break;
1571 case CSSPropertyScrollSnapCoordinate: 1588 case CSSPropertyScrollSnapCoordinate:
1572 parsedValue = parseScrollSnapCoordinate(); 1589 parsedValue = parseScrollSnapCoordinate();
1573 break; 1590 break;
1574 case CSSPropertyScrollSnapDestination: 1591 case CSSPropertyScrollSnapDestination:
1575 parsedValue = parsePosition(m_valueList); 1592 parsedValue = parsePosition(m_valueList);
1576 break; 1593 break;
1577
1578 default: 1594 default:
1579 return parseSVGValue(propId, important); 1595 return parseSVGValue(propId, important);
1580 } 1596 }
1581 1597
1582 if (validPrimitive) { 1598 if (validPrimitive) {
1583 parsedValue = parseValidPrimitive(id, value); 1599 parsedValue = parseValidPrimitive(id, value);
1584 m_valueList->next(); 1600 m_valueList->next();
1585 } 1601 }
1586 ASSERT(!m_parsedCalculation); 1602 ASSERT(!m_parsedCalculation);
1587 if (parsedValue) { 1603 if (parsedValue) {
(...skipping 3348 matching lines...) Expand 10 before | Expand all | Expand 10 after
4936 } 4952 }
4937 4953
4938 4954
4939 bool CSSPropertyParser::isCalculation(CSSParserValue* value) 4955 bool CSSPropertyParser::isCalculation(CSSParserValue* value)
4940 { 4956 {
4941 return (value->unit == CSSParserValue::Function) 4957 return (value->unit == CSSParserValue::Function)
4942 && (value->function->id == CSSValueCalc 4958 && (value->function->id == CSSValueCalc
4943 || value->function->id == CSSValueWebkitCalc); 4959 || value->function->id == CSSValueWebkitCalc);
4944 } 4960 }
4945 4961
4962 bool CSSPropertyParser::isVariableReference(CSSParserValue* value)
4963 {
4964 return value->unit == CSSParserValue::VariableValue;
4965 }
alancutter (OOO until 2018) 2015/07/14 06:12:57 Inlining this check will make it clearer that the
4966
4946 inline int CSSPropertyParser::colorIntFromValue(CSSParserValue* v) 4967 inline int CSSPropertyParser::colorIntFromValue(CSSParserValue* v)
4947 { 4968 {
4948 bool isPercent; 4969 bool isPercent;
4949 double value; 4970 double value;
4950 4971
4951 if (m_parsedCalculation) { 4972 if (m_parsedCalculation) {
4952 isPercent = m_parsedCalculation->category() == CalcPercent; 4973 isPercent = m_parsedCalculation->category() == CalcPercent;
4953 value = m_parsedCalculation->doubleValue(); 4974 value = m_parsedCalculation->doubleValue();
4954 m_parsedCalculation.release(); 4975 m_parsedCalculation.release();
4955 } else { 4976 } else {
(...skipping 2616 matching lines...) Expand 10 before | Expand all | Expand 10 after
7572 7593
7573 // If just one value is supplied, the second value 7594 // If just one value is supplied, the second value
7574 // is implicitly initialized with the first value. 7595 // is implicitly initialized with the first value.
7575 if (numValues == 1) 7596 if (numValues == 1)
7576 m_valueList->previous(); 7597 m_valueList->previous();
7577 7598
7578 return parseViewportProperty(second, important); 7599 return parseViewportProperty(second, important);
7579 } 7600 }
7580 7601
7581 template <typename CharacterType> 7602 template <typename CharacterType>
7603 static bool isVariableDefinition(const CharacterType* propertyName, unsigned len gth)
7604 {
7605 return (length >= 2 && propertyName[0] == '-' && propertyName[1] == '-');
7606 }
7607
7608 template <typename CharacterType>
7582 static CSSPropertyID unresolvedCSSPropertyID(const CharacterType* propertyName, unsigned length) 7609 static CSSPropertyID unresolvedCSSPropertyID(const CharacterType* propertyName, unsigned length)
7583 { 7610 {
7584 char buffer[maxCSSPropertyNameLength + 1]; // 1 for null character 7611 char buffer[maxCSSPropertyNameLength + 1]; // 1 for null character
7585 7612
7586 for (unsigned i = 0; i != length; ++i) { 7613 for (unsigned i = 0; i != length; ++i) {
7587 CharacterType c = propertyName[i]; 7614 CharacterType c = propertyName[i];
7588 if (c == 0 || c >= 0x7F) 7615 if (c == 0 || c >= 0x7F) {
7616 if (isVariableDefinition(propertyName, length))
7617 return CSSPropertyVariable;
7589 return CSSPropertyInvalid; // illegal character 7618 return CSSPropertyInvalid; // illegal character
7619 }
7590 buffer[i] = toASCIILower(c); 7620 buffer[i] = toASCIILower(c);
7591 } 7621 }
7592 buffer[length] = '\0'; 7622 buffer[length] = '\0';
7593 7623
7594 const char* name = buffer; 7624 const char* name = buffer;
7595 const Property* hashTableEntry = findProperty(name, length); 7625 const Property* hashTableEntry = findProperty(name, length);
7596 if (!hashTableEntry) 7626 if (!hashTableEntry) {
7627 if (isVariableDefinition(propertyName, length))
7628 return CSSPropertyVariable;
7597 return CSSPropertyInvalid; 7629 return CSSPropertyInvalid;
7630 }
7598 CSSPropertyID property = static_cast<CSSPropertyID>(hashTableEntry->id); 7631 CSSPropertyID property = static_cast<CSSPropertyID>(hashTableEntry->id);
7599 if (!CSSPropertyMetadata::isEnabledProperty(property)) 7632 if (!CSSPropertyMetadata::isEnabledProperty(property))
7600 return CSSPropertyInvalid; 7633 return CSSPropertyInvalid;
7601 return property; 7634 return property;
7602 } 7635 }
7603 7636
7604 CSSPropertyID unresolvedCSSPropertyID(const String& string) 7637 CSSPropertyID unresolvedCSSPropertyID(const String& string)
7605 { 7638 {
7606 unsigned length = string.length(); 7639 unsigned length = string.length();
7607 7640
7608 if (!length) 7641 if (!length)
7609 return CSSPropertyInvalid; 7642 return CSSPropertyInvalid;
7610 if (length > maxCSSPropertyNameLength) 7643 if (length > maxCSSPropertyNameLength) {
7644 if (string.is8Bit() ? isVariableDefinition(string.characters8(), length) : isVariableDefinition(string.characters16(), length))
7645 return CSSPropertyVariable;
7611 return CSSPropertyInvalid; 7646 return CSSPropertyInvalid;
7647 }
7612 7648
7613 return string.is8Bit() ? unresolvedCSSPropertyID(string.characters8(), lengt h) : unresolvedCSSPropertyID(string.characters16(), length); 7649 return string.is8Bit() ? unresolvedCSSPropertyID(string.characters8(), lengt h) : unresolvedCSSPropertyID(string.characters16(), length);
7614 } 7650 }
7615 7651
7616 CSSPropertyID unresolvedCSSPropertyID(const CSSParserString& string) 7652 CSSPropertyID unresolvedCSSPropertyID(const CSSParserString& string)
7617 { 7653 {
7618 unsigned length = string.length(); 7654 unsigned length = string.length();
7619 7655
7620 if (!length) 7656 if (!length)
7621 return CSSPropertyInvalid; 7657 return CSSPropertyInvalid;
7622 if (length > maxCSSPropertyNameLength) 7658 if (length > maxCSSPropertyNameLength) {
7659 if (string.is8Bit() ? isVariableDefinition(string.characters8(), length) : isVariableDefinition(string.characters16(), length))
7660 return CSSPropertyVariable;
7623 return CSSPropertyInvalid; 7661 return CSSPropertyInvalid;
7662 }
7624 7663
7625 return string.is8Bit() ? unresolvedCSSPropertyID(string.characters8(), lengt h) : unresolvedCSSPropertyID(string.characters16(), length); 7664 return string.is8Bit() ? unresolvedCSSPropertyID(string.characters8(), lengt h) : unresolvedCSSPropertyID(string.characters16(), length);
7626 } 7665 }
7627 7666
7628 template <typename CharacterType> 7667 template <typename CharacterType>
7629 static CSSValueID cssValueKeywordID(const CharacterType* valueKeyword, unsigned length) 7668 static CSSValueID cssValueKeywordID(const CharacterType* valueKeyword, unsigned length)
7630 { 7669 {
7631 char buffer[maxCSSValueKeywordLength + 1]; // 1 for null character 7670 char buffer[maxCSSValueKeywordLength + 1]; // 1 for null character
7632 7671
7633 for (unsigned i = 0; i != length; ++i) { 7672 for (unsigned i = 0; i != length; ++i) {
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
8246 } 8285 }
8247 } 8286 }
8248 8287
8249 if (!list->length()) 8288 if (!list->length())
8250 return nullptr; 8289 return nullptr;
8251 8290
8252 return list.release(); 8291 return list.release();
8253 } 8292 }
8254 8293
8255 } // namespace blink 8294 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698