parse static method

List<CssSelector> parse(
  1. String selector
)

Implementation

static List<CssSelector> parse(String selector) {
  var results = <CssSelector>[];
  void addResult(List<CssSelector> res, CssSelector cssSel) {
    if (cssSel.notSelectors.isNotEmpty &&
        cssSel.element == null &&
        cssSel.classNames.isEmpty &&
        cssSel.attrs.isEmpty) {
      cssSel.element = '*';
    }
    res.add(cssSel);
  }

  var cssSelector = CssSelector();
  var matcher = _selectorRegExp.allMatches(selector);
  var current = cssSelector;
  var inNot = false;
  for (var match in matcher) {
    if (match[1] != null) {
      if (inNot) {
        throw StateError('Nesting :not is not allowed in a selector');
      }
      inNot = true;
      current = CssSelector();
      cssSelector.notSelectors.add(current);
    }
    if (match[2] != null) {
      current.setElement(match[2]);
    }
    if (match[3] != null) {
      current.addClassName(match[3]!);
    }
    if (match[4] != null) {
      current.addAttribute(match[4]!, match[5], match[7]);
    }
    if (match[8] != null) {
      inNot = false;
      current = cssSelector;
    }
    if (match[9] != null) {
      if (inNot) {
        throw StateError('Multiple selectors in :not are not supported');
      }
      addResult(results, cssSelector);
      cssSelector = current = CssSelector();
    }
  }
  addResult(results, cssSelector);
  return results;
}