Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuerySelector on elements behaving like querySelectors on full document #34

Closed
Aulig opened this issue Mar 23, 2021 · 3 comments
Closed

Comments

@Aulig
Copy link

Aulig commented Mar 23, 2021

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
Copy link
Author

Aulig commented Mar 23, 2021

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
Copy link
Member

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

@Aulig
Copy link
Author

Aulig commented Mar 25, 2021

Looks good, thanks for your effort!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants