setProperty method

void setProperty(
  1. String propertyName,
  2. String? value, {
  3. bool? isImportant,
  4. PropertyType? propertyType,
  5. String? baseHref,
  6. bool validate = true,
})

Modifies an existing CSS property or creates a new CSS property in the declaration block.

Implementation

void setProperty(
  String propertyName,
  String? value, {
  bool? isImportant,
  PropertyType? propertyType,
  String? baseHref,
  bool validate = true,
}) {
  propertyName = normalizePropertyName(propertyName);

  // Null or empty value means should be removed.
  if (CSSStyleDeclaration.isNullOrEmptyValue(value)) {
    removeProperty(propertyName, isImportant);
    return;
  }

  final String rawValue = value.toString();
  final bool isCustomProperty = CSSVariable.isCSSSVariableProperty(propertyName);
  String normalizedValue = isCustomProperty ? rawValue : _toLowerCase(propertyName, rawValue.trim());

  if (validate && !_isValidValue(propertyName, normalizedValue)) return;

  if (_cssShorthandProperty[propertyName] != null) {
    return _expandShorthand(propertyName, normalizedValue, isImportant,
        propertyType: propertyType, baseHref: baseHref, validate: validate);
  }

  PropertyType resolvedType = propertyType ?? _defaultPropertyType;
  bool resolvedImportant = isImportant == true;

  final CSSPropertyValue? existing = _properties[propertyName];
  if (existing != null) {
    final bool existingImportant = existing.important;
    if (existingImportant && !resolvedImportant) {
      return;
    }
    if (existingImportant == resolvedImportant) {
      if (existing.propertyType == PropertyType.inline && resolvedType == PropertyType.sheet) {
        return;
      }
    }
  }

  if (existing != null &&
      existing.value == normalizedValue &&
      existing.important == resolvedImportant &&
      existing.propertyType == resolvedType &&
      (!CSSVariable.isCSSVariableValue(normalizedValue))) {
    return;
  }

  _properties[propertyName] = CSSPropertyValue(
    normalizedValue,
    baseHref: baseHref,
    important: resolvedImportant,
    propertyType: resolvedType,
  );
}