| 1 | // Package tokencheck is the shared half of every component's |
| 2 | // "...UseDefinedTokens" test: each var(--rst-*) a component stylesheet |
| 3 | // reads must be defined by the pinned rastrillo's tokens.css plus |
| 4 | // estilo's theme. AGENTS.md's two-pin case, for components. |
| 5 | package tokencheck |
| 6 | |
| 7 | import ( |
| 8 | "regexp" |
| 9 | |
| 10 | "amadan.net/oficina/estilo.git/theme" |
| 11 | "github.com/carlosframework/rastrillo/ui" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | definition = regexp.MustCompile(`(--rst-[a-z0-9-]+)\s*:`) |
| 16 | reference = regexp.MustCompile(`var\(\s*(--rst-[a-z0-9-]+)`) |
| 17 | ) |
| 18 | |
| 19 | // Undefined returns the rastrillo tokens css reads that neither |
| 20 | // ui.TokensCSS() nor theme.CSS() defines, and how many references it |
| 21 | // read in total, so a caller can refuse a stylesheet that reads none |
| 22 | // (a check over nothing passes vacuously). |
| 23 | func Undefined(css []byte) (missing []string, references int) { |
| 24 | defined := map[string]bool{} |
| 25 | for _, m := range definition.FindAllSubmatch(append(append([]byte{}, ui.TokensCSS()...), theme.CSS()...), -1) { |
| 26 | defined[string(m[1])] = true |
| 27 | } |
| 28 | for _, m := range reference.FindAllSubmatch(css, -1) { |
| 29 | references++ |
| 30 | if !defined[string(m[1])] { |
| 31 | missing = append(missing, string(m[1])) |
| 32 | } |
| 33 | } |
| 34 | return missing, references |
| 35 | } |
| 36 | |