findPreviousParsedAll method

  1. @override
List<Node> findPreviousParsedAll({
  1. RegExp? pattern,
  2. int? nodeType,
  3. int? limit,
})
inherited

These methods use previousParsed to iterate over the tags, comments, strings, etc. that came before it in the document.

The findPreviousParsedAll method returns all matches, and findPreviousParsed only returns the first match.

Filters:

- pattern, to search only for occurrences that satisfy the RegExp match.

- nodeType, what type of node/element to search.

  • Node.ATTRIBUTE_NODE = 2;
  • Node.CDATA_SECTION_NODE = 4;
  • Node.COMMENT_NODE = 8;
  • Node.DOCUMENT_FRAGMENT_NODE = 11;
  • Node.DOCUMENT_NODE = 9;
  • Node.DOCUMENT_TYPE_NODE = 10;
  • Node.ELEMENT_NODE = 1;
  • Node.ENTITY_NODE = 6;
  • Node.ENTITY_REFERENCE_NODE = 5;
  • Node.NOTATION_NODE = 12;
  • Node.PROCESSING_INSTRUCTION_NODE = 7;
  • Node.TEXT_NODE = 3;
// find all url links within the text node
final nextParsed = bs.findNextParsed(
  pattern: RegExp(r'.*(.com)'),
  nodeType: Node.TEXT_NODE,
);

Implementation

@override
List<Node> findPreviousParsedAll({
  RegExp? pattern,
  int? nodeType,
  int? limit,
}) {
  assert(limit == null || limit >= 0);

  final bs4 = _bs4;
  final bs4PrevParsedAll = bs4.previousParsedAll;
  if (bs4PrevParsedAll.isEmpty) return <Node>[];
  if (pattern == null && nodeType == null) {
    return _limitedList(bs4PrevParsedAll, limit);
  }

  final filtered = bs4PrevParsedAll.where((node) {
    if (pattern != null && nodeType == null) {
      return pattern.hasMatch(node.data);
    } else if (pattern == null && nodeType != null) {
      return nodeType == node.nodeType;
    } else {
      return (nodeType == node.nodeType) && (pattern!.hasMatch(node.data));
    }
  });

  return _limitedList(filtered.toList(), limit);
}