find method

List<MixinFound> find (Selector selector, [ VariableMixin self, Function filter ])

Returns the List of Rules that matchs selector. The results are cached.

Function: bool filter(rule)

Implementation

List<MixinFound> find(Selector selector,
    [VariableMixin self, Function filter]) {
  final String key = selector.toCSS(null); // ' selector'
  final List<MixinFound> rules = <MixinFound>[];
  final VariableMixin _self = self ?? this;

  if (_lookups.containsKey(key)) return _lookups[key];

  //List of MixinDefinition and Ruleset
  rulesets().forEach((Node rule) {
    if (rule != _self) {
      for (int j = 0; j < rule.selectors.length; j++) {
        final int match = selector
            .match(rule.selectors[j]); // Selectors matchs number. 0 not match
        if (match > 0) {
          if (selector.elements.length > match) {
            if (filter == null || filter(rule)) {
              final List<MixinFound> foundMixins = (rule as VariableMixin)
                  .find(Selector(selector.elements.sublist(match)), _self,
                      filter);
              for (int i = 0; i < foundMixins.length; i++) {
                foundMixins[i]
                    .path
                    .add(rule); //2.3.1 MixinDefinition, Ruleset
              }
              rules.addAll(foundMixins);
            }
          } else {
            rules.add(MixinFound(rule, <Node>[]));
          }
          break;
        }
      }
    }
  });
  _lookups[key] = rules;
  return rules;

//2.3.1
//  Ruleset.prototype.find = function (selector, self, filter) {
//      self = self || this;
//      var rules = [], match, foundMixins,
//          key = selector.toCSS();
//
//      if (key in this._lookups) { return this._lookups[key]; }
//
//      this.rulesets().forEach(function (rule) {
//          if (rule !== self) {
//              for (var j = 0; j < rule.selectors.length; j++) {
//                  match = selector.match(rule.selectors[j]);
//                  if (match) {
//                      if (selector.elements.length > match) {
//                        if (!filter || filter(rule)) {
//                          foundMixins = rule.find(new Selector(selector.elements.slice(match)), self, filter);
//                          for (var i = 0; i < foundMixins.length; ++i) {
//                            foundMixins[i].path.push(rule);
//                          }
//                          Array.prototype.push.apply(rules, foundMixins);
//                        }
//                      } else {
//                          rules.push({ rule: rule, path: []});
//                      }
//                      break;
//                  }
//              }
//          }
//      });
//      this._lookups[key] = rules;
//      return rules;
//  };
}