dint-dev / universal_html

Cross-platform 'dart:html'.
https://pub.dev/packages/universal_html
Apache License 2.0
201 stars 63 forks source link

QuerySelector on elements behaving like querySelectors on full document #34

Closed Aulig closed 3 years ago

Aulig commented 3 years ago

Hi, I migrated to null-safety yesterday and in the process upgraded universal_html. I'm now encountering an issue when using querySelectors on elements (not the document). It seems like the querySelector is executed on almost the entire document instead of just the element and its children.

The regular html package produces the expected result.

import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:universal_html/parsing.dart';
import 'package:universal_html/html.dart' as uhtml;

main() async {

    String html = (await get(Uri.parse("https://github.com/"))).body;

    print("html:");
    print(parse(html).querySelectorAll("a").length);
    print(parse(html).querySelector("nav").querySelectorAll("a").length);

    print("universal_html:");
    print(parseHtmlDocument(html).querySelectorAll("a").length);
    print(parseHtmlDocument(html).querySelector("nav").querySelectorAll("a").length);

    // workaround
    print(uhtml.Element.html(parseHtmlDocument(html).querySelector("nav").outerHtml).querySelectorAll("a").length);
}

Output:

html:
119
29
universal_html:
119
114
Removing disallowed element ...
29

Am I doing something wrong or is this an intended change? I'd rather not have to use the workaround as it seems like a performance issue. The other alternative would be changing my code to chain the selectors like "nav a" where html and universal_html provide the same result. (Not a 1:1 replacement though as there can be multiple nav elements)

Aulig commented 3 years ago

It looks like the issue was introduced in the upgrade from 1.2.4 to 2.0.0. Before that, html and universal_html behave the same in this example.

terrier989 commented 3 years ago

Thanks for the issue report! It has been fixed in v2.0.5.

Aulig commented 3 years ago

Looks good, thanks for your effort!