setProperty method

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

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

Implementation

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

  // Null or empty value means should be removed.
  if (CSSStyleDeclaration.isNullOrEmptyValue(value)) {
    final PropertyType resolvedType = propertyType ?? _defaultPropertyType;

    // Clearing an inline declaration should never clobber an already-staged
    // stylesheet value (e.g. during style recomputation where inlineStyle may
    // transiently carry empty entries). If the current winner isn't inline,
    // treat this as a no-op.
    if (resolvedType == PropertyType.inline) {
      final CSSPropertyValue? existing = _getEffectivePropertyValueEntry(propertyName);
      if (existing != null && existing.propertyType != PropertyType.inline) {
        final CSSPropertyValue? staged = _pendingProperties[propertyName];
        if (staged != null && staged.propertyType == PropertyType.inline) {
          _pendingProperties.remove(propertyName);
        }
        return;
      }
    }

    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 = _pendingProperties[propertyName] ?? _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;
  }

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