validateElement method

List<String> validateElement(
  1. dynamic node
)

Implementation

List<String> validateElement(dynamic node) {
  List<String> errors = [];
  if (node is! XmlElement) return errors;

  final tagName = node.name.local;
  final syntax = _rules[tagName];

  if (syntax == null) return errors;

  final parentNode = node.parent;
  if (parentNode is XmlElement && syntax.possibleParents.isNotEmpty) {
    if (!syntax.possibleParents.contains(parentNode.name.local)) {
      errors.add(
        'Element <$tagName> has invalid parent <${parentNode.name.local}>.',
      );
    }
  }

  final attributes = {for (var a in node.attributes) a.name.local: a.value};

  syntax.attributes.forEach((attrName, flag) {
    final isRequired =
        flag == attrId ||
        flag == attrRequired ||
        flag == attrRequiredNonemptyName ||
        flag == attrIdref;
    if (isRequired && !attributes.containsKey(attrName)) {
      errors.add(
        'Missing required attribute "$attrName" for element <$tagName>.',
      );
    }
  });

  for (var attrName in attributes.keys) {
    if (!syntax.attributes.containsKey(attrName)) {
      if (tagName == 'ncl' && attrName.startsWith('xmlns')) continue;
      errors.add('Unknown attribute "$attrName" for element <$tagName>.');
    }
  }

  return errors;
}