buildComputedStyle static method

List<CSSComputedStyleProperty> buildComputedStyle(
  1. Element element
)

Implementation

static List<CSSComputedStyleProperty> buildComputedStyle(Element element) {
  List<CSSComputedStyleProperty> computedStyle = [];
  CSSStyleDeclaration style = element.style;
  RenderStyle? renderStyle = element.renderStyle;

  for (int i = 0; i < style.length; i++) {
    String propertyName = style.item(i);
    String propertyValue = style.getPropertyValue(propertyName);
    propertyName = _kebabize(propertyName);

    if (CSSLength.isLength(propertyValue)) {
      CSSLengthValue? len = CSSLength.resolveLength(
        propertyValue,
        renderStyle,
        propertyName,
      );

      propertyValue = len == null ? '0' : '${len.computedValue}${_resolveCSSLengthType(len.type)}';
    }

    if (propertyName == DISPLAY) {
      propertyValue = resolveCSSDisplayString(element.renderStyle.display);
    }

    computedStyle.add(CSSComputedStyleProperty(name: propertyName, value: propertyValue));
  }

  if (!style.contains(BORDER_TOP_STYLE)) {
    computedStyle.add(CSSComputedStyleProperty(name: _kebabize(BORDER_TOP_STYLE), value: ZERO_PX));
  }
  if (!style.contains(BORDER_RIGHT_STYLE)) {
    computedStyle.add(CSSComputedStyleProperty(name: _kebabize(BORDER_RIGHT_STYLE), value: ZERO_PX));
  }
  if (!style.contains(BORDER_BOTTOM_STYLE)) {
    computedStyle.add(CSSComputedStyleProperty(name: _kebabize(BORDER_BOTTOM_STYLE), value: ZERO_PX));
  }
  if (!style.contains(BORDER_LEFT_STYLE)) {
    computedStyle.add(CSSComputedStyleProperty(name: _kebabize(BORDER_LEFT_STYLE), value: ZERO_PX));
  }

  // Calc computed size.
  Map<String, dynamic> boundingClientRect = element.boundingClientRect.toJSON();
  boundingClientRect.forEach((String name, value) {
    computedStyle.add(CSSComputedStyleProperty(name: name, value: '${value}px'));
  });

  return computedStyle;
}