@@ -9,6 +9,7 @@ import 'package:flutter/rendering.dart';
99import 'package:kraken/foundation.dart' ;
1010import 'package:kraken/rendering.dart' ;
1111import 'package:kraken/css.dart' ;
12+ import 'package:kraken/src/css/style_property.dart' ;
1213
1314// CSS Box Model: https://drafts.csswg.org/css-box-4/
1415// CSS Backgrounds and Borders: https://drafts.csswg.org/css-backgrounds/
@@ -201,68 +202,56 @@ mixin CSSDecoratedBoxMixin on CSSBackgroundMixin {
201202 }
202203
203204 /// Tip: inset not supported.
204- static RegExp commaRegExp = RegExp (r',' );
205205 List <BoxShadow > getBoxShadow (CSSStyleDeclaration style) {
206206 List <BoxShadow > boxShadow = [];
207- if (style.contains ('boxShadow' )) {
208- String processedValue = CSSColor .preprocessCSSPropertyWithRGBAColor (style['boxShadow' ]);
209- List <String > rawShadows = processedValue.split (commaRegExp);
210- for (String rawShadow in rawShadows) {
211- List <String > shadowDefinitions = rawShadow.trim ().split (_splitRegExp);
212- if (shadowDefinitions.length > 2 ) {
213- double offsetX = CSSLength .toDisplayPortValue (shadowDefinitions[0 ]) ?? 0 ;
214- double offsetY = CSSLength .toDisplayPortValue (shadowDefinitions[1 ]) ?? 0 ;
215- double blurRadius =
216- shadowDefinitions.length > 3 ? CSSLength .toDisplayPortValue (shadowDefinitions[2 ]) ?? 0 : 0.0 ;
217- double spreadRadius =
218- shadowDefinitions.length > 4 ? CSSLength .toDisplayPortValue (shadowDefinitions[3 ]) ?? 0 : 0.0 ;
219-
220- Color color = CSSColor .generate (shadowDefinitions.last);
221- if (color != null ) {
222- boxShadow.add (BoxShadow (
223- offset: Offset (offsetX, offsetY),
224- blurRadius: blurRadius,
225- spreadRadius: spreadRadius,
226- color: color,
227- ));
228- }
207+ if (style.contains (BOX_SHADOW )) {
208+ var shadows = CSSStyleProperty .getShadowValues (style[BOX_SHADOW ]);
209+ shadows.forEach ((shadowDefinitions) {
210+ // Specifies the color of the shadow. If the color is absent, it defaults to currentColor.
211+ Color color = CSSColor .parseColor (shadowDefinitions[0 ] ?? style[COLOR ]);
212+ double offsetX = CSSLength .toDisplayPortValue (shadowDefinitions[1 ]) ?? 0 ;
213+ double offsetY = CSSLength .toDisplayPortValue (shadowDefinitions[2 ]) ?? 0 ;
214+ double blurRadius = CSSLength .toDisplayPortValue (shadowDefinitions[3 ]) ?? 0 ;
215+ double spreadRadius = CSSLength .toDisplayPortValue (shadowDefinitions[4 ]) ?? 0 ;
216+
217+ if (color != null ) {
218+ boxShadow.add (BoxShadow (
219+ offset: Offset (offsetX, offsetY),
220+ blurRadius: blurRadius,
221+ spreadRadius: spreadRadius,
222+ color: color,
223+ ));
229224 }
230- }
225+ });
231226
232227 // Tips only debug.
233228 if (! PRODUCTION && boxShadow.isEmpty) {
234- print ('[Warning] Wrong style format with boxShadow: ${style ['boxShadow' ]}' );
229+ print ('[Warning] Wrong style format with boxShadow: ${style [BOX_SHADOW ]}' );
235230 print (' Correct syntax: inset? && <length>{2,4} && <color>?' );
236231 }
237232 }
233+
238234 return boxShadow;
239235 }
240236
241237 double getBorderRadius (CSSStyleDeclaration style, String side) {
242238 if (style.contains (side)) {
243239 return CSSLength .toDisplayPortValue (style[side]) ?? 0 ;
244- } else if (style.contains ('borderRadius' )) {
245- return CSSLength .toDisplayPortValue (style['borderRadius' ]) ?? 0 ;
240+ } else if (style.contains (BORDER_RADIUS )) {
241+ return CSSLength .toDisplayPortValue (style[BORDER_RADIUS ]) ?? 0 ;
246242 }
247243 return 0.0 ;
248244 }
249245
250- static RegExp _splitRegExp = RegExp (r' ' );
251-
252- static List <String > getShorttedProperties (String input) {
253- assert (input != null );
254- return input.trim ().split (_splitRegExp);
255- }
256-
257246 // border default width 3.0
258247 static double defaultBorderLineWidth = 3.0 ;
259248 static BorderStyle defaultBorderStyle = BorderStyle .none;
260- static Color defaultBorderColor = CSSColor .black ;
249+ static Color defaultBorderColor = CSSColor .initial ;
261250
262251 static BorderStyle getBorderStyle (String input) {
263252 BorderStyle borderStyle;
264253 switch (input) {
265- case 'solid' :
254+ case SOLID :
266255 borderStyle = BorderStyle .solid;
267256 break ;
268257 default :
@@ -272,15 +261,12 @@ mixin CSSDecoratedBoxMixin on CSSBackgroundMixin {
272261 return borderStyle;
273262 }
274263
275- // TODO: Shortted order in web not keep in same order
276- static Map _getShorttedInfoFromString (String input) {
277- List <String > splittedBorder = getShorttedProperties (input);
278-
279- double width = splittedBorder.length > 0 ? CSSLength .toDisplayPortValue (splittedBorder[0 ]) : null ;
280-
281- BorderStyle style = splittedBorder.length > 1 ? getBorderStyle (splittedBorder[1 ]) : null ;
264+ static Map _getShorthandInfo (String input) {
265+ List <String > properties = CSSStyleProperty .getBorderValues (input);
282266
283- Color color = splittedBorder.length > 2 ? CSSColor .generate (splittedBorder[2 ]) : null ;
267+ double width = CSSLength .toDisplayPortValue (properties[0 ]) ?? defaultBorderLineWidth;
268+ BorderStyle style = getBorderStyle (properties[1 ]);
269+ Color color = CSSColor .parseColor (properties[2 ]) ?? defaultBorderColor;
284270
285271 return {'Color' : color, 'Style' : style, 'Width' : width};
286272 }
@@ -294,27 +280,27 @@ mixin CSSDecoratedBoxMixin on CSSBackgroundMixin {
294280 final String widthName = 'Width' ;
295281 final String styleName = 'Style' ;
296282 final String colorName = 'Color' ;
297- Map borderShorttedInfo ;
298- Map borderSideShorttedInfo ;
299- if (style.contains (borderName )) {
300- borderShorttedInfo = _getShorttedInfoFromString (style[borderName ]);
283+ Map borderShorthandInfo ;
284+ Map borderSideShorthandInfo ;
285+ if (style.contains (BORDER )) {
286+ borderShorthandInfo = _getShorthandInfo (style[BORDER ]);
301287 }
302288
303289 if (style.contains (borderSideName)) {
304- borderSideShorttedInfo = _getShorttedInfoFromString (style[borderSideName]);
290+ borderSideShorthandInfo = _getShorthandInfo (style[borderSideName]);
305291 }
306292
307293 // Set border style
308294 final String borderSideStyleName = borderSideName + styleName; // eg. borderLeftStyle/borderRightStyle
309295 final String borderStyleName = borderName + styleName; // borderStyle
310296 if (style.contains (borderSideStyleName)) {
311297 borderSide.borderStyle = getBorderStyle (style[borderSideStyleName]);
312- } else if (borderSideShorttedInfo != null && borderSideShorttedInfo [styleName] != null ) {
313- borderSide.borderStyle = borderSideShorttedInfo [styleName];
298+ } else if (borderSideShorthandInfo != null && borderSideShorthandInfo [styleName] != null ) {
299+ borderSide.borderStyle = borderSideShorthandInfo [styleName];
314300 } else if (style.contains (borderStyleName)) {
315301 borderSide.borderStyle = getBorderStyle (style[borderStyleName]);
316- } else if (borderShorttedInfo != null && borderShorttedInfo [styleName] != null ) {
317- borderSide.borderStyle = borderShorttedInfo [styleName];
302+ } else if (borderShorthandInfo != null && borderShorthandInfo [styleName] != null ) {
303+ borderSide.borderStyle = borderShorthandInfo [styleName];
318304 }
319305
320306 // border width should be zero when style is none
@@ -326,14 +312,14 @@ mixin CSSDecoratedBoxMixin on CSSBackgroundMixin {
326312 final String borderWidthName = borderName + widthName; // borderWidth
327313 if (style.contains (borderSideWidthName) && (style[borderSideWidthName] as String ).isNotEmpty) {
328314 borderSide.borderWidth = CSSLength .toDisplayPortValue (style[borderSideWidthName]) ?? 0 ;
329- } else if (borderSideShorttedInfo != null && borderSideShorttedInfo [widthName] != null ) {
315+ } else if (borderSideShorthandInfo != null && borderSideShorthandInfo [widthName] != null ) {
330316 // eg. borderLeft: 'solid 1px black'
331- borderSide.borderWidth = borderSideShorttedInfo [widthName];
317+ borderSide.borderWidth = borderSideShorthandInfo [widthName];
332318 } else if (style.contains (borderWidthName)) {
333319 borderSide.borderWidth = CSSLength .toDisplayPortValue (style[borderWidthName]) ?? 0 ;
334- } else if (borderShorttedInfo != null && borderShorttedInfo [widthName] != null ) {
320+ } else if (borderShorthandInfo != null && borderShorthandInfo [widthName] != null ) {
335321 // eg. border: 'solid 2px red'
336- borderSide.borderWidth = borderShorttedInfo [widthName];
322+ borderSide.borderWidth = borderShorthandInfo [widthName];
337323 }
338324 }
339325
@@ -342,20 +328,17 @@ mixin CSSDecoratedBoxMixin on CSSBackgroundMixin {
342328 final String borderSideColorName = borderSideName + colorName; // eg. borderLeftColor/borderRightColor
343329 final String borderColorName = borderName + colorName; // borderColor
344330 if (style.contains (borderSideColorName)) {
345- borderColor = CSSColor .generate (style[borderSideColorName]);
346- } else if (borderSideShorttedInfo != null && borderSideShorttedInfo [colorName] != null ) {
347- borderColor = borderSideShorttedInfo [colorName];
331+ borderColor = CSSColor .parseColor (style[borderSideColorName]);
332+ } else if (borderSideShorthandInfo != null && borderSideShorthandInfo [colorName] != null ) {
333+ borderColor = borderSideShorthandInfo [colorName];
348334 } else if (style.contains (borderColorName)) {
349- borderColor = CSSColor .generate (style[borderColorName]);
350- } else if (borderShorttedInfo != null && borderShorttedInfo [colorName] != null ) {
351- borderColor = borderShorttedInfo [colorName];
335+ borderColor = CSSColor .parseColor (style[borderColorName]);
336+ } else if (borderShorthandInfo != null && borderShorthandInfo [colorName] != null ) {
337+ borderColor = borderShorthandInfo [colorName];
352338 }
353339
354340 if (borderColor != null ) {
355- borderSide.color = borderSide.color.withAlpha (borderColor.alpha);
356- borderSide.color = borderSide.color.withRed (borderColor.red);
357- borderSide.color = borderSide.color.withGreen (borderColor.green);
358- borderSide.color = borderSide.color.withBlue (borderColor.blue);
341+ borderSide.color = borderColor;
359342 }
360343
361344 return borderSide;
0 commit comments