Skip to content

Commit e0d4979

Browse files
authored
Improve pseudo-class error message (#270)
* Improve pseudo-class error message When encountering a pseudo-class that is unrecognized, we should not state it is "unsupported" a it may be entirely invalid. While there are some pseudo-classes that are valid but not supported, the status of such things can change all the time and our internal list may become outdated. Additionally, sometimes users trying to incorrectly specify tags with namespaces can trigger this message as they may assume that because a namespace is declared in their document as `ns:element` that they can do this in CSS as well. This specific case is not easily detectable for us as we generically parse the syntax, not the intent of the syntax. To clear up confusion, be more specific and simply state that anytime a pseudo-class is not recognized by soupsieve that it is either invalid or not recognizable by soupsieve. By stating both possibilities, this points out that pseudo-class syntax was noted and that it is unrecognized by soupsieve, regardless of status, and may be entirely invalid. Additionally, note that if it being recognized as a pseudo-class is a surprise that the colon can be escaped to avoid the recognition as such. * Update test to accommodate change in error and use SelectorSyntaxError
1 parent c811bdf commit e0d4979

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

docs/src/markdown/about/changelog.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## 2.6
44

5-
- **NEW** Add support for `&` as scoping root per the CSS Nesting Module, Level 1. When `&` is used outside the
5+
- **NEW**: Add support for `&` as scoping root per the CSS Nesting Module, Level 1. When `&` is used outside the
66
context of nesting, it is treated as the scoping root (equivalent to `:scope`).
7+
- **FIX**: Improve error message when an unrecognized pseudo-class is used.
78

89
## 2.5
910

soupsieve/css_parser.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,11 @@ def parse_pseudo_class(
655655
m.start(0)
656656
)
657657
else:
658-
raise NotImplementedError(
659-
f"'{pseudo}' pseudo-class is not implemented at this time"
658+
raise SelectorSyntaxError(
659+
f"'{pseudo}' was detected as a pseudo-class and is either unsupported or invalid. "
660+
"If the syntax was not intended to be recognized as a pseudo-class, please escape the colon.",
661+
self.pattern,
662+
m.start(0)
660663
)
661664

662665
return has_selector, is_html
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test general pseudo-class cases."""
22
from .. import util
3+
from soupsieve import SelectorSyntaxError
34

45

56
class TestPseudoClass(util.TestCase):
@@ -8,9 +9,9 @@ class TestPseudoClass(util.TestCase):
89
def test_pseudo_class_not_implemented(self):
910
"""Test pseudo-class that is not implemented."""
1011

11-
self.assert_raises(':not-implemented', NotImplementedError)
12+
self.assert_raises(':not-implemented', SelectorSyntaxError)
1213

1314
def test_unrecognized_pseudo(self):
1415
"""Test unrecognized pseudo class."""
1516

16-
self.assert_raises(':before', NotImplementedError)
17+
self.assert_raises(':before', SelectorSyntaxError)

0 commit comments

Comments
 (0)