hasElementWhere method

bool hasElementWhere({
  1. String? name,
  2. String? id,
  3. List<String>? attributeNames,
  4. List<XmlAttribute>? attributes,
  5. bool matchAllAttributes = false,
  6. bool attributesMustBeIdentical = false,
  7. List<XmlNode>? children,
  8. bool matchAllChildren = false,
  9. bool childrenMustBeIdentical = false,
  10. bool global = false,
})

Recursively checks all nested elements and returns true if one is found with properties matching those specified.

name and id must not be empty if they are provided.

If attributeNames is not null, only elements posessing an attribute with a name contained in attributeNames will be returned. If matchAllAttributes is true, an element must possess every attribute contained in attributeNames to be returned, if false, the element only needs to posess a single attribute contained in attributeNames.

If attributes isn't null, only elements possessing attributes with an identical name and value as those contained in attributes will be returned. If matchAllAttributes is true, an element must possess every attribute contained in attributes, if false, the element only needs to possess a single attribute contained in attributes.

If children isn't null, only elements possessing children matching those in children will be returned. If matchAllChildren is true, an element must posess every child found in children, if false, the element only needs to posess a single child found in children. If childrenMustBeIdentical is true, the element's children must be in the same order and possess the same number of children as those in children, children will also be compared with the == operator, rather than the compareValues method.

If global is true, only top-level elements will be returned, making this method identical to hasChildWhere.

Implementation

bool hasElementWhere({
  String? name,
  String? id,
  List<String>? attributeNames,
  List<XmlAttribute>? attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode>? children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
  bool global = false,
}) {
  assert(name == null || name.isNotEmpty);
  assert(id == null || id.isNotEmpty);

  if (this.children == null) return false;

  name = name?.toLowerCase();

  for (var child in children!) {
    if (child is XmlElement) {
      if (compareValues(
        child,
        name: name,
        id: id,
        attributeNames: attributeNames,
        attributes: attributes,
        matchAllAttributes: matchAllAttributes,
        attributesMustBeIdentical: attributesMustBeIdentical,
        children: children,
        matchAllChildren: matchAllChildren,
        childrenMustBeIdentical: childrenMustBeIdentical,
      )) {
        return true;
      }

      if (!global) continue;

      final elementIsNested = child.hasElementWhere(
        name: name,
        id: id,
        attributeNames: attributeNames,
        attributes: attributes,
        matchAllAttributes: matchAllAttributes,
        attributesMustBeIdentical: attributesMustBeIdentical,
        children: children,
        matchAllChildren: matchAllChildren,
        childrenMustBeIdentical: childrenMustBeIdentical,
      );

      if (elementIsNested) return true;
    }
  }

  return false;
}