| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "core/css/properties/CSSPropertyAlignmentUtils.h" | 5 #include "core/css/properties/CSSPropertyAlignmentUtils.h" |
| 6 | 6 |
| 7 #include "core/css/CSSContentDistributionValue.h" |
| 7 #include "core/css/CSSValuePair.h" | 8 #include "core/css/CSSValuePair.h" |
| 8 #include "core/css/parser/CSSPropertyParserHelpers.h" | 9 #include "core/css/parser/CSSPropertyParserHelpers.h" |
| 9 | 10 |
| 10 namespace blink { | 11 namespace blink { |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 CSSIdentifierValue* consumeSelfPositionKeyword(CSSParserTokenRange& range) { | 15 CSSIdentifierValue* consumeSelfPositionKeyword(CSSParserTokenRange& range) { |
| 15 CSSValueID id = range.peek().id(); | 16 CSSValueID id = range.peek().id(); |
| 16 if (id == CSSValueStart || id == CSSValueEnd || id == CSSValueCenter || | 17 if (id == CSSValueStart || id == CSSValueEnd || id == CSSValueCenter || |
| 17 id == CSSValueSelfStart || id == CSSValueSelfEnd || | 18 id == CSSValueSelfStart || id == CSSValueSelfEnd || |
| 18 id == CSSValueFlexStart || id == CSSValueFlexEnd || id == CSSValueLeft || | 19 id == CSSValueFlexStart || id == CSSValueFlexEnd || id == CSSValueLeft || |
| 19 id == CSSValueRight) | 20 id == CSSValueRight) |
| 20 return CSSPropertyParserHelpers::consumeIdent(range); | 21 return CSSPropertyParserHelpers::consumeIdent(range); |
| 21 return nullptr; | 22 return nullptr; |
| 22 } | 23 } |
| 23 | 24 |
| 25 bool isContentDistributionKeyword(CSSValueID id) { |
| 26 return CSSPropertyParserHelpers::identMatches< |
| 27 CSSValueSpaceBetween, CSSValueSpaceAround, CSSValueSpaceEvenly, |
| 28 CSSValueStretch>(id); |
| 29 } |
| 30 |
| 31 bool isContentPositionKeyword(CSSValueID id) { |
| 32 return CSSPropertyParserHelpers::identMatches< |
| 33 CSSValueStart, CSSValueEnd, CSSValueCenter, CSSValueFlexStart, |
| 34 CSSValueFlexEnd, CSSValueLeft, CSSValueRight>(id); |
| 35 } |
| 36 |
| 37 bool isOverflowKeyword(CSSValueID id) { |
| 38 return CSSPropertyParserHelpers::identMatches<CSSValueUnsafe, CSSValueSafe>( |
| 39 id); |
| 40 } |
| 41 |
| 42 bool isBaselineKeyword(CSSValueID id) { |
| 43 return CSSPropertyParserHelpers::identMatches<CSSValueFirst, CSSValueLast, |
| 44 CSSValueBaseline>(id); |
| 45 } |
| 46 |
| 24 CSSValue* consumeBaselineKeyword(CSSParserTokenRange& range) { | 47 CSSValue* consumeBaselineKeyword(CSSParserTokenRange& range) { |
| 25 CSSValueID id = range.peek().id(); | 48 CSSValueID id = range.peek().id(); |
| 26 if (CSSPropertyParserHelpers::identMatches<CSSValueBaseline>(id)) | 49 if (CSSPropertyParserHelpers::identMatches<CSSValueBaseline>(id)) |
| 27 return CSSPropertyParserHelpers::consumeIdent(range); | 50 return CSSPropertyParserHelpers::consumeIdent(range); |
| 28 | 51 |
| 29 if (CSSIdentifierValue* preference = | 52 if (CSSIdentifierValue* preference = |
| 30 CSSPropertyParserHelpers::consumeIdent<CSSValueFirst, CSSValueLast>( | 53 CSSPropertyParserHelpers::consumeIdent<CSSValueFirst, CSSValueLast>( |
| 31 range)) { | 54 range)) { |
| 32 if (range.peek().id() == CSSValueBaseline) { | 55 if (range.peek().id() == CSSValueBaseline) { |
| 33 return CSSValuePair::create(preference, | 56 return CSSValuePair::create(preference, |
| 34 CSSPropertyParserHelpers::consumeIdent(range), | 57 CSSPropertyParserHelpers::consumeIdent(range), |
| 35 CSSValuePair::DropIdenticalValues); | 58 CSSValuePair::DropIdenticalValues); |
| 36 } | 59 } |
| 37 } | 60 } |
| 38 return nullptr; | 61 return nullptr; |
| 39 } | 62 } |
| 40 | 63 |
| 41 } // namespace | 64 } // namespace |
| 42 | 65 |
| 43 CSSValue* CSSPropertyAlignmentUtils::consumeSelfPositionOverflowPosition( | 66 CSSValue* CSSPropertyAlignmentUtils::consumeSelfPositionOverflowPosition( |
| 44 CSSParserTokenRange& range) { | 67 CSSParserTokenRange& range) { |
| 45 if (CSSPropertyParserHelpers::identMatches<CSSValueAuto, CSSValueNormal, | 68 if (CSSPropertyParserHelpers::identMatches<CSSValueAuto, CSSValueNormal, |
| 46 CSSValueStretch>( | 69 CSSValueStretch>( |
| 47 range.peek().id())) | 70 range.peek().id())) |
| 48 return CSSPropertyParserHelpers::consumeIdent(range); | 71 return CSSPropertyParserHelpers::consumeIdent(range); |
| 49 | 72 |
| 50 if (CSSPropertyParserHelpers::identMatches<CSSValueFirst, CSSValueLast, | 73 if (isBaselineKeyword(range.peek().id())) |
| 51 CSSValueBaseline>( | |
| 52 range.peek().id())) | |
| 53 return consumeBaselineKeyword(range); | 74 return consumeBaselineKeyword(range); |
| 54 | 75 |
| 55 CSSIdentifierValue* overflowPosition = | 76 CSSIdentifierValue* overflowPosition = |
| 56 CSSPropertyParserHelpers::consumeIdent<CSSValueUnsafe, CSSValueSafe>( | 77 CSSPropertyParserHelpers::consumeIdent<CSSValueUnsafe, CSSValueSafe>( |
| 57 range); | 78 range); |
| 58 CSSIdentifierValue* selfPosition = consumeSelfPositionKeyword(range); | 79 CSSIdentifierValue* selfPosition = consumeSelfPositionKeyword(range); |
| 59 if (!selfPosition) | 80 if (!selfPosition) |
| 60 return nullptr; | 81 return nullptr; |
| 61 if (!overflowPosition) { | 82 if (!overflowPosition) { |
| 62 overflowPosition = | 83 overflowPosition = |
| 63 CSSPropertyParserHelpers::consumeIdent<CSSValueUnsafe, CSSValueSafe>( | 84 CSSPropertyParserHelpers::consumeIdent<CSSValueUnsafe, CSSValueSafe>( |
| 64 range); | 85 range); |
| 65 } | 86 } |
| 66 if (overflowPosition) { | 87 if (overflowPosition) { |
| 67 return CSSValuePair::create(selfPosition, overflowPosition, | 88 return CSSValuePair::create(selfPosition, overflowPosition, |
| 68 CSSValuePair::DropIdenticalValues); | 89 CSSValuePair::DropIdenticalValues); |
| 69 } | 90 } |
| 70 return selfPosition; | 91 return selfPosition; |
| 71 } | 92 } |
| 72 | 93 |
| 94 CSSValue* CSSPropertyAlignmentUtils::consumeContentDistributionOverflowPosition( |
| 95 CSSParserTokenRange& range) { |
| 96 CSSValueID id = range.peek().id(); |
| 97 if (CSSPropertyParserHelpers::identMatches<CSSValueNormal>(id)) { |
| 98 return CSSContentDistributionValue::create( |
| 99 CSSValueInvalid, range.consumeIncludingWhitespace().id(), |
| 100 CSSValueInvalid); |
| 101 } |
| 102 |
| 103 if (isBaselineKeyword(id)) { |
| 104 CSSValue* baseline = consumeBaselineKeyword(range); |
| 105 if (!baseline) |
| 106 return nullptr; |
| 107 CSSValueID baselineID = CSSValueBaseline; |
| 108 if (baseline->isValuePair()) { |
| 109 if (toCSSIdentifierValue(toCSSValuePair(baseline)->first()) |
| 110 .getValueID() == CSSValueLast) { |
| 111 baselineID = CSSValueLastBaseline; |
| 112 } else { |
| 113 baselineID = CSSValueFirstBaseline; |
| 114 } |
| 115 } |
| 116 return CSSContentDistributionValue::create(CSSValueInvalid, baselineID, |
| 117 CSSValueInvalid); |
| 118 } |
| 119 |
| 120 CSSValueID distribution = CSSValueInvalid; |
| 121 CSSValueID position = CSSValueInvalid; |
| 122 CSSValueID overflow = CSSValueInvalid; |
| 123 do { |
| 124 if (isContentDistributionKeyword(id)) { |
| 125 if (distribution != CSSValueInvalid) |
| 126 return nullptr; |
| 127 distribution = id; |
| 128 } else if (isContentPositionKeyword(id)) { |
| 129 if (position != CSSValueInvalid) |
| 130 return nullptr; |
| 131 position = id; |
| 132 } else if (isOverflowKeyword(id)) { |
| 133 if (overflow != CSSValueInvalid) |
| 134 return nullptr; |
| 135 overflow = id; |
| 136 } else { |
| 137 return nullptr; |
| 138 } |
| 139 range.consumeIncludingWhitespace(); |
| 140 id = range.peek().id(); |
| 141 } while (!range.atEnd()); |
| 142 |
| 143 // The grammar states that we should have at least <content-distribution> or |
| 144 // <content-position>. |
| 145 if (position == CSSValueInvalid && distribution == CSSValueInvalid) |
| 146 return nullptr; |
| 147 |
| 148 // The grammar states that <overflow-position> must be associated to |
| 149 // <content-position>. |
| 150 if (overflow != CSSValueInvalid && position == CSSValueInvalid) |
| 151 return nullptr; |
| 152 |
| 153 return CSSContentDistributionValue::create(distribution, position, overflow); |
| 154 } |
| 155 |
| 73 } // namespace blink | 156 } // namespace blink |
| OLD | NEW |