Skip to content
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

Invalid pseudo selectors #2445

Merged
merged 2 commits into from
May 1, 2023
Merged
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
8 changes: 8 additions & 0 deletions src/textual/css/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
"underline",
"uu",
}
VALID_PSEUDO_CLASSES: Final = {
"blur",
"disabled",
"enabled",
"focus-within",
"focus",
"hover",
}


NULL_SPACING: Final = Spacing.all(0)
27 changes: 26 additions & 1 deletion src/textual/css/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from rich.syntax import Syntax
from rich.text import Text

from ..suggestions import get_suggestion
from ._error_tools import friendly_list
from .constants import VALID_PSEUDO_CLASSES


class TokenError(Exception):
Expand Down Expand Up @@ -56,7 +58,7 @@ def _get_snippet(self) -> Panel:
line_numbers=True,
indent_guides=True,
line_range=(max(0, line_no - 2), line_no + 2),
highlight_lines={line_no},
highlight_lines={line_no + 1},
)
syntax.stylize_range("reverse bold", self.start, self.end)
return Panel(syntax, border_style="red")
Expand Down Expand Up @@ -227,6 +229,29 @@ def get_token(self, expect: Expect) -> Token:
(line_no, col_no),
referenced_by=None,
)

if (
token.name == "pseudo_class"
and token.value.strip(":") not in VALID_PSEUDO_CLASSES
):
pseudo_class = token.value.strip(":")
suggestion = get_suggestion(pseudo_class, list(VALID_PSEUDO_CLASSES))
all_valid = f"must be one of {friendly_list(VALID_PSEUDO_CLASSES)}"
if suggestion:
raise TokenError(
self.path,
self.code,
(line_no, col_no),
f"unknown pseudo-class {pseudo_class!r}; did you mean {suggestion!r}?; {all_valid}",
)
else:
raise TokenError(
self.path,
self.code,
(line_no, col_no),
f"unknown pseudo-class {pseudo_class!r}; {all_valid}",
)

col_no += len(value)
if col_no >= len(line):
line_no += 1
Expand Down
60 changes: 30 additions & 30 deletions tests/css/test_mega_stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -140,52 +140,52 @@ A1

/**********************************************************************/

A:foo {}
A:foo:bar {}
A:focus {}
A:focus:hover {}
A
:foo {}
:focus {}
A
:foo:bar {}
:focus:hover {}
A
:foo
:bar {}
A:foo-bar {}
:focus
:hover {}
A:enabled {}
A
:foo-bar {}
:enabled {}

A :foo {}
A :foo :bar {}
A :foo-bar {}
A :focus {}
A :focus :hover {}
A :enabled {}

.A:foo {}
.A:foo:bar {}
.A:focus {}
.A:focus:hover {}
.A
:foo {}
:focus {}
.A
:foo:bar {}
:focus:hover {}
.A
:foo
:bar {}
.A:foo-bar {}
:focus
:hover {}
.A:enabled {}
.A
:foo-bar {}
:enabled {}

#A:foo {}
#A:foo:bar {}
#A:focus {}
#A:focus:hover {}
#A
:foo {}
:focus {}
#A
:foo:bar {}
:focus:hover {}
#A
:foo
:bar {}
#A:foo-bar {}
:focus
:hover {}
#A:enabled {}
#A
:foo-bar {}
:enabled {}

A1.A1.A1:foo {}
A1.A1#A1:foo {}
A1:foo.A1:foo#A1:foo {}
A1.A1.A1:focus {}
A1.A1#A1:focus {}
A1:focus.A1:focus#A1:focus {}

/**********************************************************************/

Expand Down
18 changes: 18 additions & 0 deletions tests/css/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,21 @@ def test_combined_type_starts_with_number(self):
stylesheet.add_source(f"StartType {separator} 1TestType {{}}")
with pytest.raises(TokenError):
stylesheet.parse()


def test_parse_bad_psuedo_selector():
"""Check unknown selector raises a token error."""

bad_selector = """\
Widget:foo{
border: red;
}
"""

stylesheet = Stylesheet()
stylesheet.add_source(bad_selector, "foo")

with pytest.raises(TokenError) as error:
stylesheet.parse()

assert error.value.start == (0, 6)