handlePseudoRules method

void handlePseudoRules(
  1. Element parentElement,
  2. List<CSSStyleRule> rules
)

Implementation

void handlePseudoRules(Element parentElement, List<CSSStyleRule> rules) {
  if (rules.isEmpty) return;

  List<CSSStyleRule> beforeRules = [];
  List<CSSStyleRule> afterRules = [];

  for (CSSStyleRule style in rules) {
    for (Selector selector in style.selectorGroup.selectors) {
      for (SimpleSelectorSequence sequence in selector.simpleSelectorSequences) {
        if (sequence.simpleSelector is PseudoElementSelector) {
          if (sequence.simpleSelector.name == 'before') {
            beforeRules.add(style);
          } else if (sequence.simpleSelector.name == 'after') {
            afterRules.add(style);
          }
        }
      }
    }
  }

  int sortRules(leftRule, rightRule) {
    int isCompare = leftRule.selectorGroup.matchSpecificity.compareTo(rightRule.selectorGroup.matchSpecificity);
    if (isCompare == 0) {
      return leftRule.position.compareTo(rightRule.position);
    }
    return isCompare;
  }

  // sort selector
  beforeRules.sort(sortRules);
  afterRules.sort(sortRules);

  if (beforeRules.isNotEmpty) {
    pseudoBeforeStyle ??= CSSStyleDeclaration();
    // Merge all the rules
    for (CSSStyleRule rule in beforeRules) {
      pseudoBeforeStyle!.union(rule.declaration);
    }
    parentElement.markBeforePseudoElementNeedsUpdate();
  } else if (beforeRules.isEmpty && pseudoBeforeStyle != null) {
    pseudoBeforeStyle = null;
  }

  if (afterRules.isNotEmpty) {
    pseudoAfterStyle ??= CSSStyleDeclaration();
    for (CSSStyleRule rule in afterRules) {
      pseudoAfterStyle!.union(rule.declaration);
    }
    parentElement.markAfterPseudoElementNeedsUpdate();
  } else if (afterRules.isEmpty && pseudoAfterStyle != null) {
    pseudoAfterStyle = null;
  }
}