[css][bugfix] CSS tokens can now have digits in their names

...excepted for their first char of course
This commit is contained in:
Olivier Philippon
2022-04-28 12:18:23 +01:00
parent d0b8cacc35
commit 3ed17cb406
2 changed files with 3 additions and 18 deletions

View File

@@ -461,25 +461,10 @@ class StylesBuilder:
def process_color(self, name: str, tokens: list[Token]) -> None:
"""Processes a simple color declaration."""
name = name.replace("-", "_")
token_indexes_to_skip = []
for i, token in enumerate(tokens):
if i in token_indexes_to_skip:
continue
for token in tokens:
if token.name in ("color", "token"):
value = token.value
# Color names can include digits (e.g. "turquoise4"): if the next token is a number
# we should consider it part of the color name:
next_token = tokens[i + 1] if len(tokens) > (i + 1) else None
if (
next_token
and next_token.name == "number"
and next_token.location[1] == token.location[1] + len(value)
):
value = value + next_token.value
# skip next token, as we included it in this one's value:
token_indexes_to_skip.append(i + 1)
try:
self.styles._rules[name] = Color.parse(value)
self.styles._rules[name] = Color.parse(token.value)
except Exception as error:
self.error(
name, token, f"failed to parse color {token.value!r}; {error}"

View File

@@ -11,7 +11,7 @@ DURATION = r"\d+\.?\d*(?:ms|s)"
NUMBER = r"\-?\d+\.?\d*"
COLOR = r"\#[0-9a-fA-F]{8}|\#[0-9a-fA-F]{6}|rgb\(\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*\)|rgba\(\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*\)"
KEY_VALUE = r"[a-zA-Z_-][a-zA-Z0-9_-]*=[0-9a-zA-Z_\-\/]+"
TOKEN = "[a-zA-Z_-]+"
TOKEN = "[a-zA-Z][a-zA-Z0-9_-]*"
STRING = r"\".*?\""
VARIABLE_REF = r"\$[a-zA-Z0-9_\-]+"