getParent function Null safety

String? getParent(
  1. Document document,
  2. void onError(
    1. JaelError error
    )?
)

Finds the name of the parent template.

Implementation

String? getParent(Document document, void Function(JaelError error)? onError) {
  var element = document.root;
  if (element.tagName.name != 'extend') return null;

  var attr = element.attributes.firstWhereOrNull((a) => a.name == 'src');
  if (attr == null) {
    onError!(JaelError(JaelErrorSeverity.warning,
        'Missing "src" attribute in "extend" tag.', element.tagName.span));
    return null;
  } else if (attr.value is! StringLiteral) {
    onError!(JaelError(
        JaelErrorSeverity.warning,
        'The "src" attribute in an "extend" tag must be a string literal.',
        element.tagName.span));
    return null;
  } else {
    return (attr.value as StringLiteral).value;
  }
}