| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/css/properties/CSSPropertyShapeUtils.h" |
| 6 |
| 7 #include "core/css/CSSBasicShapeValues.h" |
| 8 #include "core/css/parser/CSSParserContext.h" |
| 9 #include "core/css/parser/CSSParserMode.h" |
| 10 #include "core/css/parser/CSSParserTokenRange.h" |
| 11 #include "core/css/parser/CSSPropertyParserHelpers.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 using namespace CSSPropertyParserHelpers; |
| 16 |
| 17 namespace { |
| 18 |
| 19 static CSSValue* consumeShapeRadius(CSSParserTokenRange& args, |
| 20 CSSParserMode cssParserMode) { |
| 21 if (identMatches<CSSValueClosestSide, CSSValueFarthestSide>(args.peek().id())) |
| 22 return consumeIdent(args); |
| 23 return consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative); |
| 24 } |
| 25 |
| 26 static CSSBasicShapeCircleValue* consumeBasicShapeCircle( |
| 27 CSSParserTokenRange& args, |
| 28 const CSSParserContext* context) { |
| 29 // spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes |
| 30 // circle( [<shape-radius>]? [at <position>]? ) |
| 31 CSSBasicShapeCircleValue* shape = CSSBasicShapeCircleValue::create(); |
| 32 if (CSSValue* radius = consumeShapeRadius(args, context->mode())) |
| 33 shape->setRadius(radius); |
| 34 if (consumeIdent<CSSValueAt>(args)) { |
| 35 CSSValue* centerX = nullptr; |
| 36 CSSValue* centerY = nullptr; |
| 37 if (!consumePosition(args, context->mode(), UnitlessQuirk::Forbid, centerX, |
| 38 centerY)) |
| 39 return nullptr; |
| 40 shape->setCenterX(centerX); |
| 41 shape->setCenterY(centerY); |
| 42 } |
| 43 return shape; |
| 44 } |
| 45 |
| 46 static CSSBasicShapeEllipseValue* consumeBasicShapeEllipse( |
| 47 CSSParserTokenRange& args, |
| 48 const CSSParserContext* context) { |
| 49 // spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes |
| 50 // ellipse( [<shape-radius>{2}]? [at <position>]? ) |
| 51 CSSBasicShapeEllipseValue* shape = CSSBasicShapeEllipseValue::create(); |
| 52 if (CSSValue* radiusX = consumeShapeRadius(args, context->mode())) { |
| 53 shape->setRadiusX(radiusX); |
| 54 if (CSSValue* radiusY = consumeShapeRadius(args, context->mode())) |
| 55 shape->setRadiusY(radiusY); |
| 56 } |
| 57 if (consumeIdent<CSSValueAt>(args)) { |
| 58 CSSValue* centerX = nullptr; |
| 59 CSSValue* centerY = nullptr; |
| 60 if (!consumePosition(args, context->mode(), UnitlessQuirk::Forbid, centerX, |
| 61 centerY)) |
| 62 return nullptr; |
| 63 shape->setCenterX(centerX); |
| 64 shape->setCenterY(centerY); |
| 65 } |
| 66 return shape; |
| 67 } |
| 68 |
| 69 static CSSBasicShapePolygonValue* consumeBasicShapePolygon( |
| 70 CSSParserTokenRange& args, |
| 71 const CSSParserContext* context) { |
| 72 CSSBasicShapePolygonValue* shape = CSSBasicShapePolygonValue::create(); |
| 73 if (identMatches<CSSValueEvenodd, CSSValueNonzero>(args.peek().id())) { |
| 74 shape->setWindRule(args.consumeIncludingWhitespace().id() == CSSValueEvenodd |
| 75 ? RULE_EVENODD |
| 76 : RULE_NONZERO); |
| 77 if (!consumeCommaIncludingWhitespace(args)) |
| 78 return nullptr; |
| 79 } |
| 80 |
| 81 do { |
| 82 CSSPrimitiveValue* xLength = |
| 83 consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 84 if (!xLength) |
| 85 return nullptr; |
| 86 CSSPrimitiveValue* yLength = |
| 87 consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 88 if (!yLength) |
| 89 return nullptr; |
| 90 shape->appendPoint(xLength, yLength); |
| 91 } while (consumeCommaIncludingWhitespace(args)); |
| 92 return shape; |
| 93 } |
| 94 |
| 95 static CSSBasicShapeInsetValue* consumeBasicShapeInset( |
| 96 CSSParserTokenRange& args, |
| 97 const CSSParserContext* context) { |
| 98 CSSBasicShapeInsetValue* shape = CSSBasicShapeInsetValue::create(); |
| 99 CSSPrimitiveValue* top = |
| 100 consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 101 if (!top) |
| 102 return nullptr; |
| 103 CSSPrimitiveValue* right = |
| 104 consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 105 CSSPrimitiveValue* bottom = nullptr; |
| 106 CSSPrimitiveValue* left = nullptr; |
| 107 if (right) { |
| 108 bottom = consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 109 if (bottom) |
| 110 left = consumeLengthOrPercent(args, context->mode(), ValueRangeAll); |
| 111 } |
| 112 if (left) |
| 113 shape->updateShapeSize4Values(top, right, bottom, left); |
| 114 else if (bottom) |
| 115 shape->updateShapeSize3Values(top, right, bottom); |
| 116 else if (right) |
| 117 shape->updateShapeSize2Values(top, right); |
| 118 else |
| 119 shape->updateShapeSize1Value(top); |
| 120 |
| 121 if (consumeIdent<CSSValueRound>(args)) { |
| 122 CSSValue* horizontalRadii[4] = {0}; |
| 123 CSSValue* verticalRadii[4] = {0}; |
| 124 if (!CSSPropertyShapeUtils::consumeRadii(horizontalRadii, verticalRadii, |
| 125 args, context->mode(), false)) |
| 126 return nullptr; |
| 127 shape->setTopLeftRadius( |
| 128 CSSValuePair::create(horizontalRadii[0], verticalRadii[0], |
| 129 CSSValuePair::DropIdenticalValues)); |
| 130 shape->setTopRightRadius( |
| 131 CSSValuePair::create(horizontalRadii[1], verticalRadii[1], |
| 132 CSSValuePair::DropIdenticalValues)); |
| 133 shape->setBottomRightRadius( |
| 134 CSSValuePair::create(horizontalRadii[2], verticalRadii[2], |
| 135 CSSValuePair::DropIdenticalValues)); |
| 136 shape->setBottomLeftRadius( |
| 137 CSSValuePair::create(horizontalRadii[3], verticalRadii[3], |
| 138 CSSValuePair::DropIdenticalValues)); |
| 139 } |
| 140 return shape; |
| 141 } |
| 142 |
| 143 } // namespace |
| 144 |
| 145 bool CSSPropertyShapeUtils::consumeRadii(CSSValue* horizontalRadii[4], |
| 146 CSSValue* verticalRadii[4], |
| 147 CSSParserTokenRange& range, |
| 148 CSSParserMode cssParserMode, |
| 149 bool useLegacyParsing) { |
| 150 unsigned i = 0; |
| 151 for (; i < 4 && !range.atEnd() && range.peek().type() != DelimiterToken; |
| 152 ++i) { |
| 153 horizontalRadii[i] = |
| 154 consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative); |
| 155 if (!horizontalRadii[i]) |
| 156 return false; |
| 157 } |
| 158 if (!horizontalRadii[0]) |
| 159 return false; |
| 160 if (range.atEnd()) { |
| 161 // Legacy syntax: -webkit-border-radius: l1 l2; is equivalent to |
| 162 // border-radius: l1 / l2; |
| 163 if (useLegacyParsing && i == 2) { |
| 164 verticalRadii[0] = horizontalRadii[1]; |
| 165 horizontalRadii[1] = nullptr; |
| 166 } else { |
| 167 complete4Sides(horizontalRadii); |
| 168 for (unsigned i = 0; i < 4; ++i) |
| 169 verticalRadii[i] = horizontalRadii[i]; |
| 170 return true; |
| 171 } |
| 172 } else { |
| 173 if (!consumeSlashIncludingWhitespace(range)) |
| 174 return false; |
| 175 for (i = 0; i < 4 && !range.atEnd(); ++i) { |
| 176 verticalRadii[i] = |
| 177 consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative); |
| 178 if (!verticalRadii[i]) |
| 179 return false; |
| 180 } |
| 181 if (!verticalRadii[0] || !range.atEnd()) |
| 182 return false; |
| 183 } |
| 184 complete4Sides(horizontalRadii); |
| 185 complete4Sides(verticalRadii); |
| 186 return true; |
| 187 } |
| 188 |
| 189 CSSValue* CSSPropertyShapeUtils::consumeBasicShape( |
| 190 CSSParserTokenRange& range, |
| 191 const CSSParserContext* context) { |
| 192 CSSValue* shape = nullptr; |
| 193 if (range.peek().type() != FunctionToken) |
| 194 return nullptr; |
| 195 CSSValueID id = range.peek().functionId(); |
| 196 CSSParserTokenRange rangeCopy = range; |
| 197 CSSParserTokenRange args = consumeFunction(rangeCopy); |
| 198 if (id == CSSValueCircle) |
| 199 shape = consumeBasicShapeCircle(args, context); |
| 200 else if (id == CSSValueEllipse) |
| 201 shape = consumeBasicShapeEllipse(args, context); |
| 202 else if (id == CSSValuePolygon) |
| 203 shape = consumeBasicShapePolygon(args, context); |
| 204 else if (id == CSSValueInset) |
| 205 shape = consumeBasicShapeInset(args, context); |
| 206 if (!shape || !args.atEnd()) |
| 207 return nullptr; |
| 208 range = rangeCopy; |
| 209 return shape; |
| 210 } |
| 211 |
| 212 } // namespace blink |
| OLD | NEW |