parseLength static method
CSSLengthValue
parseLength(
- String text,
- RenderStyle? renderStyle, [
- String? propertyName,
- Axis? axisType,
Implementation
static CSSLengthValue parseLength(String text, RenderStyle? renderStyle, [String? propertyName, Axis? axisType]) {
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 == INHERIT) {
if (renderStyle != null && propertyName != null && renderStyle.target.parentElement != null) {
var element = renderStyle.target.parentElement!;
return parseLength(element.style.getPropertyValue(propertyName), element.renderStyle, propertyName, axisType);
}
return CSSLengthValue.zero;
} else if (text == AUTO) {
return CSSLengthValue.auto;
} else if (text == NONE) {
return CSSLengthValue.none;
} else if (text.toLowerCase() == 'content') {
// flex-basis: content
return CSSLengthValue(null, CSSLengthType.CONTENT, renderStyle, propertyName, axisType);
} 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(EX)) {
value = double.tryParse(text.split(EX)[0]);
unit = CSSLengthType.EX;
} else if (text.endsWith(RPX)) {
value = double.tryParse(text.split(RPX)[0]);
unit = CSSLengthType.RPX;
} 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)) {
if (renderStyle != null) {
CSSCalcValue? calcValue = CSSCalcValue.tryParse(renderStyle, propertyName ?? '', text);
if (calcValue != null) {
return CSSLengthValue.calc(calcValue, renderStyle, propertyName);
}
}
} else {
value = double.tryParse(text);
}
if (value == 0 && unit != CSSLengthType.PERCENTAGE) {
return CSSLengthValue.zero;
} else if (value == null) {
return CSSLengthValue.unknown;
} else if (unit == CSSLengthType.PX) {
return CSSLengthValue(value, unit);
} else {
return CSSLengthValue(value, unit, renderStyle, propertyName, axisType);
}
}