getAttribute function

String? getAttribute(
  1. Map<String, String> el,
  2. String name, {
  3. String? def = '',
  4. bool checkStyle = true,
})

Gets the attribute, trims it, and returns the attribute or default if the attribute is null or ''.

Will look to the style first if it can.

Implementation

String? getAttribute(
  Map<String, String> el,
  String name, {
  String? def = '',
  bool checkStyle = true,
}) {
  String raw = '';
  if (checkStyle) {
    final String? style = _getAttribute(el, 'style');
    if (style != '' && style != null) {
      // Probably possible to slightly optimize this (e.g. use indexOf instead of split),
      // but handling potential whitespace will get complicated and this just works.
      // I also don't feel like writing benchmarks for what is likely a micro-optimization.
      final List<String> styles = style.split(';');
      raw = styles.firstWhere(
          (String str) => str.trimLeft().startsWith('$name:'),
          orElse: () => '');

      if (raw != '') {
        raw = raw.substring(raw.indexOf(':') + 1).trim();
      }
    }

    if (raw == '') {
      raw = _getAttribute(el, name);
    }
  } else {
    raw = _getAttribute(el, name);
  }

  return raw == '' ? def : raw;
}