findBestRuleSetAndAdd method

void findBestRuleSetAndAdd(
  1. Selector selector,
  2. CSSRule rule
)

Implementation

void findBestRuleSetAndAdd(Selector selector, CSSRule rule) {
  String? id, className, attributeName, tagName, pseudoName;

  for (final simpleSelectorSequence in selector.simpleSelectorSequences.reversed) {
    final simpleSelector = simpleSelectorSequence.simpleSelector;
    if (simpleSelector.runtimeType == IdSelector) {
      id = simpleSelector.name;
    } else if (simpleSelector.runtimeType == ClassSelector) {
      className = simpleSelector.name;
    } else if (simpleSelector.runtimeType == AttributeSelector) {
      attributeName = simpleSelector.name;
    } else if (simpleSelector.runtimeType == ElementSelector) {
      if (simpleSelector.isWildcard) {
        break;
      }
      tagName = simpleSelector.name;
    } else if (simpleSelector.runtimeType == PseudoClassSelector ||
        simpleSelector.runtimeType == PseudoElementSelector ||
        simpleSelector.runtimeType == PseudoClassFunctionSelector) {
      pseudoName = simpleSelector.name;
    }

    if (id != null || className != null || attributeName != null || tagName != null || pseudoName != null) {
      break;
    }
  }

  void insertRule(String key, CSSRule rule, CSSMap map) {
    List<CSSRule>? rules = map[key] ?? [];
    rules.add(rule);
    map[key] = rules;
  }

  if (id != null && id.isNotEmpty == true) {
    insertRule(id, rule, idRules);
    return;
  }

  if (className != null && className.isNotEmpty == true) {
    insertRule(className, rule, classRules);
    return;
  }

  if (attributeName != null && attributeName.isNotEmpty == true) {
    insertRule(attributeName.toUpperCase(), rule, attributeRules);
    return;
  }

  if (tagName != null && tagName.isNotEmpty == true) {
    insertRule(tagName.toUpperCase(), rule, tagRules);
    return;
  }

  if (pseudoName != null && _isLegacyPsuedoClass(pseudoName)) {
    pseudoRules.add(rule);
    return;
  }

  universalRules.add(rule);
}