findAll method

  1. @override
List<Bs4Element> findAll(
  1. String name, {
  2. String? id,
  3. String? class_,
  4. Map<String, Object>? attrs,
  5. Pattern? regex,
  6. Pattern? string,
  7. String? selector,
  8. int? limit,
})
inherited

Looks through a tag’s descendants and retrieves all descendants that match your filters.

Filters:

- name, the tag name, use asterisk (*) to search any tag.

- class_, search by CSS class. Remember that a single tag can have multiple values for its “class” attribute. When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes.

For example for HTML '<p class="body strikeout"></p>'

  1. class_: 'body' ... matches

  2. class_: 'body strikeout' ... matches

  3. class_: 'strikeout body' ... does not match (different order)

- id, search by 'id' attribute.

- attrs, for specifying the attributes of a tag, where key is name of the attribute and value is the value of the attribute (the only allowed types of the value are String or bool). Use true as value of an attribute to search for any attribute values.

- regex, search for element names, via Pattern.

For example: regex: r'^b' ... will search for <b>, <body>, etc. elements.

- string, search for strings (text) inside of elements, via Pattern.

For example: string: r'^Article #\d*'

- limit, to take only a certain number of results.

For example:

bs.findAll('p', class_: 'story'); // finds all "p" elements which have defined "story" attribute with "story" value
bs.findAll('p', attrs: {'title': 'Copy'}); // finds all "p" elements which have defined "title" attribute with "Copy" value
bs.findAll('*', attrs: {'class': true}); // finds all elements of any tag which have defined "class" attribute
bs.findAll(customSelector: '.nav_bar'); // find all with custom selector

You can provide your own CSS [selector](https://drafts.csswg.org/selectors-4/#overview) [selector], if it is specified then both [name] and [attrs] will be ignored. If such selector is not implemented this method will throw [UnimplementedError].

Use true as an attribute value to search any value.

Implementation

@override
List<Bs4Element> findAll(
  String name, {
  String? id,
  String? class_,
  Map<String, Object>? attrs,
  Pattern? regex,
  Pattern? string,
  String? selector,
  int? limit,
}) {
  assert(limit == null || limit >= 0);

  if (selector != null) {
    return ((element ?? doc).querySelectorAll(selector) as List<Element>)
        .map((e) => e.bs4)
        .toList();
  }
  bool anyTag = _isAnyTag(name);
  bool validTag = _isValidTag(name);
  if (attrs == null && !anyTag && validTag) {
    final elements =
        ((element ?? doc).querySelectorAll(name) as List<Element>)
            .map((e) => e.bs4)
            .toList();
    final filtered = _filterResults(
      allResults: elements.toList(),
      id: id,
      class_: class_,
      regex: regex,
      string: string,
    );
    return _limitedList(filtered, limit);
  }
  final cssSelector = ((!validTag || anyTag) && (attrs == null))
      ? '*'
      : _selectorBuilder(tagName: validTag ? name : '*', attrs: attrs!);
  final elements =
      ((element ?? doc).querySelectorAll(cssSelector) as List<Element>)
          .map((e) => e.bs4);

  final filtered = _filterResults(
    allResults: elements.toList(),
    id: id,
    class_: class_,
    regex: regex,
    string: string,
  );
  return _limitedList(filtered, limit);
}