Skip to content

Commit 751c57b

Browse files
authored
Fix :nth-child/:nth-of-type matching for An+B index boundaries (#297)
1 parent 08e9ede commit 751c57b

3 files changed

Lines changed: 39 additions & 37 deletions

File tree

‎docs/src/markdown/about/changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ icon: lucide/scroll-text
77

88
- **NEW**: Drop Python 3.9 support.
99
- **NEW**: Lazy compile selector patterns to improve initial import speed.
10+
- **FIX**: Correct `:nth-child`/`:nth-of-type` (and `-last-` variants) for `An+B` values whose sequence steps onto index 0 or onto the last child (e.g. `:nth-child(2n-2)`, `:nth-child(n-1)`, `:nth-child(n+5)`), which previously matched the wrong elements or nothing at all (@gaoflow).
1011

1112
## 2.8.4
1213

‎soupsieve/css_match.py‎

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -995,44 +995,18 @@ def match_nth(self, el: bs4.Tag, nth: tuple[ct.SelectorNth, ...]) -> bool:
995995

996996
# We can only adjust bounds within a variable index
997997
if var:
998-
# Abort if our nth index is out of bounds and only getting further out of bounds as we increment.
999-
# Otherwise, increment to try to get in bounds.
1000-
adjust = None
1001-
while idx < 1 or idx > last_index:
1002-
if idx < 0:
1003-
diff_low = 0 - idx
1004-
if adjust is not None and adjust == 1:
1005-
break
1006-
adjust = -1
1007-
count += count_incr
1008-
idx = last_idx = a * count + b if var else a
1009-
diff = 0 - idx
1010-
if diff >= diff_low:
1011-
break
1012-
else:
1013-
diff_high = idx - last_index
1014-
if adjust is not None and adjust == -1:
1015-
break
1016-
adjust = 1
1017-
count += count_incr
1018-
idx = last_idx = a * count + b if var else a
1019-
diff = idx - last_index
1020-
if diff >= diff_high:
1021-
break
1022-
diff_high = diff
1023-
1024-
# If a < 0, our count is working backwards, so floor the index by increasing the count.
1025-
# Find the count that yields the lowest, in bound value and use that.
1026-
# Lastly reverse count increment so that we'll increase our index.
1027-
lowest = count
1028-
if a < 0:
1029-
while idx >= 1:
1030-
lowest = count
1031-
count += count_incr
1032-
idx = last_idx = a * count + b if var else a
998+
# Find the count `n` that yields the smallest in-bounds index
999+
# (>= 1), then set the increment direction so that the index
1000+
# ascends from there as the evaluation loop below walks children.
1001+
if a > 0:
1002+
# Ascending sequence: smallest n with a * n + b >= 1.
1003+
count = 0 if b >= 1 else -(-(1 - b) // a)
1004+
elif a < 0:
1005+
# Descending sequence: largest n with a * n + b >= 1, then
1006+
# walk n back down so the index increases.
1007+
count = (b - 1) // -a if b >= 1 else 0
10331008
count_incr = -1
1034-
count = lowest
1035-
idx = last_idx = a * count + b if var else a
1009+
idx = last_idx = a * count + b
10361010

10371011
# Evaluate elements while our calculated nth index is still in range
10381012
while 1 <= idx <= last_index + 1:

‎tests/test_level3/test_nth_child.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,30 @@ def test_nth_child_with_bad_parameters(self):
244244
"""Test that pseudo class fails with bad parameters (basically it doesn't match)."""
245245

246246
self.assert_raises(':nth-child(a)', SelectorSyntaxError)
247+
248+
def test_nth_child_an_plus_b_boundaries(self):
249+
"""Test `An+B` forms whose sequence steps onto index 0 or the last child."""
250+
251+
markup = """
252+
<body>
253+
<i id="0"></i>
254+
<i id="1"></i>
255+
<i id="2"></i>
256+
<i id="3"></i>
257+
<i id="4"></i>
258+
</body>
259+
"""
260+
261+
# `B < 1` sequences whose first valid index is > 1 or is all indices
262+
self.assert_selector(markup, "i:nth-child(2n-2)", ['1', '3'], flags=util.HTML)
263+
self.assert_selector(
264+
markup, "i:nth-child(n-1)", ['0', '1', '2', '3', '4'], flags=util.HTML
265+
)
266+
# `B` equal to the child count (index lands on the last child)
267+
self.assert_selector(markup, "i:nth-child(n+5)", ['4'], flags=util.HTML)
268+
self.assert_selector(markup, "i:nth-child(2n+5)", ['4'], flags=util.HTML)
269+
# same boundaries, counted from the end
270+
self.assert_selector(
271+
markup, "i:nth-last-child(2n-2)", ['1', '3'], flags=util.HTML
272+
)
273+
self.assert_selector(markup, "i:nth-last-child(n+5)", ['0'], flags=util.HTML)

0 commit comments

Comments
 (0)