addElementsClasses function

bool addElementsClasses(
  1. Iterable<Element> elements,
  2. Iterable<String> classes
)

Add to elements a set of classes. Will remove classes starting with '!'.

Implementation

bool addElementsClasses(Iterable<Element> elements, Iterable<String> classes) {
  if (isEmptyObject(classes)) return false;

  var initialClasses =
      Set<String>.from(classes.where((c) => isNotEmptyString(c)));
  if (initialClasses.isEmpty) return false;

  var changedAny = false;

  for (var className in initialClasses) {
    className = className.trim();
    var remove = className.startsWith('!');
    if (remove) className = className.substring(1);

    for (var element in elements) {
      bool changed;
      if (remove) {
        changed = element.classes.remove(className);
      } else {
        changed = element.classes.add(className);
      }

      if (changed) changedAny = true;
    }
  }

  return changedAny;
}