findNextParsedAll method

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

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

The findNextParsedAll method returns all matches, and findNextParsed 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> findNextParsedAll({
  RegExp? pattern,
  int? nodeType,
  int? limit,
}) {
  assert(limit == null || limit >= 0);

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

  final filtered = bs4NextParsedAll.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);
}