findPreviousElement method

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

These methods use previousElements to iterate over the tags and strings that came before it in the document.

The findAllPreviousElements method returns all matches, and findPreviousElement 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
Bs4Element? findPreviousElement(
  String name, {
  String? id,
  String? class_,
  Map<String, Object>? attrs,
  Pattern? regex,
  Pattern? string,
  String? selector,
}) {
  final filtered = findAllPreviousElements(
    name,
    attrs: attrs,
    selector: selector,
  );
  return filtered.firstOrNull;
}