| 1 | package check |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "regexp" |
| 7 | "slices" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/rastrillo/fiesta" |
| 13 | ) |
| 14 | |
| 15 | // The gates that read tokens.css, shell.css and the themes as text, |
| 16 | // ported from rastrillo's ui/ui_test.go at the seed commit: what the |
| 17 | // stylesheets embed, the one-block theme structure, the motion and |
| 18 | // scheme gates, the selectors the components rely on, and the markup |
| 19 | // contracts the examples hold (the dropdown's native exclusivity, |
| 20 | // the modal as a rendered-open dialog). They read the stylesheets |
| 21 | // with the readers the gates share: cssSpan and the bracket reader in |
| 22 | // markuphelp_test.go, declarationsOf in baseline_test.go, leafRules |
| 23 | // here, cssRules and splitSelectorList in spelling_test.go, and |
| 24 | // themeTokens in contrast_test.go. None searches a stylesheet's text |
| 25 | // for what it must hold. The tests over markup read the example state |
| 26 | // the manifest's idiom table names. |
| 27 | |
| 28 | func TestTokensCSSIsEmbedded(t *testing.T) { |
| 29 | css := fiesta.TokensCSS() |
| 30 | if len(css) == 0 { |
| 31 | t.Fatal("fiesta.TokensCSS() is empty") |
| 32 | } |
| 33 | // Each is read where a browser reads it, so one only a comment |
| 34 | // mentions is missing: a declared token, a var() a value reads, |
| 35 | // a selector. |
| 36 | decls, _ := parseDeclarations(t, "tokens.css", string(css)) |
| 37 | declared, reads := map[string]bool{}, map[string]bool{} |
| 38 | for _, d := range decls { |
| 39 | declared[d.Property] = true |
| 40 | for _, ref := range varRefs(d.Value) { |
| 41 | reads["var("+ref.name+")"] = true |
| 42 | } |
| 43 | } |
| 44 | attributes, classes := styledAttributes(t, string(css)), styledClasses(t, string(css)) |
| 45 | for _, want := range []string{"--rst-sp-4", "--rst-fs-base"} { |
| 46 | if !declared[want] { |
| 47 | t.Errorf("tokens.css does not declare %s", want) |
| 48 | } |
| 49 | } |
| 50 | for _, want := range []string{"var(--rst-bg)", "var(--rst-text)", "var(--rst-accent)", "var(--rst-font)", "var(--rst-radius)", "var(--rst-shadow-pop)"} { |
| 51 | if !reads[want] { |
| 52 | t.Errorf("tokens.css never reads %s", want) |
| 53 | } |
| 54 | } |
| 55 | if !attributes["[rst-status]"] || !classes["rst-sr-only"] { |
| 56 | t.Errorf("tokens.css has no [rst-status] or .rst-sr-only selector") |
| 57 | } |
| 58 | // The colour blocks are the theme's now. tokens.css naming one again |
| 59 | // means a palette leaked back into the structural file, where a |
| 60 | // theme swap could no longer reach it. The shape tokens joined them |
| 61 | // in v2: a "--rst-radius:" or "--rst-shadow-pop:" *declaration* here |
| 62 | // would silently outrank nothing and confuse everything, since every |
| 63 | // theme declares its own. |
| 64 | leaks, err := shapeLeaks(string(css)) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | for _, gone := range leaks { |
| 69 | t.Errorf("tokens.css declares %q; colour and shape belong in themes/", gone) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // shapeLeaks returns every colour-block or shape-token declaration |
| 74 | // tokens.css must not carry. The one allowance is print's flatten of |
| 75 | // the knob's two shadow tokens (their hover lift outranks the knob's |
| 76 | // print reset), at html:root inside @media print and nowhere else; an |
| 77 | // error when that flatten is missing, doubled or elsewhere. Everything |
| 78 | // else is searched everywhere, the print and forced-colours blocks |
| 79 | // included: a radius there would override a theme's geometry. |
| 80 | // |
| 81 | // It reads rules with the declaration reader. Read as text, the |
| 82 | // flatten counted where it sat in a string at html:root, with the real |
| 83 | // one gone, and the print block it had to sit in was cut by counting |
| 84 | // braces. |
| 85 | func shapeLeaks(css string) ([]string, error) { |
| 86 | rules, err := readLeafRules(css) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | const none = "0 0 0 transparent" |
| 91 | flatten := func(r leafRule) bool { |
| 92 | return r.sets("--rst-shadow-knob", none) && r.sets("--rst-shadow-knob-hover", none) |
| 93 | } |
| 94 | var allowed *leafRule |
| 95 | flattens := 0 |
| 96 | for i, r := range rules { |
| 97 | if flatten(r) { |
| 98 | flattens++ |
| 99 | if r.selector == "html:root" && slices.Contains(strings.Split(r.context, " > "), "@media print") { |
| 100 | allowed = &rules[i] |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if flattens != 1 { |
| 105 | return nil, fmt.Errorf("tokens.css carries print's knob-shadow flatten %d times, want once, at html:root inside @media print", flattens) |
| 106 | } |
| 107 | if allowed == nil { |
| 108 | return nil, fmt.Errorf("print's knob-shadow flatten is not at html:root inside @media print, the one place it is allowed") |
| 109 | } |
| 110 | shape := []string{ |
| 111 | "--rst-radius", "--rst-radius-sm", "--rst-radius-pill", |
| 112 | "--rst-shadow-pop", "--rst-shadow-knob", "--rst-shadow-lift", "--rst-overlay", |
| 113 | } |
| 114 | leaked := map[string]bool{} |
| 115 | for i, r := range rules { |
| 116 | scheme := strings.ReplaceAll(r.context+" "+r.selector, " ", "") |
| 117 | for _, gone := range []string{"prefers-color-scheme:dark", `:root[data-theme="dark"]`, `:root[data-theme="light"]`} { |
| 118 | if strings.Contains(scheme, gone) { |
| 119 | leaked[gone] = true |
| 120 | } |
| 121 | } |
| 122 | for _, d := range r.decls { |
| 123 | if &rules[i] == allowed && strings.HasPrefix(d[0], "--rst-shadow-knob") { |
| 124 | continue |
| 125 | } |
| 126 | leaked[d[0]+":"] = true |
| 127 | } |
| 128 | } |
| 129 | var out []string |
| 130 | for _, gone := range []string{"prefers-color-scheme:dark", `:root[data-theme="dark"]`, `:root[data-theme="light"]`} { |
| 131 | if leaked[gone] { |
| 132 | out = append(out, gone) |
| 133 | } |
| 134 | } |
| 135 | for _, p := range shape { |
| 136 | if leaked[p+":"] { |
| 137 | out = append(out, p+":") |
| 138 | } |
| 139 | } |
| 140 | return out, nil |
| 141 | } |
| 142 | |
| 143 | // TestTheShapeGateSeesInsideThePrintAndForcedBlocks (switch review 6): |
| 144 | // the allowance is the knob flatten at its place, not the blocks. Radii |
| 145 | // at html:root inside print and inside forced colours are leaks like |
| 146 | // any other, and the flatten under another selector in print is an |
| 147 | // error. The shipped tokens.css is the control. |
| 148 | func TestTheShapeGateSeesInsideThePrintAndForcedBlocks(t *testing.T) { |
| 149 | css := string(fiesta.TokensCSS()) |
| 150 | if leaks, err := shapeLeaks(css); err != nil || len(leaks) != 0 { |
| 151 | t.Fatalf("the shipped tokens.css: leaks %v, err %v; want none", leaks, err) |
| 152 | } |
| 153 | const radii = "\n html:root { --rst-radius: 0; --rst-radius-pill: 0; }" |
| 154 | for _, m := range []struct{ name, header string }{{"print", "@media print {"}, {"forced colours", "@media (forced-colors: active) {"}} { |
| 155 | mutant := strings.Replace(css, m.header, m.header+radii, 1) |
| 156 | if mutant == css { |
| 157 | t.Fatalf("%s: the block is not where this test expects it", m.name) |
| 158 | } |
| 159 | leaks, err := shapeLeaks(mutant) |
| 160 | if err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if strings.Join(leaks, " ") != "--rst-radius: --rst-radius-pill:" { |
| 164 | t.Errorf("%s block with radii at html:root: the gate reports %v; want --rst-radius: and --rst-radius-pill:", m.name, leaks) |
| 165 | } |
| 166 | } |
| 167 | const flatten = "--rst-shadow-knob: 0 0 0 transparent; --rst-shadow-knob-hover: 0 0 0 transparent;" |
| 168 | moved := strings.Replace(css, flatten, "", 1) |
| 169 | moved = strings.Replace(moved, "@media print {", "@media print {\n .rst-switch__track::after { "+flatten+" }", 1) |
| 170 | if moved == css || !strings.Contains(moved, flatten) { |
| 171 | t.Fatal("the knob flatten is not where this test expects it") |
| 172 | } |
| 173 | if _, err := shapeLeaks(moved); err == nil { |
| 174 | t.Error("the knob flatten moved off html:root in print passed the gate") |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Every shipped theme is a whole theme: the palette, the type family, |
| 179 | // the shape tokens, and the one-block structure the contrast gate parses. |
| 180 | func TestThemeCSSIsEmbedded(t *testing.T) { |
| 181 | for _, name := range fiesta.ThemeNames() { |
| 182 | css, ok := fiesta.ThemeCSS(name) |
| 183 | if !ok { |
| 184 | t.Fatalf("fiesta.ThemeCSS(%q) reports missing, but it is in fiesta.ThemeNames()", name) |
| 185 | } |
| 186 | // Read as declarations, so a token or a toggle that is only in |
| 187 | // a comment or a string is missing. |
| 188 | where := "themes/" + name + ".css" |
| 189 | decls := themeDeclarations(t, where, string(css)) |
| 190 | root := declaredAt(decls, "", ":root") |
| 191 | for _, want := range []string{ |
| 192 | "--rst-bg", "--rst-surface", "--rst-text", "--rst-accent", |
| 193 | "--rst-tone-positive-fg", "--rst-font", "--rst-radius", "--rst-shadow-pop", |
| 194 | } { |
| 195 | if _, ok := root[want]; !ok { |
| 196 | t.Errorf("%s declares no %s at :root", where, want) |
| 197 | } |
| 198 | } |
| 199 | if root["color-scheme"] != "light dark" { |
| 200 | t.Errorf("%s sets color-scheme %q at :root, want light dark", where, root["color-scheme"]) |
| 201 | } |
| 202 | pairs := 0 |
| 203 | for _, v := range root { |
| 204 | if _, _, ok := splitLightDark(v); ok { |
| 205 | pairs++ |
| 206 | } |
| 207 | } |
| 208 | if pairs == 0 { |
| 209 | t.Errorf("%s declares no light-dark() pair at :root", where) |
| 210 | } |
| 211 | // A theme restates nothing per scheme but numbers: |
| 212 | // light-dark() cannot wrap a number, so the press strengths are |
| 213 | // written once in :root for light and again in two blocks for |
| 214 | // dark, and those blocks hold --rst-* numbers, the same numbers |
| 215 | // in both, each also declared in :root. Anything else in the |
| 216 | // file beyond :root and the two toggle rules is the second |
| 217 | // palette block light-dark() was adopted to make impossible. |
| 218 | // The toggles are read there too. |
| 219 | schemeNumberBlocks(t, name, string(css)) |
| 220 | // day's relative-colour block (derived_test.go) nests a :root of |
| 221 | // its own; outside it the palette is one block. |
| 222 | bare := 0 |
| 223 | for _, r := range leafRules(t, string(css)) { |
| 224 | if r.context == "" && r.selector == ":root" { |
| 225 | bare++ |
| 226 | } |
| 227 | } |
| 228 | if bare != 1 { |
| 229 | t.Errorf("%s opens %d bare :root blocks, want exactly 1", where, bare) |
| 230 | } |
| 231 | for _, prop := range []string{"--rst-bg", "--rst-text", "--rst-accent", "--rst-radius"} { |
| 232 | got := 0 |
| 233 | for _, d := range decls { |
| 234 | if d.Property == prop { |
| 235 | got++ |
| 236 | } |
| 237 | } |
| 238 | if got != 1 { |
| 239 | t.Errorf("%s declares %s: %d times, want exactly 1 (one block, both schemes)", where, prop, got) |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | for _, bad := range []string{"nope", "", "day.css", "../tokens"} { |
| 244 | if _, ok := fiesta.ThemeCSS(bad); ok { |
| 245 | t.Errorf("fiesta.ThemeCSS(%q) reports a theme that is not shipped", bad) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Both schemes are authored, never inverted. Before v2 that meant |
| 251 | // counting three copies of every colour block; now it means every themed |
| 252 | // colour is declared exactly once, as a light-dark() call with two |
| 253 | // distinct-or-deliberate halves. A theme that wrote a bare hex where a |
| 254 | // light-dark() belongs has a dark scheme that is just the light one |
| 255 | // showing through, which is the modern shape of the same rot. |
| 256 | // |
| 257 | // The shadows and the overlay are exempt from the light-dark() |
| 258 | // requirement but not from being declared: a shadow whose two schemes |
| 259 | // are genuinely identical (day's knob and lift) is allowed to say so |
| 260 | // once, and light-dark() may only wrap the colour inside a shadow value |
| 261 | // anyway, never the whole thing. |
| 262 | func TestBothSchemesAreAuthoredInEveryTheme(t *testing.T) { |
| 263 | needsLightDark := []string{ |
| 264 | "--rst-bg", "--rst-surface", "--rst-surface-2", "--rst-line", "--rst-line-strong", |
| 265 | "--rst-text", "--rst-text-muted", "--rst-text-faint", |
| 266 | "--rst-accent", "--rst-accent-strong", "--rst-accent-soft", "--rst-on-accent", |
| 267 | "--rst-tone-neutral-fg", "--rst-tone-neutral-bg", |
| 268 | "--rst-tone-positive-fg", "--rst-tone-positive-bg", |
| 269 | "--rst-tone-warning-fg", "--rst-tone-warning-bg", |
| 270 | "--rst-tone-negative-fg", "--rst-tone-negative-bg", |
| 271 | "--rst-overlay", |
| 272 | "--rst-accent-fill", "--rst-accent-fill-hover", "--rst-accent-fill-edge", "--rst-accent-fill-edge-hover", |
| 273 | "--rst-danger-fill", "--rst-danger-fill-hover", "--rst-danger-fill-edge", "--rst-danger-fill-edge-hover", |
| 274 | "--rst-control-face", "--rst-control-face-hover", "--rst-control-border", "--rst-control-border-hover", "--rst-control-text-hover", |
| 275 | "--rst-knob-shade", "--rst-track-off", "--rst-tint-hover", "--rst-tint-press", "--rst-row-tint-hover", "--rst-row-tint-press", |
| 276 | "--rst-nav-current-bg", "--rst-nav-current-fg", |
| 277 | } |
| 278 | // --rst-header-rule is declared once but is NOT in needsLightDark: |
| 279 | // it is derived from tokens the two schemes already split, so one |
| 280 | // color-mix() (or, in plain, one var()) covers both. Wrapping a |
| 281 | // derived value in light-dark() would reintroduce exactly the second |
| 282 | // hand-authored colour per scheme the derivation exists to avoid. |
| 283 | // |
| 284 | // The depth axis's one-value tokens are here too: the numbers, the |
| 285 | // transparent faces and edges (--rst-control-face-press, the ghost |
| 286 | // and summary faces are transparent in two themes, so a light-dark() |
| 287 | // requirement would force a colour where none is painted), and the |
| 288 | // type choices. The press strengths are in neither list: they are |
| 289 | // declared in :root and restated in the two dark-number blocks, and |
| 290 | // schemeNumberBlocks holds those. |
| 291 | declaredOnce := append([]string{ |
| 292 | "--rst-font", "--rst-radius", "--rst-radius-sm", "--rst-radius-pill", |
| 293 | "--rst-shadow-pop", "--rst-shadow-knob", "--rst-shadow-knob-hover", "--rst-shadow-lift", |
| 294 | "--rst-header-rule", |
| 295 | "--rst-on-accent-fill", "--rst-on-danger-fill", "--rst-on-danger-fill-hover", "--rst-danger-hover-filter", "--rst-knob", "--rst-control-face-press", |
| 296 | "--rst-ghost-edge", "--rst-ghost-face-hover", "--rst-ghost-edge-hover", "--rst-summary-face", "--rst-summary-face-hover", "--rst-summary-edge", "--rst-summary-edge-hover", "--rst-summary-radius", "--rst-tint-tap", "--rst-tint-card-hover", |
| 297 | // The tile and group tints are the surface or transparent in |
| 298 | // the flat themes (a tile had no hover, so its hover is its |
| 299 | // surface), so they are declared once, not per scheme. |
| 300 | "--rst-tint-tile-hover", "--rst-tint-tile-press", "--rst-tint-group-hover", |
| 301 | "--rst-depth-hl", "--rst-depth-contact", "--rst-depth-ambient", "--rst-depth-face", "--rst-press-drop", |
| 302 | "--rst-focus-edge", "--rst-focus-halo", |
| 303 | "--rst-sheen-fill", "--rst-sheen-plain", "--rst-light-rim", "--rst-light-wash", |
| 304 | "--rst-title-size", "--rst-title-weight", "--rst-title-track", "--rst-heading-size", |
| 305 | "--rst-label-case", "--rst-label-track", "--rst-rail-bg", |
| 306 | // The switch's two size sets: lengths, the same in both schemes. |
| 307 | "--rst-switch-w", "--rst-switch-h", "--rst-switch-knob", "--rst-switch-touch-w", "--rst-switch-touch-h", "--rst-switch-touch-knob", |
| 308 | }, needsLightDark...) |
| 309 | |
| 310 | for _, name := range fiesta.ThemeNames() { |
| 311 | raw, ok := fiesta.ThemeCSS(name) |
| 312 | if !ok { |
| 313 | t.Fatalf("fiesta.ThemeCSS(%q) missing", name) |
| 314 | } |
| 315 | // The relative-colour block redeclares a handful of day's tokens |
| 316 | // (derived_test.go); the once-only count reads the file without |
| 317 | // it, and the block may restate a name at most once. Read as |
| 318 | // declarations, so one in a comment or a string is not counted. |
| 319 | values := map[string]string{} |
| 320 | count, supports := map[string]int{}, map[string]int{} |
| 321 | for _, d := range themeDeclarations(t, "themes/"+name+".css", string(raw)) { |
| 322 | if d.Context == accentSupports { |
| 323 | supports[d.Property]++ |
| 324 | continue |
| 325 | } |
| 326 | count[d.Property]++ |
| 327 | values[d.Property] = fullValue(d) |
| 328 | } |
| 329 | for _, prop := range declaredOnce { |
| 330 | if got := count[prop]; got != 1 { |
| 331 | t.Errorf("themes/%s.css declares %s %d times, want exactly 1", name, prop, got) |
| 332 | } |
| 333 | if got := supports[prop]; got > 1 { |
| 334 | t.Errorf("themes/%s.css redeclares %s %d times in its relative-colour block, want at most 1", name, prop, got) |
| 335 | } |
| 336 | } |
| 337 | for _, prop := range needsLightDark { |
| 338 | // A derived value is exempt the way --rst-header-rule is: a |
| 339 | // bare var() of a token declared in this theme, or a |
| 340 | // color-mix(), reads tokens the schemes already split, and |
| 341 | // wrapping it in light-dark() would be the second |
| 342 | // hand-authored colour per scheme the derivation exists to |
| 343 | // avoid. What passes here: day's tint aliases, focus edge and |
| 344 | // halo, and plain's and signal's fills, which read their |
| 345 | // accent. day's fills and soft ground are light-dark() pairs |
| 346 | // (of relative colours, in the @supports block) and are |
| 347 | // split like any other. |
| 348 | if v := values[prop]; strings.HasPrefix(v, "color-mix(") { |
| 349 | continue |
| 350 | } else if name, _, ok := splitVar(v); ok { |
| 351 | if _, declared := values[name]; !declared { |
| 352 | t.Errorf("themes/%s.css: %s reads %s, which this theme does not declare", name, prop, name) |
| 353 | } |
| 354 | continue |
| 355 | } |
| 356 | light, dark, ok := splitLightDark(values[prop]) |
| 357 | if !ok { |
| 358 | t.Errorf("themes/%s.css: %s is %q, want a whole-value light-dark(<light>, <dark>)", name, prop, values[prop]) |
| 359 | continue |
| 360 | } |
| 361 | if light == dark { |
| 362 | t.Errorf("themes/%s.css: %s declares %s for both schemes; that is an inverted dark theme wearing light-dark()'s clothes", name, prop, light) |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // reducedMotionAllowlist names selectors that carry a transition/animation |
| 369 | // with no matching disable under @media (prefers-reduced-motion: reduce), |
| 370 | // found pre-existing at the time this gate was added. The gate came |
| 371 | // alone; it did not silently fix CSS it did not write. Empty today — |
| 372 | // every transition tokens.css declares (rst-caret, rst-switch-track and |
| 373 | // its ::after, rst-tip::after) already has a reduce-block "transition: |
| 374 | // none" counterpart, so nothing needs listing. If a future change adds a |
| 375 | // transition without one, this test fails; add the selector here only |
| 376 | // with a comment explaining why it is deliberately left un-disabled, not |
| 377 | // as a way to silence the failure. |
| 378 | var reducedMotionAllowlist = map[string]bool{} |
| 379 | |
| 380 | // leafRule is one style rule, or one keyframe, as declarationsOf reads |
| 381 | // it: the at-rules around it, joined as a declaration's Context is; |
| 382 | // top, the top-level statement it sits in, counted from 0, so source |
| 383 | // order between rules can be read off it; its selector list as written, |
| 384 | // whitespace collapsed; and its declarations as cssDeclarations reads |
| 385 | // them. A gate reads a rule's declarations here rather than its text, |
| 386 | // so a "cursor: pointer" inside a string is not one. |
| 387 | type leafRule struct { |
| 388 | context string |
| 389 | top int |
| 390 | selector string |
| 391 | decls [][3]string |
| 392 | } |
| 393 | |
| 394 | // value is what the rule sets prop to, as the cascade reads one block: |
| 395 | // the last !important declaration of it, else the last. |
| 396 | func (r leafRule) value(prop string) (string, bool) { |
| 397 | value, found, important := "", false, false |
| 398 | for _, d := range r.decls { |
| 399 | if d[0] == prop && (d[2] != "" || !important) { |
| 400 | value, found, important = d[1], true, d[2] != "" |
| 401 | } |
| 402 | } |
| 403 | return value, found |
| 404 | } |
| 405 | |
| 406 | // sets reports whether the rule sets prop to value. |
| 407 | func (r leafRule) sets(prop, value string) bool { |
| 408 | v, ok := r.value(prop) |
| 409 | return ok && v == value |
| 410 | } |
| 411 | |
| 412 | // readLeafRules is every style rule and keyframe in css, nested in an |
| 413 | // at-rule or not, read with the declaration reader. A pattern over |
| 414 | // braces cut [rst-snippet] { --rst-x: "}"; cursor: pointer; } at the |
| 415 | // quoted brace, and a reader of braces alone took a { in a rule's |
| 416 | // block for a rule of its own and dropped the one around it. This one |
| 417 | // refuses what declarationsOf refuses, a nested rule among them. |
| 418 | func readLeafRules(css string) ([]leafRule, error) { |
| 419 | r := cssReader{} |
| 420 | bare, err := cssWithoutComments(css) |
| 421 | if err == nil { |
| 422 | err = r.rules(bare, nil) |
| 423 | } |
| 424 | if err != nil { |
| 425 | return nil, err |
| 426 | } |
| 427 | return r.leaves, nil |
| 428 | } |
| 429 | |
| 430 | // leafRules is readLeafRules failing the test on a stylesheet it |
| 431 | // refuses. |
| 432 | func leafRules(t testing.TB, css string) []leafRule { |
| 433 | t.Helper() |
| 434 | rules, err := readLeafRules(css) |
| 435 | if err != nil { |
| 436 | t.Fatal(err) |
| 437 | } |
| 438 | return rules |
| 439 | } |
| 440 | |
| 441 | // A brace inside a string is text to a browser, so it neither ends a |
| 442 | // rule nor starts one. Cut at the quoted } instead, the first rule |
| 443 | // below loses its cursor: pointer, and the tap inventory never sees |
| 444 | // the control it styles. |
| 445 | func TestLeafRulesReadABraceInAStringAsText(t *testing.T) { |
| 446 | const css = `[rst-snippet] { --rst-x: "}"; cursor: pointer; } [rst-a] { content: '{'; color: red } @media (x) { [rst-b] { cursor: pointer } }` |
| 447 | want := []leafRule{ |
| 448 | {top: 0, selector: "[rst-snippet]", decls: [][3]string{{"--rst-x", `"}"`, ""}, {"cursor", "pointer", ""}}}, |
| 449 | {top: 1, selector: "[rst-a]", decls: [][3]string{{"content", "'{'", ""}, {"color", "red", ""}}}, |
| 450 | {context: "@media (x)", top: 2, selector: "[rst-b]", decls: [][3]string{{"cursor", "pointer", ""}}}, |
| 451 | } |
| 452 | if got := leafRules(t, css); !reflect.DeepEqual(got, want) { |
| 453 | t.Errorf("leafRules(%q) =\n%+v\nwant\n%+v", css, got, want) |
| 454 | } |
| 455 | var controls []string |
| 456 | for _, r := range interactiveCSSRules(t, css) { |
| 457 | controls = append(controls, r.selector) |
| 458 | } |
| 459 | if want := []string{"[rst-snippet]", "[rst-b]"}; !reflect.DeepEqual(controls, want) { |
| 460 | t.Errorf("interactiveCSSRules(%q) finds %q, want %q", css, controls, want) |
| 461 | } |
| 462 | // A { in a rule's block is a nested rule to a browser, which still |
| 463 | // applies the rule around it. A reader of braces took the nested |
| 464 | // one for the leaf and lost [rst-c]'s cursor: pointer, so the tap |
| 465 | // inventory never saw the control. It is refused instead. |
| 466 | if rules, err := readLeafRules(`[rst-c] { cursor: pointer; [rst-d] { color: red } }`); err == nil { |
| 467 | t.Errorf("readLeafRules read a nested rule as %+v, want an error", rules) |
| 468 | } |
| 469 | // Within one block an !important declaration wins over a later |
| 470 | // plain one. |
| 471 | r := leafRule{decls: [][3]string{{"cursor", "pointer", "!important"}, {"cursor", "auto", ""}}} |
| 472 | if v, _ := r.value("cursor"); v != "pointer" { |
| 473 | t.Errorf("value(cursor) of %+v = %q, want pointer", r.decls, v) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // TestReducedMotionDisablesEveryTransition is the motion gate: |
| 478 | // every selector that declares a real transition or animation outside a |
| 479 | // prefers-reduced-motion block must have that exact selector — not a |
| 480 | // selector merely containing or contained by it — disable that exact |
| 481 | // same property (transition disabled by "transition: none", animation by |
| 482 | // "animation: none") somewhere inside a |
| 483 | // @media (prefers-reduced-motion: reduce) block. Two failure modes this |
| 484 | // specifically guards against, both found by mutation during review: |
| 485 | // declaring a *different* motion property on an already-listed selector |
| 486 | // (selector reappearing is not enough — the property must actually be |
| 487 | // neutralized), and a new selector merely overlapping textually with one |
| 488 | // already covered ("[rst-tip]" vs "[rst-tip]::after") — hence the exact |
| 489 | // selector match in motionProblems rather than a substring check. |
| 490 | // |
| 491 | // This gate is the early warning, not the authority. It finds a |
| 492 | // disable but does not check that the disable wins: a later rule for |
| 493 | // the same selector, or an !important one, beats a disable that came |
| 494 | // first, and this gate still passes. Telling which declaration wins |
| 495 | // means ordering and weighing the whole cascade, and a copy of that in |
| 496 | // Go would be one more reader to get wrong. The authority is the six |
| 497 | // parts of TestNothingMovesUnderReducedMotion |
| 498 | // (reduced_motion_browser_test.go), which ask the browser what every |
| 499 | // element on every example page computes under reduced motion. This |
| 500 | // one runs in a second with no browser, and names the selector to fix. |
| 501 | func TestReducedMotionDisablesEveryTransition(t *testing.T) { |
| 502 | assertReducedMotion(t, "tokens.css", string(fiesta.TokensCSS())) |
| 503 | } |
| 504 | |
| 505 | // shell.css is the other stylesheet a shell links, and every rule in it |
| 506 | // animates: the gate holds it to the same exact-selector rule, unchanged |
| 507 | // in what it accepts, so the slide cannot escape it. |
| 508 | func TestShellCSSReducedMotionDisablesEveryAnimation(t *testing.T) { |
| 509 | assertReducedMotion(t, "shell.css", string(fiesta.ShellCSS())) |
| 510 | } |
| 511 | |
| 512 | // The motion gate reads declarations as a browser does. A quoted brace |
| 513 | // is text, so "{" in one reduce rule and "}" in a later rule do not |
| 514 | // pair up and carry .missed into the reduce block; a quoted |
| 515 | // "transition: none" is a string, not a disable; and TRANSITION is |
| 516 | // transition. Each hole lets a transition run under reduced motion. The last stylesheet is the |
| 517 | // control: its one transition is disabled, so the gate passes. |
| 518 | func TestTheMotionGateHasNoKnownHoles(t *testing.T) { |
| 519 | for _, c := range []struct{ css, missed string }{ |
| 520 | {`.ok { transition: opacity 1s; } |
| 521 | @media (prefers-reduced-motion: reduce) { |
| 522 | .ok { transition: none; } |
| 523 | .marker { --rst-open: "{"; } |
| 524 | } |
| 525 | .missed { transition: opacity 1s; } |
| 526 | .marker { --rst-close: "}"; }`, ".missed"}, |
| 527 | {`.moving { transition: opacity 1s; } |
| 528 | @media (prefers-reduced-motion: reduce) { |
| 529 | .moving { --rst-proof: "; transition: none;"; } |
| 530 | }`, ".moving"}, |
| 531 | // A property name is not case-sensitive to a browser. |
| 532 | {`.loud { TRANSITION: opacity 1s; } |
| 533 | @media (prefers-reduced-motion: reduce) { |
| 534 | .other { transition: none; } |
| 535 | }`, ".loud"}, |
| 536 | } { |
| 537 | problems, _, err := motionProblems(c.css) |
| 538 | named := false |
| 539 | for _, p := range problems { |
| 540 | named = named || strings.Contains(p, `"`+c.missed+`"`) |
| 541 | } |
| 542 | if err == nil && !named { |
| 543 | t.Errorf("the motion gate passes %s, which still moves under reduced motion:\n%s", c.missed, c.css) |
| 544 | } |
| 545 | } |
| 546 | const control = `.ok { transition: opacity 1s; } @media (prefers-reduced-motion: reduce) { .ok { transition: none; } }` |
| 547 | if problems, tested, err := motionProblems(control); err != nil || len(problems) != 0 || tested != 1 { |
| 548 | t.Errorf("the motion gate on a disabled transition: %v, %d checked, err %v; want no problems and 1 checked", problems, tested, err) |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // braceMatchEnd reads a quoted brace as text, so the block it finds |
| 553 | // ends at the brace that closes it, not at one inside a string. |
| 554 | func TestBraceMatchEndReadsAStringAsText(t *testing.T) { |
| 555 | const css = `@media x { a { b: "}"; } c { d: "{"; } } e { f: "}" }` |
| 556 | start := strings.Index(css, "{") + 1 |
| 557 | want := strings.Index(css, "} } e") + 2 |
| 558 | if got, err := braceMatchEnd(css, start); err != nil || got != want { |
| 559 | t.Errorf("braceMatchEnd(%q, %d) = %d, %v; want %d", css, start, got, err, want) |
| 560 | } |
| 561 | // A brace that closes the wrong bracket, or none, is an error, not |
| 562 | // the end of the file. |
| 563 | for _, css := range []string{`{ a: ( }`, `{ a: "}"`} { |
| 564 | if end, err := braceMatchEnd(css, 1); err == nil { |
| 565 | t.Errorf("braceMatchEnd(%q, 1) = %d, want an error", css, end) |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | func assertReducedMotion(t *testing.T, name, css string) { |
| 571 | t.Helper() |
| 572 | problems, tested, err := motionProblems(css) |
| 573 | if err != nil { |
| 574 | t.Fatalf("%s: %v", name, err) |
| 575 | } |
| 576 | for _, p := range problems { |
| 577 | t.Error(p) |
| 578 | } |
| 579 | if tested == 0 { |
| 580 | t.Fatal("found no transition/animation declarations to check outside reduce blocks — the parser likely broke, not that " + name + " lost every transition") |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // motionProblems is the motion gate over one stylesheet: a problem for |
| 585 | // each selector's transition or animation that no reduce block |
| 586 | // disables, and how many it checked. It reads the stylesheet with |
| 587 | // declarationsOf, so a brace or a "transition: none" inside a string |
| 588 | // is text, as it is to a browser, and a selector list is split where |
| 589 | // a browser splits it. |
| 590 | func motionProblems(css string) (problems []string, tested int, err error) { |
| 591 | decls, err := declarationsOf("", css) |
| 592 | if err != nil { |
| 593 | return nil, 0, err |
| 594 | } |
| 595 | const reduce = "@media (prefers-reduced-motion: reduce)" |
| 596 | inReduce := func(d declaration) bool { |
| 597 | return slices.Contains(strings.Split(d.Context, " > "), reduce) |
| 598 | } |
| 599 | motion := func(d declaration) bool { return d.Property == "transition" || d.Property == "animation" } |
| 600 | type disable struct{ selector, property string } |
| 601 | disabled := map[disable]bool{} |
| 602 | for _, d := range decls { |
| 603 | if inReduce(d) && motion(d) && d.Value == "none" { |
| 604 | disabled[disable{d.Selector, d.Property}] = true |
| 605 | } |
| 606 | } |
| 607 | if !slices.ContainsFunc(decls, inReduce) { |
| 608 | return nil, 0, fmt.Errorf("declares no %s block at all", reduce) |
| 609 | } |
| 610 | for _, d := range decls { |
| 611 | // none is already disabled unconditionally; nothing to gate. |
| 612 | if !motion(d) || inReduce(d) || d.Value == "none" { |
| 613 | continue |
| 614 | } |
| 615 | tested++ |
| 616 | if reducedMotionAllowlist[d.Selector] || disabled[disable{d.Selector, d.Property}] { |
| 617 | continue |
| 618 | } |
| 619 | problems = append(problems, fmt.Sprintf("selector %q declares %s: %s with no exact-selector %q: none under prefers-reduced-motion: reduce (add one, or add %q to reducedMotionAllowlist with a reason)", |
| 620 | d.Selector, d.Property, d.Value, d.Property+": none", d.Selector)) |
| 621 | } |
| 622 | return problems, tested, nil |
| 623 | } |
| 624 | |
| 625 | // The one non-partial asset this package ships gets the same bar as the |
| 626 | // partials and the vendored icons. |
| 627 | func TestTokensCSSIsSelfContained(t *testing.T) { |
| 628 | reachOut := []string{"@import", "url(", "http://", "https://", "//fonts", "src:"} |
| 629 | css := string(fiesta.TokensCSS()) |
| 630 | for _, bad := range reachOut { |
| 631 | if strings.Contains(css, bad) { |
| 632 | t.Errorf("tokens.css reaches outside the page (%q)", bad) |
| 633 | } |
| 634 | } |
| 635 | // The promise spans both scaffolded stylesheets: a theme is where a |
| 636 | // webfont would be most tempting, and it is exactly as banned there. |
| 637 | for _, name := range fiesta.ThemeNames() { |
| 638 | theme, ok := fiesta.ThemeCSS(name) |
| 639 | if !ok { |
| 640 | t.Fatalf("fiesta.ThemeCSS(%q) missing", name) |
| 641 | } |
| 642 | for _, bad := range reachOut { |
| 643 | if strings.Contains(string(theme), bad) { |
| 644 | t.Errorf("themes/%s.css reaches outside the page (%q)", name, bad) |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // A var() naming a token nobody declares resolves to the property's |
| 651 | // initial value with no error anywhere — the date picker's muted text |
| 652 | // shipped as full-strength body colour that way, through a misspelt |
| 653 | // --rst-muted. So every fallback-less reference in tokens.css must be |
| 654 | // declared by tokens.css itself or by every theme; a reference that |
| 655 | // carries a fallback is an app-settable hook (--rst-cols) and is fine |
| 656 | // undeclared. |
| 657 | func TestEveryTokenReferenceIsDeclared(t *testing.T) { |
| 658 | // Read as declarations, not text: a "--rst-x:" in prose or in a |
| 659 | // string would otherwise count as a declaration, and these files |
| 660 | // are mostly prose. var() is read with varRefs, so var( --rst-x ) |
| 661 | // spaced is still a reference without a fallback. |
| 662 | tokens, _ := parseDeclarations(t, "tokens.css", string(fiesta.TokensCSS())) |
| 663 | sheets := map[string][]declaration{"tokens.css": tokens} |
| 664 | for _, name := range fiesta.ThemeNames() { |
| 665 | theme, _ := fiesta.ThemeCSS(name) |
| 666 | where := "themes/" + name + ".css" |
| 667 | sheets[where] = themeDeclarations(t, where, string(theme)) |
| 668 | } |
| 669 | declared := map[string]bool{} |
| 670 | inEvery := map[string]int{} |
| 671 | for file, decls := range sheets { |
| 672 | seen := map[string]bool{} |
| 673 | for _, d := range decls { |
| 674 | if !strings.HasPrefix(d.Property, "--rst-") { |
| 675 | continue |
| 676 | } |
| 677 | if file == "tokens.css" { |
| 678 | declared[d.Property] = true |
| 679 | } else if !seen[d.Property] { |
| 680 | seen[d.Property] = true |
| 681 | inEvery[d.Property]++ |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | for tok, n := range inEvery { |
| 686 | if n == len(fiesta.ThemeNames()) { |
| 687 | declared[tok] = true |
| 688 | } |
| 689 | } |
| 690 | // A theme derives tokens from tokens (--rst-header-rule mixes the |
| 691 | // accent), so its references are held to the same bar. |
| 692 | for _, file := range sortedKeys(sheets) { |
| 693 | reported := map[string]bool{} |
| 694 | for _, d := range sheets[file] { |
| 695 | for _, ref := range varRefs(d.Value) { |
| 696 | if !ref.fallback && strings.HasPrefix(ref.name, "--rst-") && !declared[ref.name] && !reported[ref.name] { |
| 697 | reported[ref.name] = true |
| 698 | t.Errorf("%s uses var(%s), which neither tokens.css nor every theme declares", file, ref.name) |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // varRefs reads a var() as a browser does. A pattern over the text |
| 706 | // took var(--rst-a) quoted in a string for a reference, missed |
| 707 | // var( --rst-b ) for its spacing, and so could pass a token nobody |
| 708 | // declares or fail one nobody reads. |
| 709 | func TestVarRefsReadAStringAsText(t *testing.T) { |
| 710 | got := varRefs(`"var(--rst-a)" var( --rst-b ) var(--rst-c, var(--rst-d)) VAR(--rst-e,)`) |
| 711 | want := []varRef{{"--rst-b", false}, {"--rst-c", true}, {"--rst-d", false}, {"--rst-e", true}} |
| 712 | if !reflect.DeepEqual(got, want) { |
| 713 | t.Errorf("varRefs = %+v, want %+v", got, want) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // varRef is one var() a value reads: the property it names, and |
| 718 | // whether it carries a fallback. |
| 719 | type varRef struct { |
| 720 | name string |
| 721 | fallback bool |
| 722 | } |
| 723 | |
| 724 | // varRefs is every var() in value, a var() inside another's fallback |
| 725 | // included, read with cssSpan and braceMatchEnd: a var( inside a |
| 726 | // string is text, and spacing inside the brackets changes nothing. |
| 727 | func varRefs(value string) []varRef { |
| 728 | var out []varRef |
| 729 | for i := 0; i < len(value); { |
| 730 | end, _, err := cssSpan(value, i) |
| 731 | if err != nil { |
| 732 | return out |
| 733 | } |
| 734 | if strings.EqualFold(value[i:end], "var") && end < len(value) && value[end] == '(' { |
| 735 | close, err := braceMatchEnd(value, end+1) |
| 736 | if err != nil { |
| 737 | return out |
| 738 | } |
| 739 | out = append(out, readVarRef(strings.TrimLeft(value[end+1:close], " \t\n\r\f"))) |
| 740 | // Read on inside the brackets: a fallback can hold a var() |
| 741 | // of its own. |
| 742 | end++ |
| 743 | } |
| 744 | i = end |
| 745 | } |
| 746 | return out |
| 747 | } |
| 748 | |
| 749 | // readVarRef reads what is inside one var(): the name it starts with, |
| 750 | // and whether a comma outside any bracket or string follows. |
| 751 | func readVarRef(inner string) varRef { |
| 752 | var ref varRef |
| 753 | depth := 0 |
| 754 | for j := 0; j < len(inner); { |
| 755 | end, _, err := cssSpan(inner, j) |
| 756 | if err != nil { |
| 757 | break |
| 758 | } |
| 759 | if j == 0 { |
| 760 | ref.name = inner[:end] |
| 761 | } |
| 762 | switch inner[j] { |
| 763 | case '(', '[': |
| 764 | depth++ |
| 765 | case ')', ']': |
| 766 | depth-- |
| 767 | case ',': |
| 768 | ref.fallback = ref.fallback || depth == 0 |
| 769 | } |
| 770 | j = end |
| 771 | } |
| 772 | return ref |
| 773 | } |
| 774 | |
| 775 | // Same drift check again, for the form family: field, field-select, |
| 776 | // field-textarea, field-check and choice-field between them can emit |
| 777 | // every one of these, and every one is spelled as the examples spell it. |
| 778 | func TestFormPartialClassesAreStyled(t *testing.T) { |
| 779 | attributes := styledAttributes(t, string(fiesta.TokensCSS())) |
| 780 | for _, name := range []string{ |
| 781 | "rst-field", "rst-field-label", "rst-field-hint", "rst-field-help", "rst-field-error", |
| 782 | "rst-input", "rst-input~=short", "rst-input~=primary", |
| 783 | "rst-switch", "rst-switch-track", |
| 784 | "rst-choice", "rst-choice-cards", "rst-choice-title", "rst-choice-desc", |
| 785 | "rst-seg-tabs", |
| 786 | } { |
| 787 | if !tokensStyle(attributes, name) { |
| 788 | t.Errorf("tokens.css has no selector for %q", name) |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | // Same drift check again, direct rather than sample-driven: the classes |
| 794 | // rastrillo's partials (confirm-form, back-nav, notice, form-error, |
| 795 | // bulk-bar) emit themselves, so there is no arbitrary caller-composed |
| 796 | // body for an example to carry them — their examples already hold the |
| 797 | // partials' fixtures, and this pins |
| 798 | // that every class they can render resolves to a tokens.css selector. |
| 799 | func TestRoutesFamilyPartialClassesAreStyled(t *testing.T) { |
| 800 | attributes := styledAttributes(t, string(fiesta.TokensCSS())) |
| 801 | for _, name := range []string{ |
| 802 | "rst-btn~=ghost", "rst-btn~=danger", |
| 803 | "rst-back-nav", "rst-notice", "rst-form-error", |
| 804 | "rst-bulkbar", "rst-bulkbar-close", "rst-bulkbar-count", "rst-bulkbar-escalate", |
| 805 | } { |
| 806 | if !tokensStyle(attributes, name) { |
| 807 | t.Errorf("tokens.css has no selector for %q", name) |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | func TestJobStatusPartialClassesAreStyled(t *testing.T) { |
| 813 | attributes := styledAttributes(t, string(fiesta.TokensCSS())) |
| 814 | for _, name := range []string{"rst-spin"} { |
| 815 | if !tokensStyle(attributes, name) { |
| 816 | t.Errorf("tokens.css has no selector for %q", name) |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // The dropdown's exclusivity (only one open at a time) is the native |
| 822 | // <details name> attribute, not JavaScript — this pins both halves of |
| 823 | // that promise. |
| 824 | func TestDropdownExclusivityIsNative(t *testing.T) { |
| 825 | sample := dropdownSample(t) |
| 826 | if !strings.Contains(sample, `<details rst-dropdown name=`) { |
| 827 | t.Errorf("dropdown sample's outer <details> carries no name attribute: %s", sample) |
| 828 | } |
| 829 | if strings.Contains(sample, "<script") { |
| 830 | t.Errorf("dropdown sample reaches for <script>; exclusivity must stay native: %s", sample) |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | // detailsNamePattern reads the name attribute off a <details> open tag, |
| 835 | // whichever order the attributes are written in. |
| 836 | var detailsNamePattern = regexp.MustCompile(`<details [^>]*name="([^"]*)"`) |
| 837 | |
| 838 | // The submenu trap, pinned. <details name> exclusivity is DOCUMENT-wide, |
| 839 | // not sibling-scoped: two elements sharing a name close each other even |
| 840 | // when one is inside the other. A nested rst-menu-group that inherited |
| 841 | // its parent's group would therefore close the parent the instant it |
| 842 | // opened — the submenu flashes and the whole menu vanishes under the |
| 843 | // pointer. The nested sample must carry a name of its own, and it must |
| 844 | // not be the parent's. |
| 845 | func TestNestedMenuGroupNeverSharesItsParentsName(t *testing.T) { |
| 846 | sample := dropdownSample(t) |
| 847 | names := detailsNamePattern.FindAllStringSubmatch(sample, -1) |
| 848 | if len(names) != 2 { |
| 849 | t.Fatalf("expected two named <details> in the dropdown sample, found %d: %s", len(names), sample) |
| 850 | } |
| 851 | if !strings.Contains(sample, "rst-menu-group") { |
| 852 | t.Fatalf("the dropdown sample no longer nests an rst-menu-group: %s", sample) |
| 853 | } |
| 854 | if names[0][1] == names[1][1] { |
| 855 | t.Errorf("the nested rst-menu-group shares its parent's group %q; opening it would close the parent", names[0][1]) |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | // The modal panel is <dialog open>: the rendered-open, non-modal dialog, |
| 860 | // which is exactly what a modal-as-a-URL already was. Nothing calls |
| 861 | // showModal(), so nothing enters the top layer, ::backdrop never paints, |
| 862 | // and the overlay div stays the scrim. The UA's own dialog block has to |
| 863 | // be undone or the panel lays out absolutely positioned with 1em of |
| 864 | // padding. |
| 865 | func TestModalPanelIsARenderedOpenDialog(t *testing.T) { |
| 866 | sample := idiomMarkup(t, "modal") |
| 867 | for _, want := range []string{ |
| 868 | `<dialog rst-modal-panel open `, `</dialog>`, |
| 869 | `<div rst-modal-overlay>`, `<div rst-backdrop inert>`, |
| 870 | } { |
| 871 | if !strings.Contains(sample, want) { |
| 872 | t.Errorf("modal sample missing %q: %s", want, sample) |
| 873 | } |
| 874 | } |
| 875 | if strings.Contains(sample, `<div rst-modal-panel>`) { |
| 876 | t.Errorf("the modal panel is still a div: %s", sample) |
| 877 | } |
| 878 | // A dialog role with no accessible name is announced as "dialog" and |
| 879 | // nothing else, and fails axe's aria-dialog-name — a failure this |
| 880 | // vocabulary created for itself the moment the panel stopped being a |
| 881 | // div. The name comes from the panel's own heading rather than a |
| 882 | // string in this library, so it is already in the page's language. |
| 883 | id := regexp.MustCompile(`aria-labelledby="([^"]+)"`).FindStringSubmatch(sample) |
| 884 | if id == nil { |
| 885 | t.Fatalf("the dialog has no accessible name: %s", sample) |
| 886 | } |
| 887 | if !strings.Contains(sample, `<h2 id="`+id[1]+`">`) { |
| 888 | t.Errorf("aria-labelledby=%q points at no heading in the panel: %s", id[1], sample) |
| 889 | } |
| 890 | // Comments stripped first: this file explains ::backdrop and names |
| 891 | // the reset rule in prose, and a check that cannot tell a selector |
| 892 | // from a sentence about one is not a check. |
| 893 | // Read as rules: a check that cannot tell a selector from a |
| 894 | // sentence about one, or from a string, is not a check. |
| 895 | css := stripCSSComments(t, string(fiesta.TokensCSS())) |
| 896 | var reset *leafRule |
| 897 | for _, r := range leafRules(t, css) { |
| 898 | if slices.ContainsFunc(splitSelectorList(r.selector), func(s string) bool { return strings.Contains(s, "dialog[rst-modal-panel]") }) { |
| 899 | reset = &r |
| 900 | break |
| 901 | } |
| 902 | } |
| 903 | if reset == nil { |
| 904 | t.Fatal("tokens.css has no scoped dialog reset; the UA dialog block would position the panel absolutely") |
| 905 | } |
| 906 | for _, want := range [][2]string{{"position", "static"}, {"padding", "0"}, {"margin", "0"}, {"color", "inherit"}} { |
| 907 | if !reset.sets(want[0], want[1]) { |
| 908 | t.Errorf("the dialog reset does not undo the UA block's %s: %s: %s { %v }", want[0], want[1], reset.selector, reset.decls) |
| 909 | } |
| 910 | } |
| 911 | // ::backdrop belongs to the top layer, which a rendered-open dialog |
| 912 | // never reaches. Styling it would be dead CSS that reads as if the |
| 913 | // scrim came from the element. |
| 914 | if strings.Contains(css, "::backdrop") { |
| 915 | t.Error("tokens.css styles ::backdrop; a rendered-open dialog never paints one — the overlay div is the scrim") |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | // The focus ring covers the whole app column, so a hand-rolled control |
| 920 | // inside [rst-page] no longer restates the outline. |
| 921 | func TestFocusRingScopeIncludesThePageColumn(t *testing.T) { |
| 922 | sels := ruleSelectors(t, string(fiesta.TokensCSS())) |
| 923 | if !slices.ContainsFunc(sels, func(s string) bool { return strings.HasPrefix(s, ":where([rst-page],") }) { |
| 924 | t.Error("tokens.css :focus-visible scope does not start with [rst-page]") |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // .rst-sr-only and the component rules it has to beat are all single |
| 929 | // class selectors, so the cascade falls to source order: declared before |
| 930 | // them it loses, and anything carrying both classes ends up a full-width |
| 931 | // absolutely-positioned box hidden only by clip-path. That is how a |
| 932 | // hidden <select> nearly shipped at full width — the utility must come |
| 933 | // last, and this stops it drifting back. |
| 934 | func TestSrOnlyUtilityComesAfterTheRulesItMustBeat(t *testing.T) { |
| 935 | // Source order is read off the rules, so a selector mentioned in a |
| 936 | // comment or a string earlier in the file is not taken for the |
| 937 | // rule. |
| 938 | rules := leafRules(t, string(fiesta.TokensCSS())) |
| 939 | first := func(sel string) int { |
| 940 | for i, r := range rules { |
| 941 | if slices.Contains(splitSelectorList(r.selector), sel) { |
| 942 | return i |
| 943 | } |
| 944 | } |
| 945 | return -1 |
| 946 | } |
| 947 | srOnly := first(".rst-sr-only") |
| 948 | if srOnly < 0 { |
| 949 | t.Fatal("tokens.css no longer defines .rst-sr-only") |
| 950 | } |
| 951 | for _, competitor := range []string{"[rst-input]", "[rst-btn]"} { |
| 952 | at := first(competitor) |
| 953 | if at < 0 { |
| 954 | continue // that component may have been renamed; not this test's business |
| 955 | } |
| 956 | if at > srOnly { |
| 957 | t.Errorf("%s is declared after .rst-sr-only; the utility must come last or it loses the cascade to it", competitor) |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | // A separator inside a menu panel is an <hr>, and a UA <hr> is a thick |
| 963 | // inset 3D rule that looks nothing like the hairline the menus draw. The |
| 964 | // rule that neutralises it existed but was scoped to one panel — |
| 965 | // [rst-row-menu-panel] — so the topbar account menu, which is a |
| 966 | // [rst-dropdown-menu], rendered the browser's own. The panels are one |
| 967 | // surface with three entry points, so the rule has to name all three. |
| 968 | // |
| 969 | // Selectors, not substrings: "[rst-dropdown-menu] hr" is not satisfied |
| 970 | // by "[rst-dropdown-menu] hr" appearing inside some longer selector that |
| 971 | // does not actually match a menu separator. |
| 972 | // |
| 973 | // Values, not substrings either. An earlier version of this gate asked |
| 974 | // only whether the rule body contained "border-top", which |
| 975 | // `border-top: 0` satisfies while the UA's groove comes straight back. |
| 976 | // It takes BOTH halves to neutralise it — `border: 0` to drop the |
| 977 | // inset 3D box, then a real `border-top` to draw the hairline — so both |
| 978 | // are checked, and the width is checked for being non-zero. |
| 979 | func TestEveryMenuSurfaceStylesItsSeparator(t *testing.T) { |
| 980 | styled := map[string]bool{} |
| 981 | for _, rule := range leafRules(t, string(fiesta.TokensCSS())) { |
| 982 | border, _ := rule.value("border") |
| 983 | top, _ := rule.value("border-top") |
| 984 | if !resetsBorder(border) || !drawsARule(top) { |
| 985 | continue |
| 986 | } |
| 987 | for _, sel := range splitSelectorList(rule.selector) { |
| 988 | styled[sel] = true |
| 989 | } |
| 990 | } |
| 991 | for _, want := range []string{ |
| 992 | "[rst-row-menu-panel] hr", |
| 993 | "[rst-dropdown-menu] hr", |
| 994 | "[rst-locale] hr", |
| 995 | } { |
| 996 | if !styled[want] { |
| 997 | t.Errorf("no rule both resets the UA border and draws a hairline for %q; that menu renders the browser's own thick inset <hr>", want) |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // resetsBorder reports whether a value drops the UA's inset 3D border |
| 1003 | // altogether. Only the shorthand does that; a longhand leaves three |
| 1004 | // sides of groove behind. |
| 1005 | func resetsBorder(val string) bool { |
| 1006 | switch strings.ToLower(val) { |
| 1007 | case "0", "0px", "none": |
| 1008 | return true |
| 1009 | } |
| 1010 | return false |
| 1011 | } |
| 1012 | |
| 1013 | // drawsARule reports whether a border-top value paints a visible line: |
| 1014 | // a width that is not zero, and a style that is not none. "0", |
| 1015 | // "0 solid …" and "1px none …" all fail it. |
| 1016 | func drawsARule(val string) bool { |
| 1017 | fields := strings.Fields(strings.ToLower(val)) |
| 1018 | if len(fields) == 0 { |
| 1019 | return false |
| 1020 | } |
| 1021 | for _, f := range fields { |
| 1022 | if f == "none" || f == "hidden" { |
| 1023 | return false |
| 1024 | } |
| 1025 | } |
| 1026 | width := fields[0] |
| 1027 | if width == "0" { |
| 1028 | return false |
| 1029 | } |
| 1030 | // A bare unit-ful zero — 0px, 0rem, 0em — is still zero. |
| 1031 | if num := strings.TrimRight(width, "abcdefghijklmnopqrstuvwxyz%"); num != "" { |
| 1032 | if f, err := strconv.ParseFloat(num, 64); err == nil && f == 0 { |
| 1033 | return false |
| 1034 | } |
| 1035 | } |
| 1036 | return true |
| 1037 | } |
| 1038 | |
| 1039 | // Every theme file must declare exactly the same set of --rst-* |
| 1040 | // properties as day, the reference theme. A theme that forgets a token |
| 1041 | // does not fail loudly at render time — it silently falls back to |
| 1042 | // whatever the cascade already had, which in a scaffolded app is |
| 1043 | // nothing at all, so the affected element renders unstyled rather than |
| 1044 | // wrong. Since v2 that set includes the shape tokens (the radii and the |
| 1045 | // four depth values), which used to be tokens.css's and are now part of |
| 1046 | // what a theme has to answer for. Names only: values are the theme's |
| 1047 | // whole point, and their contrast is contrast_test.go's job. |
| 1048 | func TestThemesDeclareIdenticalTokenSets(t *testing.T) { |
| 1049 | names := fiesta.ThemeNames() |
| 1050 | if len(names) == 0 || names[0] != "day" { |
| 1051 | t.Fatalf("ThemeNames = %v; day must exist and come first", names) |
| 1052 | } |
| 1053 | want := themePropSet(t, "day") |
| 1054 | if len(want) == 0 { |
| 1055 | t.Fatal("day declares no --rst- properties") |
| 1056 | } |
| 1057 | for _, shape := range []string{ |
| 1058 | "--rst-radius", "--rst-radius-sm", "--rst-radius-pill", |
| 1059 | "--rst-shadow-pop", "--rst-shadow-knob", "--rst-shadow-knob-hover", "--rst-shadow-lift", "--rst-overlay", |
| 1060 | // The depth axis. day turns |
| 1061 | // it on; plain and signal declare the same names at today's |
| 1062 | // values. A name missing from day here is a token tokens.css |
| 1063 | // would read through its fallback forever. |
| 1064 | "--rst-accent-fill", "--rst-accent-fill-hover", "--rst-accent-fill-edge", "--rst-accent-fill-edge-hover", "--rst-on-accent-fill", |
| 1065 | "--rst-danger-fill", "--rst-danger-fill-hover", "--rst-danger-fill-edge", "--rst-danger-fill-edge-hover", "--rst-on-danger-fill", "--rst-on-danger-fill-hover", "--rst-danger-hover-filter", |
| 1066 | "--rst-control-face", "--rst-control-face-hover", "--rst-control-face-press", "--rst-control-border", "--rst-control-border-hover", "--rst-control-text-hover", |
| 1067 | "--rst-ghost-edge", "--rst-ghost-face-hover", "--rst-ghost-edge-hover", "--rst-summary-face", "--rst-summary-face-hover", "--rst-summary-edge", "--rst-summary-edge-hover", "--rst-summary-radius", |
| 1068 | "--rst-depth-hl", "--rst-depth-contact", "--rst-depth-ambient", "--rst-depth-face", "--rst-press-drop", |
| 1069 | "--rst-knob", "--rst-knob-shade", "--rst-track-off", |
| 1070 | "--rst-switch-w", "--rst-switch-h", "--rst-switch-knob", "--rst-switch-touch-w", "--rst-switch-touch-h", "--rst-switch-touch-knob", |
| 1071 | "--rst-tint-hover", "--rst-tint-press", "--rst-tint-tap", "--rst-tint-card-hover", "--rst-tint-tile-hover", "--rst-tint-tile-press", "--rst-tint-group-hover", "--rst-row-tint-hover", "--rst-row-tint-press", "--rst-focus-edge", "--rst-focus-halo", |
| 1072 | "--rst-sheen-fill", "--rst-sheen-plain", "--rst-light-rim", "--rst-light-wash", |
| 1073 | "--rst-press-wash", "--rst-press-spot", "--rst-press-wash-plain", "--rst-press-spot-plain", |
| 1074 | "--rst-title-size", "--rst-title-weight", "--rst-title-track", "--rst-heading-size", |
| 1075 | "--rst-label-case", "--rst-label-track", "--rst-rail-bg", |
| 1076 | "--rst-nav-current-bg", "--rst-nav-current-fg", |
| 1077 | } { |
| 1078 | if !want[shape] { |
| 1079 | t.Errorf("day does not declare %s; the shape axis is the theme's since v2", shape) |
| 1080 | } |
| 1081 | } |
| 1082 | for _, n := range names[1:] { |
| 1083 | got := themePropSet(t, n) |
| 1084 | for p := range want { |
| 1085 | if !got[p] { |
| 1086 | t.Errorf("theme %s is missing %s", n, p) |
| 1087 | } |
| 1088 | } |
| 1089 | for p := range got { |
| 1090 | if !want[p] { |
| 1091 | t.Errorf("theme %s declares %s, which day does not", n, p) |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | // themePropSet: every --rst-* name declared anywhere in the theme |
| 1098 | // file, read as declarations: a pattern over the text counted one a |
| 1099 | // comment mentions as declared. |
| 1100 | func themePropSet(t *testing.T, name string) map[string]bool { |
| 1101 | t.Helper() |
| 1102 | css, ok := fiesta.ThemeCSS(name) |
| 1103 | if !ok { |
| 1104 | t.Fatalf("fiesta.ThemeCSS(%q) missing", name) |
| 1105 | } |
| 1106 | out := map[string]bool{} |
| 1107 | for _, d := range themeDeclarations(t, "themes/"+name+".css", string(css)) { |
| 1108 | if strings.HasPrefix(d.Property, "--rst-") { |
| 1109 | out[d.Property] = true |
| 1110 | } |
| 1111 | } |
| 1112 | return out |
| 1113 | } |
| 1114 | |
| 1115 | // schemeNumberBlocks returns the numbers a theme restates for dark, or |
| 1116 | // nil when it restates none, and fails the test for every problem |
| 1117 | // schemeNumberProblems reports. |
| 1118 | func schemeNumberBlocks(t *testing.T, name, css string) map[string]string { |
| 1119 | t.Helper() |
| 1120 | numbers, problems := schemeNumberProblems(name, css) |
| 1121 | for _, p := range problems { |
| 1122 | t.Error(p) |
| 1123 | } |
| 1124 | return numbers |
| 1125 | } |
| 1126 | |
| 1127 | // schemeNumberProblems reads a theme file against the shape a theme may take: |
| 1128 | // a :root rule; the two toggle rules |
| 1129 | // `:root[data-theme="light"] { color-scheme: light; }` and |
| 1130 | // `:root[data-theme="dark"] { color-scheme: dark; }`; and, together or |
| 1131 | // not at all, `:root[data-theme="dark"] { numbers }` and |
| 1132 | // `@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { numbers } }`. |
| 1133 | // Every declaration in a number block must be a --rst-* name with a |
| 1134 | // bare number (an optional px, em or rem unit), nothing else may be in |
| 1135 | // the block, the two blocks must carry identical declarations, and |
| 1136 | // every name restated must be declared in :root as a number too. Both |
| 1137 | // blocks, because a reader who chose Dark has data-theme and a reader on |
| 1138 | // System on a dark OS has only the media query; one block alone hands |
| 1139 | // the other reader the light numbers. Nothing beyond those five pieces |
| 1140 | // may be in the file: anything else is the second palette block |
| 1141 | // light-dark() was adopted to make impossible. It returns the numbers |
| 1142 | // restated for dark. |
| 1143 | // |
| 1144 | // It reads the file with the declaration reader and sorts each |
| 1145 | // declaration by the rule and at-rule it sits in. Patterns over the |
| 1146 | // text found a toggle or a number block quoted in a string, and took |
| 1147 | // it for the real one the file no longer had. |
| 1148 | func schemeNumberProblems(name, css string) (numbers map[string]string, problems []string) { |
| 1149 | where := "themes/" + name + ".css" |
| 1150 | bad := func(format string, args ...any) { problems = append(problems, where+": "+fmt.Sprintf(format, args...)) } |
| 1151 | decls, err := declarationsOf(where, css) |
| 1152 | if err != nil { |
| 1153 | bad("%v", err) |
| 1154 | return nil, problems |
| 1155 | } |
| 1156 | const ( |
| 1157 | toggleLight = `:root[data-theme="light"]` |
| 1158 | toggleDark = `:root[data-theme="dark"]` |
| 1159 | darkMedia = "@media (prefers-color-scheme: dark)" |
| 1160 | darkSystem = `:root:not([data-theme="light"])` |
| 1161 | ) |
| 1162 | root := declaredAt(decls, "", ":root") |
| 1163 | if len(root) == 0 { |
| 1164 | bad("has no :root block") |
| 1165 | return nil, problems |
| 1166 | } |
| 1167 | toggles := map[string]int{} |
| 1168 | var attr, media []declaration |
| 1169 | for _, d := range decls { |
| 1170 | switch { |
| 1171 | case d.Context == "" && d.Selector == ":root": |
| 1172 | case d.Context == "" && d.Selector == toggleLight && d.Property == "color-scheme" && fullValue(d) == "light": |
| 1173 | toggles["light"]++ |
| 1174 | case d.Context == "" && d.Selector == toggleDark && d.Property == "color-scheme" && fullValue(d) == "dark": |
| 1175 | toggles["dark"]++ |
| 1176 | case d.Context == "" && d.Selector == toggleDark: |
| 1177 | attr = append(attr, d) |
| 1178 | case d.Context == darkMedia && d.Selector == darkSystem: |
| 1179 | media = append(media, d) |
| 1180 | // One more block is allowed: a relative-colour redeclaration of |
| 1181 | // names :root already declares (day's hue-derived fills), so |
| 1182 | // that an engine below the floor keeps the :root value instead |
| 1183 | // of a fill that resolves to nothing (derived_test.go). Nothing |
| 1184 | // else may be in it, and it may not introduce a name. |
| 1185 | case d.Context == accentSupports && d.Selector == ":root": |
| 1186 | if !strings.HasPrefix(d.Property, "--rst-") { |
| 1187 | bad("carries something other than --rst-* declarations in its relative-colour block: %s", d) |
| 1188 | } else if _, ok := root[d.Property]; !ok { |
| 1189 | bad("redeclares %s in its relative-colour block but never declares it in :root, so an engine without relative colour has no value for it", d.Property) |
| 1190 | } |
| 1191 | default: |
| 1192 | bad("carries rules beyond :root, the toggles and the two dark-number blocks: %s", d) |
| 1193 | } |
| 1194 | } |
| 1195 | if toggles["light"] != 1 || toggles["dark"] != 1 { |
| 1196 | bad("does not carry the two toggle rules exactly once") |
| 1197 | } |
| 1198 | if len(attr) == 0 && len(media) == 0 { |
| 1199 | return nil, problems |
| 1200 | } |
| 1201 | if len(attr) == 0 || len(media) == 0 { |
| 1202 | bad("restates dark numbers in %d attribute and %d media declarations, want them in both blocks or neither", len(attr), len(media)) |
| 1203 | return nil, problems |
| 1204 | } |
| 1205 | num := regexp.MustCompile(`^-?[0-9]*\.?[0-9]+(px|em|rem)?$`) |
| 1206 | read := func(block []declaration) map[string]string { |
| 1207 | out := map[string]string{} |
| 1208 | // Every declaration is consumed by the numeric grammar, or the |
| 1209 | // block is rejected: a stray `color: red;` must not pass because |
| 1210 | // the reader only looked for --rst- names. |
| 1211 | for _, d := range block { |
| 1212 | v := fullValue(d) |
| 1213 | if !strings.HasPrefix(d.Property, "--rst-") { |
| 1214 | bad("has %q in a scheme block; only --rst-* numbers may be restated per scheme", d.Property+": "+v) |
| 1215 | continue |
| 1216 | } |
| 1217 | if !num.MatchString(v) { |
| 1218 | bad("restates %s as %q in a scheme block; only numbers may be restated per scheme", d.Property, v) |
| 1219 | } |
| 1220 | if rv, ok := root[d.Property]; !ok { |
| 1221 | bad("restates %s for dark but never declares it in :root", d.Property) |
| 1222 | } else if !num.MatchString(rv) { |
| 1223 | bad("declares %s as %q in :root; a number restated per scheme must be a number there too", d.Property, rv) |
| 1224 | } |
| 1225 | out[d.Property] = v |
| 1226 | } |
| 1227 | return out |
| 1228 | } |
| 1229 | a, m := read(attr), read(media) |
| 1230 | if !reflect.DeepEqual(a, m) { |
| 1231 | bad("two dark-number blocks differ: attribute %v, media %v", a, m) |
| 1232 | } |
| 1233 | return a, problems |
| 1234 | } |
| 1235 | |
| 1236 | // TestSchemeNumberGateRejectsWhatItMustReject feeds the gate the shapes |
| 1237 | // it exists to refuse, since a gate that only ever sees the shipped |
| 1238 | // themes is not known to refuse anything. |
| 1239 | func TestSchemeNumberGateRejectsWhatItMustReject(t *testing.T) { |
| 1240 | const good = ":root {\n --rst-bg: light-dark(#fff, #000);\n --rst-press-wash: 0.08;\n}\n:root[data-theme=\"light\"] { color-scheme: light; }\n:root[data-theme=\"dark\"] { color-scheme: dark; }\n:root[data-theme=\"dark\"] { --rst-press-wash: 0.18; }\n@media (prefers-color-scheme: dark) { :root:not([data-theme=\"light\"]) { --rst-press-wash: 0.18; } }\n" |
| 1241 | if numbers, problems := schemeNumberProblems("good", good); len(problems) != 0 || numbers["--rst-press-wash"] != "0.18" { |
| 1242 | t.Fatalf("the well-formed theme was refused: %v (numbers %v)", problems, numbers) |
| 1243 | } |
| 1244 | withSupports := good + "@supports (color: oklch(from red l c h)) {\n :root {\n --rst-bg: light-dark(oklch(from #fff 0.5 c h), #000);\n }\n}\n" |
| 1245 | if _, problems := schemeNumberProblems("good", withSupports); len(problems) != 0 { |
| 1246 | t.Fatalf("a relative-colour block restating a :root name was refused: %v", problems) |
| 1247 | } |
| 1248 | for name, css := range map[string]string{ |
| 1249 | "a colour in a block": strings.ReplaceAll(good, "--rst-press-wash: 0.18;", "--rst-press-wash: 0.18; color: red;"), |
| 1250 | "a colour restated": strings.ReplaceAll(good, "--rst-press-wash: 0.18;", "--rst-bg: #111;"), |
| 1251 | "only one block": strings.Replace(good, "@media (prefers-color-scheme: dark) { :root:not([data-theme=\"light\"]) { --rst-press-wash: 0.18; } }\n", "", 1), |
| 1252 | "blocks that differ": strings.Replace(good, ":root[data-theme=\"dark\"] { --rst-press-wash: 0.18; }", ":root[data-theme=\"dark\"] { --rst-press-wash: 0.30; }", 1), |
| 1253 | "a number not in root": strings.ReplaceAll(good, "--rst-press-wash: 0.18;", "--rst-press-spot: 0.3;"), |
| 1254 | "a root default not a number": strings.Replace(good, "--rst-press-wash: 0.08;", "--rst-press-wash: calc(1 - 0.92);", 1), |
| 1255 | "an extra rule": good + ".rst-btn { color: red; }\n", |
| 1256 | "color-scheme in a block": strings.ReplaceAll(good, "--rst-press-wash: 0.18;", "--rst-press-wash: 0.18; color-scheme: dark;"), |
| 1257 | "a supports block with a new name": good + "@supports (color: oklch(from red l c h)) {\n :root {\n --rst-new: #fff;\n }\n}\n", |
| 1258 | "a supports block with a rule": good + "@supports (color: oklch(from red l c h)) {\n :root {\n --rst-bg: #fff; color: red;\n }\n}\n", |
| 1259 | "a supports block for another feature": good + "@supports (color: oklch(0.5 0.1 20)) {\n :root {\n --rst-bg: #fff;\n }\n}\n", |
| 1260 | // The real rule gone, and its text quoted in a string: a |
| 1261 | // browser applies neither, so Dark has no color-scheme, or a |
| 1262 | // reader who chose Dark gets the light numbers. |
| 1263 | "a toggle only in a string": strings.Replace(strings.Replace(good, ":root[data-theme=\"dark\"] { color-scheme: dark; }\n", "", 1), |
| 1264 | "--rst-press-wash: 0.08;", "--rst-press-wash: 0.08; --rst-note: ':root[data-theme=\"dark\"] { color-scheme: dark; }';", 1), |
| 1265 | "a number block only in a string": strings.Replace(strings.Replace(good, ":root[data-theme=\"dark\"] { --rst-press-wash: 0.18; }\n", "", 1), |
| 1266 | "--rst-press-wash: 0.08;", "--rst-press-wash: 0.08; --rst-note: ':root[data-theme=\"dark\"] { --rst-press-wash: 0.18; }';", 1), |
| 1267 | } { |
| 1268 | if _, problems := schemeNumberProblems("bad", css); len(problems) == 0 { |
| 1269 | t.Errorf("%s was accepted", name) |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | func TestTokensCSSHasNoColourLiterals(t *testing.T) { |
| 1275 | // After the split, structural tokens.css may reference colours only |
| 1276 | // via var(); bare hex in a *declaration value* means a colour leaked |
| 1277 | // back in. Exempt: none — the three rgba() literals that used to live |
| 1278 | // here (the switch knob shadow, the seg-tab lift shadow, the modal |
| 1279 | // scrim) are gone too, replaced with var(--rst-shadow-knob), |
| 1280 | // var(--rst-shadow-lift) and var(--rst-overlay) declared per theme — |
| 1281 | // so this gate also matches rgba()/rgb()/hsl() function values, not |
| 1282 | // just bare hex, to keep that claim enforced instead of just stated. |
| 1283 | literal := regexp.MustCompile(`#[0-9a-fA-F]{3,6}|rgba?\(|hsl\(`) |
| 1284 | // Ink and light: tokens.css may paint pure black or pure white |
| 1285 | // at an alpha, where the strength is a number token or a fixed |
| 1286 | // fraction. Those two spellings are the only literals allowed; a |
| 1287 | // value that mixes one with any other colour function still fails. |
| 1288 | achromatic := regexp.MustCompile(`rgb\((0 0 0|255 255 255) / `) |
| 1289 | // Every declaration value, read with the declaration reader: a |
| 1290 | // value written over two lines was missed by a line-by-line read. |
| 1291 | decls, _ := parseDeclarations(t, "tokens.css", string(fiesta.TokensCSS())) |
| 1292 | for _, d := range decls { |
| 1293 | if literal.MatchString(achromatic.ReplaceAllString(d.Value, "ink(")) { |
| 1294 | t.Errorf("tokens.css declares a colour literal: %s", d) |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // The same drift check the other partial families get: every class the |
| 1300 | // error page emits has a rule in tokens.css. |
| 1301 | func TestErrorPageClassesAreStyled(t *testing.T) { |
| 1302 | attributes := styledAttributes(t, string(fiesta.TokensCSS())) |
| 1303 | for _, name := range []string{ |
| 1304 | "rst-error", "rst-error-status", "rst-error-title", |
| 1305 | "rst-error-body", "rst-error-cta", "rst-error-ref", |
| 1306 | } { |
| 1307 | if !tokensStyle(attributes, name) { |
| 1308 | t.Errorf("tokens.css has no selector for %q", name) |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | // A button's own display rule would otherwise beat the user agent's |
| 1314 | // [hidden] rule, and the passkey door would show before its script |
| 1315 | // decided it can work. |
| 1316 | func TestHiddenButtonsStayHidden(t *testing.T) { |
| 1317 | for _, r := range leafRules(t, string(fiesta.TokensCSS())) { |
| 1318 | if r.context == "" && r.selector == ".rst-btn[hidden], [rst-btn][hidden]" && r.sets("display", "none") { |
| 1319 | return |
| 1320 | } |
| 1321 | } |
| 1322 | t.Fatal("tokens.css does not keep a [hidden] button hidden") |
| 1323 | } |
| 1324 | |
| 1325 | // The depth axis is a theme decision: day turns it on, and plain and |
| 1326 | // signal prove a theme can switch it off (spec §3). Flat means the |
| 1327 | // highlight, the shadows, the sheen and the wash are transparent, the |
| 1328 | // face does not shade, the press has no strength and no drop, and the |
| 1329 | // rim is the line: declared as var(--rst-line), so an app's override of |
| 1330 | // the line reaches it, or as the line's own value. Pinned by |
| 1331 | // value for day because its numbers were measured; pinned flat |
| 1332 | // for the others because "renders as today" is a promise a later theme |
| 1333 | // edit could quietly break. |
| 1334 | func TestDayTurnsDepthOnAndTheOthersStayFlat(t *testing.T) { |
| 1335 | for _, name := range fiesta.ThemeNames() { |
| 1336 | for _, scheme := range []string{"light", "dark"} { |
| 1337 | tok := themeTokens(t, name)[scheme] |
| 1338 | rimIsLine := tok["--rst-light-rim"] == "var(--rst-line)" || tok["--rst-light-rim"] == tok["--rst-line"] |
| 1339 | flat := tok["--rst-depth-hl"] == "transparent" && tok["--rst-depth-contact"] == "transparent" && |
| 1340 | tok["--rst-depth-ambient"] == "transparent" && tok["--rst-depth-face"] == "0" && tok["--rst-press-drop"] == "0px" && |
| 1341 | tok["--rst-sheen-fill"] == "transparent" && tok["--rst-sheen-plain"] == "transparent" && |
| 1342 | tok["--rst-light-wash"] == "transparent" && rimIsLine && |
| 1343 | tok["--rst-press-wash"] == "0" && tok["--rst-press-spot"] == "0" |
| 1344 | if name == "day" { |
| 1345 | if flat { |
| 1346 | t.Errorf("day/%s is flat; the depth axis is day's to turn on", scheme) |
| 1347 | } |
| 1348 | if tok["--rst-depth-face"] != "0.04" || tok["--rst-press-drop"] != "1px" { |
| 1349 | t.Errorf("day/%s shades a filled face by %q and drops a press by %q, want 0.04 and 1px", scheme, tok["--rst-depth-face"], tok["--rst-press-drop"]) |
| 1350 | } |
| 1351 | continue |
| 1352 | } |
| 1353 | if !flat { |
| 1354 | t.Errorf("%s/%s is not flat (plain and signal render as they did)", name, scheme) |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | day := themeTokens(t, "day") |
| 1359 | for scheme, want := range map[string]map[string]string{ |
| 1360 | "light": {"--rst-accent": "#1f56d6", "--rst-danger-fill": "#c62f2f", "--rst-track-off": "#8a8a95", "--rst-radius": "6px", "--rst-radius-sm": "4px", "--rst-header-rule": "var(--rst-line)"}, |
| 1361 | "dark": {"--rst-accent": "#7aa2ff", "--rst-danger-fill": "#c43232", "--rst-track-off": "#71737c", "--rst-radius": "6px", "--rst-radius-sm": "4px", "--rst-header-rule": "var(--rst-line)"}, |
| 1362 | } { |
| 1363 | for tokName, v := range want { |
| 1364 | if day[scheme][tokName] != v { |
| 1365 | t.Errorf("day/%s %s = %q, want %q", scheme, tokName, day[scheme][tokName], v) |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | // The fill is derived from the accent (derived_test.go), so it is pinned |
| 1370 | // resolved: the accent's hue at day's fill lightness, within two |
| 1371 | // units of the #2257df and #2f61e8 the design was measured with. |
| 1372 | resolved := resolvedTokens(t, "day") |
| 1373 | for scheme, want := range map[string]string{"light": "#2059dd", "dark": "#3160ea"} { |
| 1374 | if got := resolved[scheme]["--rst-accent-fill"]; got != want { |
| 1375 | t.Errorf("day/%s --rst-accent-fill resolves to %q, want %q (derived)", scheme, got, want) |
| 1376 | } |
| 1377 | } |
| 1378 | css, _ := fiesta.ThemeCSS("day") |
| 1379 | if numbers := schemeNumberBlocks(t, "day", string(css)); numbers["--rst-press-wash"] != "0.18" { |
| 1380 | t.Errorf("day restates no dark press strengths; want --rst-press-wash: 0.18 in both scheme blocks") |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | // Hidden where a picture of waves would do harm: forced colours paint |
| 1385 | // every stroke in the system's text colour at full strength, and a |
| 1386 | // printout does not need it. The stylesheet is the only place this can |
| 1387 | // happen, because the art carries no style of its own. |
| 1388 | func TestStageArtIsHiddenUnderForcedColoursAndInPrint(t *testing.T) { |
| 1389 | rules := leafRules(t, string(fiesta.TokensCSS())) |
| 1390 | for _, media := range []string{"@media (forced-colors: active)", "@media print"} { |
| 1391 | if !slices.ContainsFunc(rules, func(r leafRule) bool { |
| 1392 | return r.context == media && r.selector == ".rst-stage-art, [rst-stage-art]" && r.sets("display", "none") |
| 1393 | }) { |
| 1394 | t.Errorf("tokens.css does not hide [rst-stage-art] under %s", media) |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | |