findAllNextElements method

  1. @override
List<Bs4Element> findAllNextElements(
  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

These methods use nextElements to iterate over elements that come after it in the document.

The findAllNextElements method returns all matches, and findNextElement only returns the first match.

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.
You can provide your own CSS selector 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> findAllNextElements(
  String name, {
  String? id,
  String? class_,
  Map<String, Object>? attrs,
  Pattern? regex,
  Pattern? string,
  String? selector,
  int? limit,
}) {
  assert(limit == null || limit >= 0);

  final matched = <Bs4Element>[];
  final bs4 = _bs4;
  final bs4NextElements = bs4.nextElements;
  if (bs4NextElements.isEmpty) return matched;

  final topElement = _getTopElement(bs4);
  final allResults = _getAllResults(
    topElement: topElement,
    name: name,
    class_: class_,
    id: id,
    attrs: attrs,
    regex: regex,
    string: string,
    selector: selector,
  );

  final filtered = _findMatches(allResults, bs4NextElements);
  matched.addAll(filtered);

  return _limitedList(matched, limit);
}