Skip to content

[html] fix TypeError in nth-child query selector #2015

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/html/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.15.6-wip

- Fixed a TypeError in nth-child with non numeric value (e.g. `nth-child(even)`)

## 0.15.5+1

- Support "ambiguous ampersand" in attribute values.
Expand Down
11 changes: 9 additions & 2 deletions pkgs/html/lib/src/query_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,17 @@ class SelectorEvaluator extends Visitor {
final exprs = node.expression.expressions;
if (exprs.length == 1 && exprs[0] is LiteralTerm) {
final literal = exprs[0] as LiteralTerm;

if (literal.value is! num) {
// non numeric values (e.g. `nth-child(even)`) are not supported
return false;
}

final numericLiteral = literal.value as num;
final parent = _element!.parentNode;
return parent != null &&
(literal.value as num) > 0 &&
parent.nodes.indexOf(_element) == literal.value;
numericLiteral > 0 &&
parent.nodes.indexOf(_element) == numericLiteral;
}
break;

Expand Down
2 changes: 1 addition & 1 deletion pkgs/html/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: html
version: 0.15.5+1
version: 0.15.6-wip
description: APIs for parsing and manipulating HTML content outside the browser.
repository: https://github.com/dart-lang/tools/tree/main/pkgs/html
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Ahtml
Expand Down