textual icon indicating copy to clipboard operation
textual copied to clipboard

Nested TCSS interacts poorly with pseudo-classes in selectors.

Open rodrigogiraoserrao opened this issue 7 months ago • 1 comments

Working on #3969 and #3999 we realised that pseudo-classes in nested selectors aren't correctly handled.

(In short, to the tokenizer, a selector + pseudo-class looks the same as a rule + value in a nested context.)

Examples of CSS that breaks the tokenizer:

from textual.app import App, ComposeResult
from textual.widgets import Label


class NestedCSSTokenErrorApp(App[None]):
    CSS = """
    Screen {
        Label:hover {
                border: solid red;
        }
    }
    """

    def compose(self) -> ComposeResult:
        yield Label("This is class foo", classes="foo")
        yield Label("This is class bar", classes="bar")


if __name__ == "__main__":
    NestedCSSTokenErrorApp().run()
from textual.app import App, ComposeResult
from textual.widgets import Label


class NestedCSSTokenErrorApp(App[None]):
    CSS = """
    Screen {
        Label {
            &.foo, &.bar:hover {
                border: solid red;
            }
        }

        border: solid green;
    }
    """

    def compose(self) -> ComposeResult:
        yield Label("This is class foo", classes="foo")
        yield Label("This is class bar", classes="bar")


if __name__ == "__main__":
    NestedCSSTokenErrorApp().run()

rodrigogiraoserrao avatar Jan 17 '24 17:01 rodrigogiraoserrao

See Will's comment showing how the use of & “makes” some cases work.

rodrigogiraoserrao avatar Jan 17 '24 17:01 rodrigogiraoserrao

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

github-actions[bot] avatar Mar 05 '24 11:03 github-actions[bot]