buildComputedStyle static method

List<CSSComputedStyleProperty> buildComputedStyle(
  1. Element element
)

Implementation

static List<CSSComputedStyleProperty> buildComputedStyle(Element element) {
  List<CSSComputedStyleProperty> computedStyle = [];
  CSSStyleDeclaration style = element.style;
  ElementManager elementManager = element.elementManager;
  double viewportWidth = elementManager.viewportWidth;
  double viewportHeight = elementManager.viewportHeight;
  Size viewportSize = Size(viewportWidth, viewportHeight);

  Element rootElement = elementManager.viewportElement;
  RenderBoxModel rootBoxModel = rootElement.renderBoxModel!;
  double rootFontSize = rootBoxModel.renderStyle.fontSize;
  RenderStyle renderStyle = element.renderBoxModel!.renderStyle;
  double fontSize = renderStyle.fontSize;

  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)) {
      double? len = CSSLength.toDisplayPortValue(
        propertyValue,
        viewportSize: viewportSize,
        rootFontSize: rootFontSize,
        fontSize: fontSize
      );

      propertyValue = len == 0 ? '0' : '${len}px';
    }

    if (propertyName == DISPLAY) {
      propertyValue = element.defaultDisplay;
    }

    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;
}