diff --git a/sandbox/uber.py b/sandbox/uber.py index ae44da30d9..91835343b5 100644 --- a/sandbox/uber.py +++ b/sandbox/uber.py @@ -28,7 +28,7 @@ async def on_mount(self): Widget(id="uber2-child1"), Widget(id="uber2-child2"), ) - first_child = Placeholder(id="child1", classes={"list-item"}) + first_child = Placeholder(id="child1", classes="list-item") uber1 = Widget( Placeholder(id="child1", classes="list-item"), Placeholder(id="child2", classes="list-item"), diff --git a/src/textual/app.py b/src/textual/app.py index 319415142f..d54cf41818 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -504,6 +504,7 @@ async def process_messages(self) -> None: try: if self.css_file is not None: self.stylesheet.read(self.css_file) + self.stylesheet.parse() if self.css is not None: self.stylesheet.add_source( self.css, path=f"<{self.__class__.__name__}>" diff --git a/src/textual/css/_help_text.py b/src/textual/css/_help_text.py index 241c6e39f4..6dac2bee2a 100644 --- a/src/textual/css/_help_text.py +++ b/src/textual/css/_help_text.py @@ -2,6 +2,7 @@ import sys from dataclasses import dataclass +from typing import Iterable from textual.css._help_renderables import Example, Bullet, HelpText from textual.css.constants import ( @@ -229,7 +230,7 @@ def scalar_help_text( def string_enum_help_text( property_name: str, - valid_values: list[str], + valid_values: Iterable[str], context: StylingContext, ) -> HelpText: """Help text to show when the user supplies an invalid value for a string diff --git a/src/textual/css/_styles_builder.py b/src/textual/css/_styles_builder.py index 774927a5ac..54b1a2d46d 100644 --- a/src/textual/css/_styles_builder.py +++ b/src/textual/css/_styles_builder.py @@ -683,7 +683,7 @@ def align_error(name, token): if token_vertical.name != "token": align_error(name, token_vertical) - elif token_horizontal.value not in VALID_ALIGN_VERTICAL: + elif token_vertical.value not in VALID_ALIGN_VERTICAL: align_error(name, token_horizontal) name = name.replace("-", "_") @@ -693,24 +693,26 @@ def align_error(name, token): def process_align_horizontal(self, name: str, tokens: list[Token]) -> None: try: value = self._process_enum(name, tokens, VALID_ALIGN_HORIZONTAL) - self.styles._rules[name.replace("-", "_")] = value except StyleValueError: self.error( name, tokens[0], string_enum_help_text(name, VALID_ALIGN_HORIZONTAL, context="css"), ) + else: + self.styles._rules[name.replace("-", "_")] = value def process_align_vertical(self, name: str, tokens: list[Token]) -> None: try: value = self._process_enum(name, tokens, VALID_ALIGN_VERTICAL) - self.styles._rules[name.replace("-", "_")] = value except StyleValueError: self.error( name, tokens[0], string_enum_help_text(name, VALID_ALIGN_VERTICAL, context="css"), ) + else: + self.styles._rules[name.replace("-", "_")] = value process_content_align = process_align process_content_align_horizontal = process_align_horizontal diff --git a/src/textual/css/stylesheet.py b/src/textual/css/stylesheet.py index 03a6bc3bd4..fc98fb9018 100644 --- a/src/textual/css/stylesheet.py +++ b/src/textual/css/stylesheet.py @@ -17,7 +17,8 @@ from rich.syntax import Syntax from rich.text import Text -from textual._loop import loop_last +from .._loop import loop_last +from .. import log from .errors import StylesheetError from .match import _check_selectors from .model import RuleSet @@ -39,9 +40,9 @@ def __rich__(self) -> RenderableType: class StylesheetErrors: def __init__( - self, stylesheet: "Stylesheet", variables: dict[str, str] | None = None + self, rules: list[RuleSet], variables: dict[str, str] | None = None ) -> None: - self.stylesheet = stylesheet + self.rules = rules self.variables: dict[str, str] = {} self._css_variables: dict[str, list[Token]] = {} if variables: @@ -69,7 +70,7 @@ def __rich_console__( self, console: Console, options: ConsoleOptions ) -> RenderableType: error_count = 0 - for rule in self.stylesheet.rules: + for rule in self.rules: for is_last, (token, message) in loop_last(rule.errors): error_count += 1 @@ -140,10 +141,6 @@ def rules(self) -> list[RuleSet]: def css(self) -> str: return "\n\n".join(rule_set.css for rule_set in self.rules) - @property - def error_renderable(self) -> StylesheetErrors: - return StylesheetErrors(self) - def set_variables(self, variables: dict[str, str]) -> None: """Set CSS variables. @@ -226,7 +223,8 @@ def parse(self) -> None: for path, css in self.source.items(): css_rules = self._parse_rules(css, path) if any(rule.errors for rule in css_rules): - raise StylesheetParseError(self.error_renderable) + error_renderable = StylesheetErrors(css_rules) + raise StylesheetParseError(error_renderable) add_rules(css_rules) self._rules = rules self._require_parse = False @@ -244,7 +242,7 @@ def reparse(self) -> None: for path, css in self.source.items(): stylesheet.add_source(css, path) stylesheet.parse() - self.rules = stylesheet.rules + self._rules = stylesheet.rules self.source = stylesheet.source @classmethod diff --git a/src/textual/scrollbar.py b/src/textual/scrollbar.py index 62f05185c9..5633f0d0ed 100644 --- a/src/textual/scrollbar.py +++ b/src/textual/scrollbar.py @@ -69,7 +69,7 @@ def __init__( position: float = 0, thickness: int = 1, vertical: bool = True, - style: StyleType = "ansi_bright_magenta on #555555", + style: StyleType = "bright_magenta on #555555", ) -> None: self.virtual_size = virtual_size self.window_size = window_size @@ -89,7 +89,7 @@ def render_bar( thickness: int = 1, vertical: bool = True, back_color: Color = Color.parse("#555555"), - bar_color: Color = Color.parse("ansi_bright_magenta"), + bar_color: Color = Color.parse("bright_magenta"), ) -> Segments: if vertical: @@ -181,7 +181,7 @@ def __rich_console__( vertical=self.vertical, thickness=thickness, back_color=_style.bgcolor or Color.parse("#555555"), - bar_color=_style.color or Color.parse("ansi_bright_magenta"), + bar_color=_style.color or Color.parse("bright_magenta"), ) yield bar