compareValues static method

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

Compares element's values with the supplied values and returns true if they match, or false if they don't.

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.

Implementation

static bool compareValues(
  XmlNode element, {
  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,
}) {
  assert(name == null || name.isNotEmpty);
  assert(id == null || id.isNotEmpty);
  assert(attributeNames == null || attributeNames.isNotEmpty);

  if (element is XmlElement) {
    // Compare the names
    if (name != null && element.name.toLowerCase() != name.toLowerCase()) {
      return false;
    }

    // Compare the IDs
    if (id != null && element.id != id) {
      return false;
    }

    // Compare the attributes
    if (attributeNames != null || attributes != null) {
      if (element.attributes == null) return false;

      if (attributesMustBeIdentical &&
          attributes!.length != element.attributes!.length) {
        return false;
      }

      if (attributeNames != null && attributeNames.isNotEmpty) {
        var hasAttributes = (matchAllAttributes) ? attributeNames.length : 1;
        for (var attributeName in attributeNames) {
          if (element.hasAttribute(attributeName)) {
            hasAttributes--;
          }
          if (hasAttributes <= 0) break;
        }
        if (hasAttributes > 0) return false;
      }

      if (attributes != null) {
        var hasAttributes = (matchAllAttributes) ? attributes.length : 1;

        for (var attribute in attributes) {
          if (element.hasAttributeWhere(attribute.name, attribute.value)) {
            hasAttributes--;
          }
        }

        if (hasAttributes > 0) return false;
      }
    }

    // Compare the children
    if (children != null) {
      if (element.children == null) return false;

      if (childrenMustBeIdentical &&
          element.children!.length != children.length) {
        return false;
      }

      var hasChildren =
          (matchAllChildren || childrenMustBeIdentical) ? children.length : 1;

      for (var i = 0; i < children.length; i++) {
        final child = children[i];

        if (child is XmlElement) {
          if (childrenMustBeIdentical) {
            if (child != element.children![i]) {
              return false;
            } else {
              hasChildren--;
            }
          } else {
            if (element.hasChildWhere(
              name: child.name,
              id: child.id,
              attributes: child.attributes,
              matchAllAttributes: matchAllAttributes,
              attributesMustBeIdentical: attributesMustBeIdentical,
              children: child.children,
              matchAllChildren: matchAllChildren,
              childrenMustBeIdentical: childrenMustBeIdentical,
            )) {
              hasChildren--;
            }
          }
        }

        if (hasChildren <= 0) break;
      }

      if (hasChildren > 0) return false;
    }
  } else {
    return false;
  }

  return true;
}