Skip to content

Commit 98d4e4d

Browse files
authored
[html] fix TypeError in nth-child query selector (#2015)
1 parent 11a7719 commit 98d4e4d

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

pkgs/html/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.15.6-wip
2+
3+
- Fixed a TypeError in nth-child with non numeric value (e.g. `nth-child(even)`)
4+
15
## 0.15.5+1
26

37
- Support "ambiguous ampersand" in attribute values.

pkgs/html/lib/src/query_selector.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,17 @@ class SelectorEvaluator extends Visitor {
218218
final exprs = node.expression.expressions;
219219
if (exprs.length == 1 && exprs[0] is LiteralTerm) {
220220
final literal = exprs[0] as LiteralTerm;
221+
222+
if (literal.value is! num) {
223+
// non numeric values (e.g. `nth-child(even)`) are not supported
224+
return false;
225+
}
226+
227+
final numericLiteral = literal.value as num;
221228
final parent = _element!.parentNode;
222229
return parent != null &&
223-
(literal.value as num) > 0 &&
224-
parent.nodes.indexOf(_element) == literal.value;
230+
numericLiteral > 0 &&
231+
parent.nodes.indexOf(_element) == numericLiteral;
225232
}
226233
break;
227234

pkgs/html/pubspec.yaml

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

0 commit comments

Comments
 (0)