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}) {
  final Map<String, CSSPropertyValue> incomingPending =
      declaration._pendingProperties;
  if (incomingPending.isEmpty) {
    return;
  }

  for (final MapEntry<String, CSSPropertyValue> entry in incomingPending.entries) {
    final String propertyName = entry.key;
    final bool otherIsImportant = declaration._importants[propertyName] ?? false;
    if (otherIsImportant != important) continue;

    final bool currentIsImportant = _importants[propertyName] ?? false;
    final CSSPropertyValue? currentValue =
        _pendingProperties[propertyName] ?? _properties[propertyName];
    final CSSPropertyValue otherValue = entry.value;

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