Skip to content

Commit ce44e49

Browse files
authored
Merge commit from fork
1 parent 751c57b commit ce44e49

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ icon: lucide/scroll-text
88
- **NEW**: Drop Python 3.9 support.
99
- **NEW**: Lazy compile selector patterns to improve initial import speed.
1010
- **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).
11+
- **FIX**: More efficient CSS ID matching (@kaimandalic).
1112

1213
## 2.8.4
1314

‎soupsieve/css_parser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
CSS_STRING_ESCAPES = fr'(?:\\(?:[a-f0-9]{{1,6}}{WS}?|[^\r\n\f]|$|{NEWLINE}))'
121121
# CSS Identifier
122122
IDENTIFIER = fr'''
123-
(?:(?:-?(?:[^\x00-\x2f\x30-\x40\x5B-\x5E\x60\x7B-\x9f]|{CSS_ESCAPES})+|--)
123+
(?:(?:--|-?(?:[^\x00-\x2f\x30-\x40\x5B-\x5E\x60\x7B-\x9f]|{CSS_ESCAPES}))
124124
(?:[^\x00-\x2c\x2e\x2f\x3A-\x40\x5B-\x5E\x60\x7B-\x9f]|{CSS_ESCAPES})*)
125125
'''
126126
# `nth` content

‎tests/test_performance.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Test performance cases."""
2+
import unittest
3+
import sys
4+
import signal
5+
import time
6+
import soupsieve as sv
7+
from soupsieve.util import SelectorSyntaxError
8+
9+
10+
class Timeout(Exception):
11+
"""Timeout exception."""
12+
13+
14+
@unittest.skipUnless(not sys.platform.startswith('win'), "Unix/Linux test")
15+
class TestPerformance(unittest.TestCase):
16+
"""Test performance."""
17+
18+
def assert_performance(self, selector):
19+
"""Test performance of of specific cases."""
20+
21+
LIMIT = 5.0
22+
23+
signal.signal(signal.SIGALRM, lambda *_: (_ for _ in ()).throw(Timeout()))
24+
signal.setitimer(signal.ITIMER_REAL, LIMIT)
25+
dt = None
26+
t = 0
27+
try:
28+
t = time.perf_counter()
29+
sv.compile(selector)
30+
dt = time.perf_counter() - t
31+
except SelectorSyntaxError:
32+
dt = time.perf_counter() - t
33+
except Timeout:
34+
pass
35+
finally:
36+
signal.setitimer(signal.ITIMER_REAL, 0)
37+
38+
hung = dt is None
39+
success = not hung and dt < 1.0
40+
41+
if not success:
42+
print('Selector:', selector)
43+
print(f"{'> %.0f s (HANG)' % LIMIT if hung else '%.3f s' % dt}")
44+
45+
self.assertTrue(success)
46+
47+
def test_performance_caase(self):
48+
"""Test performance cases."""
49+
50+
for n in (1000, 2000, 4000, 8000):
51+
self.assert_performance("[a=" + "a" * n)
52+
53+
for n in (2000, 4000, 8000, 16000):
54+
self.assert_performance("a" * n + "!")
55+
56+
self.assert_performance("[a=" + "a" * 12000)

0 commit comments

Comments
 (0)