setShorthandBorder static method

void setShorthandBorder(
  1. Map<String, String?> properties,
  2. String property,
  3. String shorthandValue
)

Implementation

static void setShorthandBorder(Map<String, String?> properties, String property, String shorthandValue) {
  // If the entire shorthand is a single var(...) function, defer parsing to
  // compute time by assigning the same var(...) token to all relevant
  // longhands for the affected edges. Each longhand will later resolve the
  // var to a full shorthand string (e.g., "2px solid red") and extract its
  // own component.
  bool _isEntireVarFunction(String s) {
    final String trimmed = s.trimLeft();
    if (!trimmed.startsWith('var(')) return false;
    int start = trimmed.indexOf('(');
    int depth = 0;
    for (int i = start; i < trimmed.length; i++) {
      final int ch = trimmed.codeUnitAt(i);
      if (ch == 40) {
        depth++;
      } else if (ch == 41) {
        depth--;
        if (depth == 0) {
          final rest = trimmed.substring(i + 1).trim();
          return rest.isEmpty;
        }
      }
    }
    return false;
  }

  if (property == BORDER ||
      property == BORDER_TOP ||
      property == BORDER_RIGHT ||
      property == BORDER_BOTTOM ||
      property == BORDER_LEFT ||
      property == BORDER_INLINE_START ||
      property == BORDER_INLINE_END ||
      property == BORDER_BLOCK_START ||
      property == BORDER_BLOCK_END) {
    if (_isEntireVarFunction(shorthandValue)) {
      // Apply the same var(...) to width/style/color for the specified edges.
      void applyEdge(String edge) {
        switch (edge) {
          case 'top':
            properties[BORDER_TOP_WIDTH] = shorthandValue;
            properties[BORDER_TOP_STYLE] = shorthandValue;
            properties[BORDER_TOP_COLOR] = shorthandValue;
            break;
          case 'right':
            properties[BORDER_RIGHT_WIDTH] = shorthandValue;
            properties[BORDER_RIGHT_STYLE] = shorthandValue;
            properties[BORDER_RIGHT_COLOR] = shorthandValue;
            break;
          case 'bottom':
            properties[BORDER_BOTTOM_WIDTH] = shorthandValue;
            properties[BORDER_BOTTOM_STYLE] = shorthandValue;
            properties[BORDER_BOTTOM_COLOR] = shorthandValue;
            break;
          case 'left':
            properties[BORDER_LEFT_WIDTH] = shorthandValue;
            properties[BORDER_LEFT_STYLE] = shorthandValue;
            properties[BORDER_LEFT_COLOR] = shorthandValue;
            break;
          case 'inlineStart':
            properties[BORDER_INLINE_START_WIDTH] = shorthandValue;
            properties[BORDER_INLINE_START_STYLE] = shorthandValue;
            properties[BORDER_INLINE_START_COLOR] = shorthandValue;
            break;
          case 'inlineEnd':
            properties[BORDER_INLINE_END_WIDTH] = shorthandValue;
            properties[BORDER_INLINE_END_STYLE] = shorthandValue;
            properties[BORDER_INLINE_END_COLOR] = shorthandValue;
            break;
          case 'blockStart':
            properties[BORDER_BLOCK_START_WIDTH] = shorthandValue;
            properties[BORDER_BLOCK_START_STYLE] = shorthandValue;
            properties[BORDER_BLOCK_START_COLOR] = shorthandValue;
            break;
          case 'blockEnd':
            properties[BORDER_BLOCK_END_WIDTH] = shorthandValue;
            properties[BORDER_BLOCK_END_STYLE] = shorthandValue;
            properties[BORDER_BLOCK_END_COLOR] = shorthandValue;
            break;
        }
      }

      if (property == BORDER || property == BORDER_TOP) applyEdge('top');
      if (property == BORDER || property == BORDER_RIGHT) applyEdge('right');
      if (property == BORDER || property == BORDER_BOTTOM) applyEdge('bottom');
      if (property == BORDER || property == BORDER_LEFT) applyEdge('left');
      if (property == BORDER_INLINE_START) applyEdge('inlineStart');
      if (property == BORDER_INLINE_END) applyEdge('inlineEnd');
      if (property == BORDER_BLOCK_START) applyEdge('blockStart');
      if (property == BORDER_BLOCK_END) applyEdge('blockEnd');
      return;
    }
  }

  String? borderTopColor;
  String? borderRightColor;
  String? borderBottomColor;
  String? borderLeftColor;
  String? borderTopStyle;
  String? borderRightStyle;
  String? borderBottomStyle;
  String? borderLeftStyle;
  String? borderTopWidth;
  String? borderRightWidth;
  String? borderBottomWidth;
  String? borderLeftWidth;
  String? borderInlineStartWidth;
  String? borderInlineStartStyle;
  String? borderInlineStartColor;
  String? borderInlineEndWidth;
  String? borderInlineEndStyle;
  String? borderInlineEndColor;
  String? borderBlockStartWidth;
  String? borderBlockStartStyle;
  String? borderBlockStartColor;
  String? borderBlockEndWidth;
  String? borderBlockEndStyle;
  String? borderBlockEndColor;

  if (property == BORDER ||
      property == BORDER_TOP ||
      property == BORDER_RIGHT ||
      property == BORDER_BOTTOM ||
      property == BORDER_LEFT ||
      property == BORDER_INLINE_START ||
      property == BORDER_INLINE_END ||
      property == BORDER_BLOCK_START ||
      property == BORDER_BLOCK_END) {
    List<String?>? values = CSSStyleProperty._getBorderValues(shorthandValue);
    if (values == null) return;

    if (property == BORDER || property == BORDER_TOP) {
      borderTopWidth = values[0];
      borderTopStyle = values[1];
      borderTopColor = values[2];
    }
    if (property == BORDER || property == BORDER_RIGHT) {
      borderRightWidth = values[0];
      borderRightStyle = values[1];
      borderRightColor = values[2];
    }
    if (property == BORDER || property == BORDER_BOTTOM) {
      borderBottomWidth = values[0];
      borderBottomStyle = values[1];
      borderBottomColor = values[2];
    }
    if (property == BORDER || property == BORDER_LEFT) {
      borderLeftWidth = values[0];
      borderLeftStyle = values[1];
      borderLeftColor = values[2];
    }
    // Logical properties for LTR mode
    if (property == BORDER_INLINE_START) {
      borderInlineStartWidth = values[0];
      borderInlineStartStyle = values[1];
      borderInlineStartColor = values[2];
    }
    if (property == BORDER_INLINE_END) {
      borderInlineEndWidth = values[0];
      borderInlineEndStyle = values[1];
      borderInlineEndColor = values[2];
    }
    if (property == BORDER_BLOCK_START) {
      borderBlockStartWidth = values[0];
      borderBlockStartStyle = values[1];
      borderBlockStartColor = values[2];
    }
    if (property == BORDER_BLOCK_END) {
      borderBlockEndWidth = values[0];
      borderBlockEndStyle = values[1];
      borderBlockEndColor = values[2];
    }
  } else if (property == BORDER_WIDTH) {
    List<String?>? values = getEdgeValues(shorthandValue);
    if (values == null) return;

    borderTopWidth = values[0];
    borderRightWidth = values[1];
    borderBottomWidth = values[2];
    borderLeftWidth = values[3];
  } else if (property == BORDER_STYLE) {
    // @TODO: validate value
    List<String?>? values = getEdgeValues(shorthandValue);
    if (values == null) return;

    borderTopStyle = values[0];
    borderRightStyle = values[1];
    borderBottomStyle = values[2];
    borderLeftStyle = values[3];
  } else if (property == BORDER_COLOR) {
    // @TODO: validate value
    List<String?>? values = getEdgeValues(shorthandValue);
    if (values == null) return;

    borderTopColor = values[0];
    borderRightColor = values[1];
    borderBottomColor = values[2];
    borderLeftColor = values[3];
  }

  if (borderTopColor != null) properties[BORDER_TOP_COLOR] = borderTopColor;
  if (borderRightColor != null) properties[BORDER_RIGHT_COLOR] = borderRightColor;
  if (borderBottomColor != null) properties[BORDER_BOTTOM_COLOR] = borderBottomColor;
  if (borderLeftColor != null) properties[BORDER_LEFT_COLOR] = borderLeftColor;
  if (borderTopStyle != null) properties[BORDER_TOP_STYLE] = borderTopStyle;
  if (borderRightStyle != null) properties[BORDER_RIGHT_STYLE] = borderRightStyle;
  if (borderBottomStyle != null) properties[BORDER_BOTTOM_STYLE] = borderBottomStyle;
  if (borderLeftStyle != null) properties[BORDER_LEFT_STYLE] = borderLeftStyle;
  if (borderTopWidth != null) properties[BORDER_TOP_WIDTH] = borderTopWidth;
  if (borderRightWidth != null) properties[BORDER_RIGHT_WIDTH] = borderRightWidth;
  if (borderBottomWidth != null) properties[BORDER_BOTTOM_WIDTH] = borderBottomWidth;
  if (borderLeftWidth != null) properties[BORDER_LEFT_WIDTH] = borderLeftWidth;

  // Logical properties
  if (borderInlineStartWidth != null) properties[BORDER_INLINE_START_WIDTH] = borderInlineStartWidth;
  if (borderInlineStartStyle != null) properties[BORDER_INLINE_START_STYLE] = borderInlineStartStyle;
  if (borderInlineStartColor != null) properties[BORDER_INLINE_START_COLOR] = borderInlineStartColor;
  if (borderInlineEndWidth != null) properties[BORDER_INLINE_END_WIDTH] = borderInlineEndWidth;
  if (borderInlineEndStyle != null) properties[BORDER_INLINE_END_STYLE] = borderInlineEndStyle;
  if (borderInlineEndColor != null) properties[BORDER_INLINE_END_COLOR] = borderInlineEndColor;
  if (borderBlockStartWidth != null) properties[BORDER_BLOCK_START_WIDTH] = borderBlockStartWidth;
  if (borderBlockStartStyle != null) properties[BORDER_BLOCK_START_STYLE] = borderBlockStartStyle;
  if (borderBlockStartColor != null) properties[BORDER_BLOCK_START_COLOR] = borderBlockStartColor;
  if (borderBlockEndWidth != null) properties[BORDER_BLOCK_END_WIDTH] = borderBlockEndWidth;
  if (borderBlockEndStyle != null) properties[BORDER_BLOCK_END_STYLE] = borderBlockEndStyle;
  if (borderBlockEndColor != null) properties[BORDER_BLOCK_END_COLOR] = borderBlockEndColor;
}