attrHandler function
Handles attribute extraction for single or multiple elements Returns attribute value(s) or null if not found
Implementation
Object? attrHandler(
Parser parser,
Element source, {
required String selectr,
required String attr,
}) {
// Handle multiple elements mode
if (parser.multiple) {
List<Element> selector = source.querySelectorAll(selectr);
if (selector.isNotEmpty) {
List<String> result = [];
// Extract attribute from each matching element
for (final sel in selector) {
String? attribute = sel.attributes[attr];
if (attribute != null) {
result.add(attribute.toString());
}
}
return result;
}
} else {
// Handle single element mode
Element? selector = source.querySelector(selectr);
// Special case: use source element itself
if (selectr == '_self') {
selector = source;
}
if (selector != null) {
String? attribute = selector.attributes[attr];
if (attribute != null) {
return attribute.toString();
}
}
}
return null;
}