getElementAttributes function

(ElementSize, double?, Alignment) getElementAttributes(
  1. Node node,
  2. BuildContext context
)

Implementation

(
  ElementSize elementSize,
  double? margin,
  Alignment alignment,
) getElementAttributes(
  Node node,
  BuildContext context,
) {
  var elementSize = const ElementSize(null, null);
  var elementAlignment = Alignment.center;
  double? elementMargin;

  final heightValue = parseCssPropertyAsDouble(
    node.style.attributes[Attribute.height.key]?.value.toString() ?? '',
    context: context,
  );
  final widthValue = parseCssPropertyAsDouble(
    node.style.attributes[Attribute.width.key]?.value.toString() ?? '',
    context: context,
  );

  if (heightValue != null) {
    elementSize = elementSize.copyWith(
      height: heightValue,
    );
  }
  if (widthValue != null) {
    elementSize = elementSize.copyWith(
      width: widthValue,
    );
  }

  final cssStyle = node.style.attributes['style'];

  if (cssStyle != null) {
    // It css value as string but we will try to support it anyway

    final cssAttrs = parseCssString(cssStyle.value.toString());

    final cssHeightValue = parseCssPropertyAsDouble(
      (cssAttrs[Attribute.height.key]) ?? '',
      context: context,
    );
    final cssWidthValue = parseCssPropertyAsDouble(
      (cssAttrs[Attribute.width.key]) ?? '',
      context: context,
    );

    // cssHeightValue != null && elementSize.height == null
    if (cssHeightValue != null) {
      elementSize = elementSize.copyWith(height: cssHeightValue);
    }
    if (cssWidthValue != null) {
      elementSize = elementSize.copyWith(width: cssWidthValue);
    }

    elementAlignment = getAlignment(cssAttrs['alignment']);

    final margin = double.tryParse('margin');
    if (margin != null) {
      elementMargin = margin;
    }
  }

  return (elementSize, elementMargin, elementAlignment);
}