parseLength static method

CSSLengthValue parseLength(
  1. String text,
  2. RenderStyle? renderStyle, [
  3. String? propertyName,
  4. Axis? axisType,
])

Implementation

static CSSLengthValue parseLength(String text, RenderStyle? renderStyle, [String? propertyName, Axis? axisType]) {
  if (_cachedParsedLength.containsKey(text)) {
    return _cachedParsedLength[text]!;
  }

  double? value;
  CSSLengthType unit = CSSLengthType.PX;
  if (text == ZERO) {
    // Only '0' is accepted with no unit.
    return CSSLengthValue.zero;
  } else if (text == INITIAL) {
    return CSSLengthValue.initial;
  } else if (text == AUTO) {
    return CSSLengthValue.auto;
  } else if (text == NONE) {
    return CSSLengthValue.none;
  } else if (text.endsWith(REM)) {
    value = double.tryParse(text.split(REM)[0]);
    unit = CSSLengthType.REM;
  } else if (text.endsWith(EM)) {
    value = double.tryParse(text.split(EM)[0]);
    unit = CSSLengthType.EM;
  } else if (text.endsWith(RPX)) {
    value = double.tryParse(text.split(RPX)[0]);
    if (value != null) value = value / 750.0 * window.physicalSize.width / window.devicePixelRatio;
  } else if (text.endsWith(PX)) {
    value = double.tryParse(text.split(PX)[0]);
  } else if (text.endsWith(VW)) {
    value = double.tryParse(text.split(VW)[0]);
    if (value != null) value = value / 100;
    unit = CSSLengthType.VW;
  } else if (text.endsWith(VH)) {
    value = double.tryParse(text.split(VH)[0]);
    if (value != null) value = value / 100;
    unit = CSSLengthType.VH;
  } else if (text.endsWith(CM)) {
    value = double.tryParse(text.split(CM)[0]);
    if (value != null) value = value * _1cm;
  } else if (text.endsWith(MM)) {
    value = double.tryParse(text.split(MM)[0]);
    if (value != null) value = value * _1mm;
  } else if (text.endsWith(PC)) {
    value = double.tryParse(text.split(PC)[0]);
    if (value != null) value = value * _1pc;
  } else if (text.endsWith(PT)) {
    value = double.tryParse(text.split(PT)[0]);
    if (value != null) value = value * _1pt;
  } else if (text.endsWith(VMIN)) {
    value = double.tryParse(text.split(VMIN)[0]);
    if (value != null) value = value / 100;
    unit = CSSLengthType.VMIN;
  }  else if (text.endsWith(VMAX)) {
    value = double.tryParse(text.split(VMAX)[0]);
    if (value != null) value = value / 100;
    unit = CSSLengthType.VMAX;
  } else if (text.endsWith(IN)) {
    value = double.tryParse(text.split(IN)[0]);
    if (value != null) value = value * _1in;
  } else if (text.endsWith(Q)) {
    value = double.tryParse(text.split(Q)[0]);
    if (value != null) value = value * _1Q;
  } else if (text.endsWith(PERCENTAGE)) {
    value = double.tryParse(text.split(PERCENTAGE)[0]);
    if (value != null) value = value / 100;
    unit = CSSLengthType.PERCENTAGE;
  } else if (CSSFunction.isFunction(text)) {
    List<CSSFunctionalNotation> notations = CSSFunction.parseFunction(text);
    // https://drafts.csswg.org/css-env/#env-function
    // Using Environment Variables: the env() notation
    if (notations.length == 1 && notations[0].name == ENV && notations[0].args.length == 1) {
      switch (notations[0].args.first) {
        case SAFE_AREA_INSET_TOP:
          value = window.viewPadding.top / window.devicePixelRatio;
          break;
        case SAFE_AREA_INSET_RIGHT:
          value = window.viewPadding.right / window.devicePixelRatio;
          break;
        case SAFE_AREA_INSET_BOTTOM:
          value = window.viewPadding.bottom / window.devicePixelRatio;
          break;
        case SAFE_AREA_INSET_LEFT:
          value = window.viewPadding.left / window.devicePixelRatio;
          break;
        default:
          // Using fallback value if not match user agent-defined environment variable: env(xxx, 50px).
          return parseLength(notations[0].args[1], renderStyle, propertyName, axisType);
      }
    }
  }

  if (value == 0) {
    return _cachedParsedLength[text] = CSSLengthValue.zero;
  } else if (value == null) {
    return _cachedParsedLength[text] = CSSLengthValue.unknown;
  } else if (unit == CSSLengthType.PX){
    return _cachedParsedLength[text] = CSSLengthValue(value, unit);
  } else {
    return CSSLengthValue(value, unit, renderStyle, propertyName, axisType);
  }
}