I'm trying to get element with class nav by selector .nav
. As result lib throw exception:
only type selectors are implemented.
Code example:
import 'package:html5lib/parser.dart';
void main() {
String html = '''
<html>
<head><title>Some title</title></head>
<body>
<div class="nav">Some nav content</div>
</body>
</html>
''';
var doc = parse(html);
print(doc.query('.nav'));
}
Console output:
Breaking on exception: UnimplementedError: only type selectors are implemented
Unhandled exception:
UnimplementedError: only type selectors are implemented
#0 Node._typeSelector (package:html5lib/dom.dart:269:7)
#1 Node.query (package:html5lib/dom.dart:249:62)
#2 main (/home/hello/dart/test/bin/simple_exp.dart:14:18)
#3 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:190)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:93)
What is wrong?
Full CSS selector matching has not been implemented yet.
Depending on your use case, you may be able to match on the type (tag name), and then filter the results using the Element's class attribute.
Here is an example of how to do that:
Hopefully someone will implement the query selector matching support soon - because this work around is very inefficient for documents with a lot of div tags.
Another solution is to do your own traversal - something like this: