find function

Component find(
  1. String selector
)

Find an element by its selector

Implementation

Component find(String selector) {
  try {
    return stack.where((c) {
      if (c.element == null) {
        return false;
      }

      if (selector.startsWith("#")) {
        return c.element!.id == selector.substring(1);
      }

      if (selector.startsWith(".")) {
        return c.element!.classList.contains(selector.substring(1));
      }

      return c.element!.tagName.toLowerCase() == selector.toLowerCase();
    }).first;
  } catch (err) {
    throw Exception('Component with selector $selector not found');
  }
}