parseCSSLength function
Parses a CSS length, using optional unit
.
def
Default value if parse fails or cssValue
isEmptyString.
allowPXWithoutSuffix
Implementation
num? parseCSSLength(String cssValue,
{String? unit, int? def, bool allowPXWithoutSuffix = false}) {
if (isEmptyString(cssValue)) return def;
cssValue = cssValue.toLowerCase().trim();
if (isEmptyString(cssValue)) return def;
if (unit != null) {
unit = unit.toLowerCase().trim();
}
if (isNotEmptyString(unit)) {
if (cssValue.endsWith(unit!)) {
var s = cssValue.substring(0, cssValue.length - unit.length).trim();
return parseNum(s, def);
} else if (allowPXWithoutSuffix && unit == 'px' && isNum(cssValue)) {
return parseNum(cssValue, def);
}
} else {
var match = _patternCssLengthUnit.firstMatch(cssValue);
if (match != null) {
var unit = match.group(1)!;
var s = cssValue.substring(0, cssValue.length - unit.length).trim();
return parseNum(s, def);
}
}
return def;
}