from static method

OPTIONElement? from(
  1. Object? entry
)
override

Creates a DOMNode from dynamic parameter entry.

entry Can be a DOMNode, a String with HTML, a Text, a Function or an external element.

Implementation

static OPTIONElement? from(Object? entry) {
  if (entry == null) return null;

  if (_domHTML.isHtmlNode(entry)) {
    entry = _domHTML.toDOMNode(entry);
  }

  if (entry is OPTIONElement) return entry;

  if (entry is DOMElement) {
    _checkTag('option', entry);
    return OPTIONElement(
      attributes: entry._attributes,
      value: entry.value,
      label: entry.getAttributeValue('label'),
      selected: entry.getAttributeValueAsBool('selected'),
      text: entry.text,
    );
  }

  Object? value;
  Object? text;

  if (entry is String || entry is num || entry is bool) {
    value = text = entry.toString();
  } else if (entry is TextNode) {
    value = text = entry.text;
  } else if (entry is MapEntry) {
    value = entry.key;
    text = entry.value;
  } else if (entry is Pair) {
    value = entry.a;
    text = entry.b;
  } else if (entry is Iterable) {
    entry = entry.asList;
    if (entry.length == 1) {
      value = text = entry.first?.toString();
    } else if (entry.length >= 2) {
      var l = entry.toList();
      value = l[0];
      text = l[1];
    }
  } else {
    value = text = entry.toString();
  }

  var valueStr = parseString(value);
  var textStr = parseString(text);

  if (isNotEmptyString(valueStr, trim: true) ||
      isNotEmptyString(textStr, trim: true)) {
    return OPTIONElement(
      value: valueStr,
      text: textStr,
    );
  }

  return null;
}