unionByImportance method

void unionByImportance(
  1. CSSStyleDeclaration declaration, {
  2. required bool important,
})

Like union, but only applies declarations matching important.

This is used by cascade layers where !important reverses layer order.

Implementation

void unionByImportance(CSSStyleDeclaration declaration,
    {required bool important}) {
  Map<String, CSSPropertyValue> properties = {}
    ..addAll(_properties)
    ..addAll(_pendingProperties);

  for (String propertyName in declaration._pendingProperties.keys) {
    final bool otherIsImportant =
        declaration._importants[propertyName] ?? false;
    if (otherIsImportant != important) continue;

    final bool currentIsImportant = _importants[propertyName] ?? false;
    final CSSPropertyValue? currentValue = properties[propertyName];
    final CSSPropertyValue? otherValue =
        declaration._pendingProperties[propertyName];

    if ((otherIsImportant || !currentIsImportant) &&
        currentValue != otherValue) {
      if (otherValue != null) {
        _pendingProperties[propertyName] = otherValue;
      } else {
        _pendingProperties.remove(propertyName);
      }
      if (otherIsImportant) {
        _importants[propertyName] = true;
      }
    }
  }
}