diff --git a/tests/test_admin_design_elements.py b/tests/test_admin_design_elements.py index a8692b9..735a475 100644 --- a/tests/test_admin_design_elements.py +++ b/tests/test_admin_design_elements.py @@ -52,6 +52,22 @@ def _blocks(style: str) -> list: return [(m.group(1).strip(), m.group(2)) for m in re.finditer(r"([^{}]+)\{([^{}]*)\}", clean)] +# Имена цветов, которые пишут по привычке. «transparent» и «currentColor» цветом набора +# не управляют — их держим разрешёнными. +COLOR_WORDS = ("white", "black", "red", "green", "blue", "yellow", "orange", "purple", + "pink", "gray", "grey", "silver", "navy", "teal", "brown", "gold") + + +def _hardcoded_color(text: str) -> bool: + """Цвет, записанный значением, а не переменной: число, имя или hsl().""" + if re.search(r"#[0-9a-fA-F]{3,8}\b", text) or re.search(r"\b(?:rgba?|hsla?)\(", text): + return True + for word in COLOR_WORDS: + if re.search(r"(?::|\s)" + word + r"(?![-\w])", text, re.I): + return True + return False + + def _tokens(body: str) -> dict: return dict(re.findall(r"(--[a-z0-9-]+)\s*:\s*([^;]+)", body)) @@ -66,8 +82,7 @@ def test_token_blocks_are_only_root_selectors(style): for head, body in _blocks(style): if TOKEN_HEAD.match(head.strip()): continue - assert not re.search(r"#[0-9a-fA-F]{3,8}\b", body), head - assert not re.search(r"\brgba?\(", body), head + assert not _hardcoded_color(body), (head, body.strip()[:80]) def test_inline_styles_carry_no_colors_or_shapes(outside): @@ -139,7 +154,7 @@ def test_danger_is_its_own_token(style, outside): not_red.append((name, values["--danger"])) assert not not_red, not_red # В разметке и скриптах беду красят именно бедой. - assert "var(--accent)" not in "".join(re.findall(r'style="([^"]*color[^"]*)"', outside)) + assert "var(--accent)" not in "".join(re.findall(r'style="([^"]*)"', outside)) def test_every_design_defines_the_full_color_set(style): @@ -260,7 +275,9 @@ def test_numbers_are_right_aligned(style, outside): # Мало объявить правило — числовые колонки должны его получать: и в шапке, и в # ячейках, которые скрипт рисует на ходу. assert 'th class="num"' in outside - assert "fillCells(" in outside + body = outside[outside.index("function fillCells"):] + body = body[:body.index("\n}")] + assert "'num'" in body, "ячейки не получают класс выравнивания" assert outside.count("fillCells(") >= 6 @@ -279,9 +296,13 @@ def test_narrow_screen_rules_exist(style): """На узком экране полоса разделов в 220 точек съедает половину, а таблица на одиннадцать колонок выталкивает страницу за край. Порча: убрать правила узкого экрана → упадёт.""" - narrow = re.search(r"@media\s*\([^)]*max-width[^)]*\)\s*\{(.*)", style, re.S) - assert narrow, "правил для узкого экрана нет" - body = narrow.group(1) + at = re.search(r"@media\s*\([^)]*max-width[^)]*\)\s*\{", style) + assert at, "правил для узкого экрана нет" + depth, i = 1, at.end() + while depth and i < len(style): + depth += {"{": 1, "}": -1}.get(style[i], 0) + i += 1 + body = style[at.end():i] assert ".sidebar" in body assert "table" in body