attributeParser function

Data? attributeParser({
  1. required Parser parser,
  2. required Data parentData,
  3. required Map<String, Object> allData,
  4. required bool debug,
})

Parses HTML attributes from DOM elements based on CSS selectors Returns Data object with extracted attribute values or null if not found

Implementation

Data? attributeParser({
  required Parser parser,
  required Data parentData,
  required Map<String, Object> allData,
  required bool debug,
}) {
  printLog("----------------------------------", debug, color: LogColor.yellow);
  printLog("ID: ${parser.id} Parser: Attribute", debug, color: LogColor.cyan);

  // Get parent element(s) to search within
  List<Element>? element = getElementObject(parentData);
  if (element == null || element.isEmpty) {
    printLog(
      "Attribute Parser: Element not found!",
      debug,
      color: LogColor.red,
    );
    return null;
  }

  // Handle single element (multiple elements not supported)
  Element document;
  if (element.length == 1) {
    document = element[0];
  } else {
    throw UnimplementedError("Multiple elements not supported");
  }

  // Try each selector until data is found
  for (final sel in parser.selectors) {
    printLog("Attribute Selector: $sel", debug, color: LogColor.cyan);
    String selector;

    // Handle dynamic selectors with slot injection
    if (sel.contains("<slot>")) {
      selector = inject("slot", allData, sel);
      printLog(
        "Attribute Selector Modified: $selector",
        debug,
        color: LogColor.green,
      );
    } else {
      selector = sel;
    }

    // Split selector into CSS selector and attribute name
    List<String> split = selector.toString().split("::");

    // Extract attribute value using the handler
    Object? data = attrHandler(
      parser,
      document,
      selectr: split[0],
      attr: split[1],
    );

    // Return first non-empty result
    if (data != null && data != "") {
      return Data(parentData.url, data);
    }
  }

  printLog(
    "Attribute Parser: No data found!",
    debug,
    color: LogColor.orange,
  );
  return null;
}