"""Rendering a design system as its master CSS sheet. The generator is pure, so a whole sheet is one literal of tokens in and a string out. Two things here are load-bearing rather than cosmetic: what the sheet deliberately does NOT contain, and the fact that a value cannot escape its declaration. """ from types import SimpleNamespace from scribe.services.design_stylesheet import ( check_code_against_tokens, duplicate_values, is_valid_token_name, render_stylesheet, safe_comment, safe_value, selector_for_mode, ) def _token(name, value_by_mode, group_name=None, purpose=None): return SimpleNamespace( name=name, value_by_mode=value_by_mode, group_name=group_name, purpose=purpose, ) # --- safety ----------------------------------------------------------------- # # Design systems are shareable records. A value that can close its declaration # can inject arbitrary CSS into the page of anyone the system was shared with, # which makes this a real boundary rather than tidiness. def test_a_value_that_would_escape_its_declaration_is_refused(): """`red; } body { display: none` is the whole attack: end the declaration, close the block, open your own.""" assert safe_value("red; } body { display: none") is None assert safe_value("#fff}") is None assert safe_value("#fff;") is None def test_at_rules_and_tags_are_refused(): assert safe_value("@import url(evil.css)") is None assert safe_value("