oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package chrome
2
3import (
4 "html/template"
5 "regexp"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/estilo.git/menu"
10)
11
12// What Docs puts in the compact slots on a phone: its Aa and ••• at the
13// inline end, and its inline title (shown once its large title has
14// scrolled away). Markup it rendered itself.
15const (
16 testTrail = template.HTML(`<button type="button" estilo-bar-tool aria-label="Format">Aa</button><details name="estilo-menus"><summary estilo-bar-tool aria-label="More"></summary><div estilo-menu></div></details>`)
17 testTitle = template.HTML(`<b>x-inline-title</b>`)
18)
19
20func compact() Bar {
21 b := stacked()
22 b.Lead = testLead
23 b.Compact = &Compact{Trail: testTrail}
24 b.Nav = nil // Docs has none; a compact bar with nav keeps a sheet for it (TestACompactBarWithNavKeepsItsSheet)
25 return b
26}
27
28// narrowQuery is where a compact bar is the phone's one row: below 704px,
29// or a phone held sideways. bandQuery is where a compact bar, and only a
30// compact bar, is already the 56px desktop header that every other
31// stacked bar starts at 800px (Paul, 2026-11-28: the desktop header from
32// 704px for an iPad in portrait; Fable on c56314e: Mapa and Fichas stack
33// too, and their own layouts break at 800px). In px, as the 800px
34// breakpoint is, so the two never part at a reader's larger default font.
35// (bandQuery is chrome.go's.)
36const narrowQuery = "@media (max-width: 703.98px), (max-height: 480px) and (pointer: coarse)"
37
38// The Compact slot marks the bar compact and adds only what a compact bar
39// needs: the Back chevron in the brand, the app's trail in the end group
40// before the switcher and the account, and no Menu sheet, since the trail
41// carries the app's own overflow.
42func TestTheCompactSlotMarksTheBar(t *testing.T) {
43 b := compact()
44 html := render(t, "estilo-bar", b)
45 if got := header(html); got != `<header estilo-bar="stacked compact">` {
46 t.Errorf("a compact stacked bar opens %s", got)
47 }
48 b.Sticky = true
49 if got := header(render(t, "estilo-bar", b)); got != `<header estilo-bar="sticky stacked compact">` {
50 t.Errorf("a sticky compact stacked bar opens %s", got)
51 }
52 plain := signedIn("x")
53 plain.Compact = &Compact{Trail: testTrail}
54 if got := header(render(t, "estilo-bar", plain)); got != `<header estilo-bar="compact">` {
55 t.Errorf("a compact one-row bar opens %s", got)
56 }
57
58 brand := html[strings.Index(html, "<a estilo-bar-brand"):]
59 brand = brand[:strings.Index(brand, "</a>")]
60 if !strings.Contains(brand, `<svg estilo-bar-back `) || !strings.Contains(brand, `aria-hidden="true"`) {
61 t.Errorf("the compact brand has no hidden Back chevron:\n%s", brand)
62 }
63 if !strings.Contains(brand, "<span estilo-bar-name>x-app</span>") {
64 t.Errorf("the Back link lost the app's name, its label:\n%s", brand)
65 }
66
67 if strings.Contains(html, "estilo-bar-more") {
68 t.Error("a compact bar drew the Menu sheet; its trail is the overflow")
69 }
70 if !strings.Contains(html, "<div estilo-bar-trail>"+string(testTrail)+"</div>") {
71 t.Errorf("the trail is not drawn verbatim:\n%s", html)
72 }
73 order := []string{"<div estilo-bar-lead>", "<div estilo-bar-actions>", "<div estilo-bar-trail>", "<details estilo-switcher", "<details estilo-account"}
74 last := -1
75 for _, marker := range order {
76 i := strings.Index(html, marker)
77 if i < 0 || i < last {
78 t.Fatalf("%q is missing or out of order", marker)
79 }
80 last = i
81 }
82 if strings.Contains(html, "estilo-bar-compact-title") {
83 t.Error("a compact bar with no Title drew the compact title slot")
84 }
85
86 // A Title replaces the bar's own title below 44rem: it is drawn after
87 // the brand and the stack, before the end group.
88 b.Compact.Title = testTitle
89 html = render(t, "estilo-bar", b)
90 at := strings.Index(html, "<div estilo-bar-compact-title>"+string(testTitle)+"</div>")
91 if at < 0 || at < strings.Index(html, "<div estilo-bar-stack>") || at > strings.Index(html, "<div estilo-bar-end>") {
92 t.Errorf("the compact title is not between the stack and the end group:\n%s", html)
93 }
94
95 // A public page's brand is plain text: no Back.
96 pub := compact()
97 pub.App.Home = ""
98 if strings.Contains(render(t, "estilo-bar", pub), "estilo-bar-back") {
99 t.Error("a brand that links nowhere drew a Back chevron")
100 }
101}
102
103// Every rule a compact bar adds is keyed on a compact hook, and the
104// hooks are in the opt-in list (TestEverythingAfterTheMarkerIsOptIn).
105func TestTheCompactHooksAreOptIn(t *testing.T) {
106 for _, h := range []string{`[estilo-bar~="compact"]`, "[estilo-bar-trail]", "[estilo-bar-back]", "[estilo-bar-compact-title]", "[estilo-bar-tool]"} {
107 found := false
108 for _, o := range optInHooks {
109 found = found || o == h
110 }
111 if !found {
112 t.Errorf("%s is not an opt-in hook", h)
113 }
114 }
115}
116
117// Below 44rem a compact bar is the phone's one 52px row: Back, the
118// title, the lead and the trail. The menus, the actions, the switcher and
119// the account go (the trail and the app's other screens carry them), and
120// the published block size is the row's.
121func TestTheCompactBarIsOneRowBelow704px(t *testing.T) {
122 src := string(CSS())
123 src = src[strings.Index(src, barStackMarker):]
124 at := strings.Index(src, "/* ── Compact")
125 if at < 0 {
126 t.Fatal("chrome.css has no compact section")
127 }
128 section := src[at:]
129 narrow := section[strings.Index(section, narrowQuery):]
130 narrow = narrow[:strings.Index(narrow, "\n}\n")]
131 for _, want := range []string{
132 `:root:has([estilo-bar~="compact"]) { --estilo-bar-block-size: 3.25rem; }`,
133 `[estilo-bar~="compact"] [estilo-bar-menubar],`,
134 `[estilo-bar~="compact"] [estilo-bar-actions],`,
135 `[estilo-bar~="compact"] [estilo-switcher],`,
136 `[estilo-bar~="compact"] [estilo-account] { display: none; }`,
137 `[estilo-bar~="compact"] [estilo-bar-trail] { display: flex;`,
138 `[estilo-bar~="compact"] [estilo-bar-compact-title] { display: block;`,
139 `[estilo-bar~="compact"] [estilo-bar-back] { display: block;`,
140 } {
141 if !strings.Contains(narrow, want) {
142 t.Errorf("the compact narrow block lacks %s", want)
143 }
144 }
145 // The trail's panel is a drop-down from the inline end (Paul's answer
146 // 3), sized for a finger: 44px rows, no shortcut hints.
147 for _, re := range []string{
148 `\[estilo-bar-trail\] \[estilo-menu\] \{[^}]*position: absolute;[^}]*inset-inline-end: var\(--rst-sp-2\);`,
149 `\[estilo-bar-trail\] \[estilo-menu-item\] \{[^}]*min-block-size: 44px;`,
150 `\[estilo-bar-trail\] \[estilo-menu-key\] \{ display: none; \}`,
151 `\[estilo-bar-tool\] \{[^}]*min-inline-size: 44px;[^}]*block-size: 44px;`,
152 } {
153 if !regexp.MustCompile(re).MatchString(section) {
154 t.Errorf("the compact section lacks %s", re)
155 }
156 }
157 // The editable title fills its room and ends in an ellipsis there,
158 // instead of being cut mid-letter (audit finding 2).
159 if !regexp.MustCompile(`\[estilo-bar~="compact"\] \[estilo-bar-title-input\] \{[^}]*inline-size: 100%;[^}]*text-overflow: ellipsis;`).MatchString(narrow) {
160 t.Error("the compact title field does not fill its room with an ellipsis")
161 }
162 // From 44rem the trail and the compact title are gone.
163 if !regexp.MustCompile(`\n:is\(\[estilo-bar-trail\], \[estilo-bar-compact-title\], \[estilo-bar-back\]\) \{ display: none; \}`).MatchString(section) {
164 t.Error("the trail, compact title and Back are not hidden by default")
165 }
166}
167
168// chrome.js leaves a compact title unfitted where the compact bar is
169// narrow, on the CSS's own query, and publishes a stacked bar's narrow
170// height below 800px, as it always has.
171func TestTheScriptUsesTheCSSBreakpoints(t *testing.T) {
172 js := string(JS())
173 q := strings.TrimPrefix(narrowQuery, "@media ")
174 if !strings.Contains(js, "matchMedia('"+q+"')") {
175 t.Errorf("chrome.js does not read the compact query %q", q)
176 }
177 if !strings.Contains(js, "matchMedia('(max-width: 799.98px)')") {
178 t.Error("chrome.js no longer publishes the stacked bar's height below 800px")
179 }
180 if !strings.Contains(js, `(bar) => (' ' + bar.getAttribute('estilo-bar') + ' ').indexOf(' compact ') < 0)`) {
181 t.Error("chrome.js publishes a compact bar's measured height, which holds it at 52px past 704px")
182 }
183 css := string(CSS())
184 if !strings.Contains(css, narrowQuery+" {") || !strings.Contains(css, bandQuery+" {") {
185 t.Error("chrome.css does not use the compact pair")
186 }
187 if strings.Contains(css, "43.99rem") || strings.Contains(css, "(min-width: 44rem)") {
188 t.Error("a rem breakpoint is left; at a larger default font it parts from 800px")
189 }
190}
191
192// Touch: every bar trigger is a 44px target on a coarse pointer, in every
193// app (audit finding 9). The stacked header keeps its 32px look and takes
194// 44px of hit area.
195func TestTouchTargetsAre44px(t *testing.T) {
196 src := string(CSS())
197 main := src[:strings.Index(src, barStackMarker)]
198 if !regexp.MustCompile(`@media \(pointer: coarse\) \{\s*:root \{ --estilo-trigger-size: 2\.75rem; \}`).MatchString(main) {
199 t.Error("the bar's triggers are not 44px on a coarse pointer")
200 }
201 stack := src[strings.Index(src, barStackMarker):]
202 wide := stack[strings.Index(stack, "@media (min-width: 800px) {"):]
203 coarse := wide[strings.Index(wide, "@media (pointer: coarse) {"):]
204 coarse = coarse[:strings.Index(coarse, "\n }\n")]
205 for _, want := range []string{
206 `[estilo-bar~="stacked"] [estilo-menubar-title]::before { content: ""; position: absolute; inset-inline: 0; inset-block: -16px -4px; }`,
207 `[estilo-bar~="stacked"] [estilo-menu-item] { min-block-size: 44px; }`,
208 `:is([estilo-bar-control] > a, [estilo-bar-control] > button, [estilo-bar-control] > form > button)::before { content: ""; position: absolute; inset: -6px -4px; }`,
209 } {
210 if !strings.Contains(coarse, want) {
211 t.Errorf("the stacked header's coarse rules lack %s", want)
212 }
213 }
214}
215
216// Only a compact bar moves to 704px. Every other stacked bar (Mapa and
217// Fichas set Menubar, Lead and Nav, and their layouts break at 800px)
218// keeps main's rules: the 56px header from 800px, the two-row bar and its
219// Menu sheet below. Between 704px and 800px a compact bar gets the very
220// same 56px header, a copy of that block CSS() makes at init with every
221// selector scoped to a compact bar, so the two can never drift.
222func TestOnlyACompactBarMovesTo704px(t *testing.T) {
223 src := string(CSS())
224 at := strings.Index(src, bandQuery+" {")
225 if at < 0 {
226 t.Fatal("no 704px band for a compact bar")
227 }
228 if strings.Contains(string(css), `[estilo-bar~="stacked"][estilo-bar~="compact"]) { --estilo-bar-block-size: 56px; }`) {
229 t.Error("the band's copy is written by hand in chrome.css; it must be the generated one")
230 }
231 wide := regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(wideBlock(string(css)), "")
232 band := block(src, at+len(bandQuery)+2)
233 rules := func(block string) []string {
234 var out []string
235 for _, m := range regexp.MustCompile(`([^{};]+)\{`).FindAllStringSubmatch(block, -1) {
236 if l := strings.TrimSpace(m[1]); !strings.HasPrefix(l, "@") {
237 out = append(out, l)
238 }
239 }
240 return out
241 }
242 w, b := rules(wide), rules(band)
243 if len(w) < 40 || len(w) != len(b) {
244 t.Fatalf("the band has %d rules for the wide block's %d", len(b), len(w))
245 }
246 for i, list := range b {
247 for _, sel := range splitSelectors(list) {
248 if !strings.Contains(sel, `[estilo-bar~="compact"]`) {
249 t.Errorf("band rule %d, %q, reaches a bar that is not compact (from %q)", i, sel, w[i])
250 }
251 }
252 }
253 // The declarations are the wide block's, verbatim.
254 decls := func(block string) string {
255 return regexp.MustCompile(`[^{};]+\{`).ReplaceAllString(block, "{")
256 }
257 if strings.Join(strings.Fields(decls(wide)), " ") != strings.Join(strings.Fields(decls(band)), " ") {
258 t.Error("the band's declarations are not the wide block's")
259 }
260 // The scope adds no specificity (:where), so the cascade around the
261 // copy is the original's; a pseudo-element stays outside :is().
262 if strings.Contains(band, `):is([estilo-bar~="compact"]`) {
263 t.Error("the band's scope adds specificity; the forced-colors border would lose to the controls' border: 0")
264 }
265 if !strings.Contains(band, `:is([estilo-bar~="stacked"] [estilo-menubar-title]):where([estilo-bar~="compact"], [estilo-bar~="compact"] *)::before`) {
266 t.Error("the band does not keep ::before outside :is()")
267 }
268 if !strings.Contains(band, `:root:has([estilo-bar~="stacked"][estilo-bar~="compact"]) { --estilo-bar-block-size: 56px; }`) {
269 t.Error("the band does not publish the compact bar's 56px")
270 }
271 // Nothing written by hand for the band reaches a bar without Compact.
272 hand := src[strings.Index(src, "/* Between 704px and 800px"):]
273 hand = regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(block(hand, strings.Index(hand, "{")+1), "")
274 for _, list := range rules(hand) {
275 for _, sel := range splitSelectors(list) {
276 if !strings.Contains(sel, `[estilo-bar~="compact"]`) {
277 t.Errorf("%q, in the band, reaches a bar that is not compact", sel)
278 }
279 }
280 }
281 // The Rename pill stays in the title row in the band, where the narrow
282 // rule would float it under the title.
283 pill := regexp.MustCompile(`\[estilo-bar~="stacked"\] \[estilo-bar-title\]:focus-within \[estilo-bar-title-submit\] \{[^}]*\}`).FindString(wide)
284 if !strings.Contains(pill, "position: static;") {
285 t.Errorf("the wide Rename pill can still float under the title:\n%s", pill)
286 }
287}
288
289// A compact bar with Nav draws the Menu sheet (with the nav and the
290// actions) and shows it in the compact row, so the nav is never
291// unreachable; without Nav the trail is the only overflow.
292func TestACompactBarWithNavKeepsItsSheet(t *testing.T) {
293 b := compact()
294 b.Nav = []Link{{Href: "/places", Label: "x-place"}}
295 html := render(t, "estilo-bar", b)
296 sheet := html[strings.Index(html, "<details estilo-bar-more"):]
297 sheet = sheet[:strings.Index(sheet, "</details>")]
298 if !strings.Contains(sheet, "x-place") || !strings.Contains(sheet, "x-share") {
299 t.Errorf("a compact bar with nav has no sheet holding it:\n%s", html)
300 }
301 if strings.Index(html, "<details estilo-bar-more") > strings.Index(html, "<div estilo-bar-trail>") {
302 t.Error("the sheet is not before the trail")
303 }
304 b.Nav = nil
305 if strings.Contains(render(t, "estilo-bar", b), "estilo-bar-more") {
306 t.Error("a compact bar without nav drew the sheet")
307 }
308 src := string(CSS())
309 narrow := src[strings.Index(src, "/* ── Compact"):]
310 narrow = narrow[strings.Index(narrow, narrowQuery):]
311 if !strings.Contains(narrow[:strings.Index(narrow, "\n}\n")], `[estilo-bar~="compact"] [estilo-bar-more] { display: block; }`) {
312 t.Error("the compact row does not show a compact bar's sheet")
313 }
314}
315
316// Folded, Back is its chevron alone, still a 44px target (Fable, item 2).
317func TestFoldedBackIsA44pxTarget(t *testing.T) {
318 src := string(CSS())
319 src = src[strings.Index(src, "/* ── Compact"):]
320 if !regexp.MustCompile(`\[estilo-bar~="compact"\]\[estilo-bar-folded\]:has\(> \[estilo-bar-compact-title\]\) \[estilo-bar-brand\] \{[^}]*min-inline-size: 44px;[^}]*justify-content: center;`).MatchString(src) {
321 t.Error("folded Back is not a centred 44px target")
322 }
323}
324
325// A public page's brand is plain text, not a link: it is not drawn in the
326// link's accent (Fable, item 4).
327func TestOnlyALinkedBrandTakesTheAccent(t *testing.T) {
328 src := string(CSS())
329 src = src[strings.Index(src, "/* ── Compact"):]
330 for _, m := range regexp.MustCompile(`([^{};]+)\{([^}]*)\}`).FindAllStringSubmatch(src, -1) {
331 if !strings.Contains(m[2], "var(--rst-accent)") || !strings.Contains(m[1], "[estilo-bar-brand]") {
332 continue
333 }
334 for _, sel := range splitSelectors(strings.TrimSpace(m[1])) {
335 if strings.HasSuffix(sel, "[estilo-bar-brand]") && !strings.HasSuffix(sel, "a[estilo-bar-brand]") {
336 t.Errorf("%q draws a plain-text brand in the accent", sel)
337 }
338 }
339 }
340}
341
342// On a coarse pointer the rail's account row and every menubar title
343// outside the wide stacked header are 44px too (Fable, item 5), and the
344// narrow stacked bar publishes the taller menu row.
345func TestTheRemainingTouchTargetsAre44px(t *testing.T) {
346 src := string(CSS())
347 main := src[:strings.Index(src, barStackMarker)]
348 if !regexp.MustCompile(`@media \(pointer: coarse\) \{[^}]*\[estilo-account~="rail"\] > \[estilo-bar-trigger\] \{ min-block-size: 2\.75rem; \}`).MatchString(main) {
349 t.Error("the rail's account row is not 44px on touch")
350 }
351 if !regexp.MustCompile(`@media \(pointer: coarse\) \{[^@]*\[estilo-menubar-title\] \{ min-block-size: 2\.75rem; \}`).MatchString(string(menu.CSS())) {
352 t.Error("menubar titles are not 44px on touch")
353 }
354 if !strings.Contains(src, "@media (max-width: 799.98px) and (pointer: coarse) {\n :root:has([estilo-bar~=\"stacked\"]) { --estilo-bar-block-size: calc(3.25rem + 2.75rem + 1px); }") {
355 t.Error("the narrow stacked bar does not publish its 44px menu row on touch")
356 }
357}
358
359// A compact bar fits 360px with room for the title: Back, the lead and a
360// two-button trail are 300px in the mock-up, and the bar's own padding
361// and gaps are the small steps.
362func TestTheCompactBarUsesTheSmallSteps(t *testing.T) {
363 src := string(CSS())
364 src = src[strings.Index(src, "/* ── Compact"):]
365 if !regexp.MustCompile(`\[estilo-bar~="compact"\] \{[^}]*gap: var\(--rst-sp-1\);[^}]*padding-inline: var\(--rst-sp-2\);`).MatchString(src) {
366 t.Error("the compact bar is not laid out on the small steps")
367 }
368}
369
370// The menu's Icon is drawn in the compact trail's rows (and any menu that
371// passes one); a row without one is byte for byte what it was.
372func TestTheCompactTrailCanDrawIconRows(t *testing.T) {
373 got := render(t, "estilo-menu-item", menu.Item{Href: "/s", Label: "x-share", Icon: `<svg aria-hidden="true"></svg>`})
374 if got != `<a estilo-menu-item href="/s"><span estilo-menu-icon><svg aria-hidden="true"></svg></span><span estilo-menu-label>x-share</span></a>` {
375 t.Errorf("an icon row: %s", got)
376 }
377}
378
379// With Compact.Title the bar is the large-title idiom: the compact title
380// shows only once the app marks the bar folded, and then Back is its
381// chevron, its name visually hidden but still its accessible name.
382func TestACompactTitleShowsOnlyWhenFolded(t *testing.T) {
383 src := string(CSS())
384 src = src[strings.Index(src, "/* ── Compact"):]
385 for _, want := range []string{
386 `[estilo-bar~="compact"] [estilo-bar-compact-title] { opacity: 0; visibility: hidden;`,
387 `[estilo-bar~="compact"][estilo-bar-folded] [estilo-bar-compact-title] { opacity: 1; visibility: visible;`,
388 `[estilo-bar~="compact"][estilo-bar-folded]:has(> [estilo-bar-compact-title]) [estilo-bar-name] { position: absolute; clip-path: inset(50%);`,
389 `[estilo-bar~="compact"]:has(> [estilo-bar-compact-title]) { border-block-end-color: transparent; }`,
390 } {
391 if !strings.Contains(src, want) {
392 t.Errorf("the compact section lacks %s", want)
393 }
394 }
395 if strings.Contains(src, `[estilo-bar-folded]:has(> [estilo-bar-compact-title]) [estilo-bar-name] { display: none`) {
396 t.Error("folded, Back would lose its accessible name")
397 }
398}
399
400// block is the body of the block whose opening brace is just before
401// start, up to its matching closing brace.
402func block(src string, start int) string {
403 depth := 1
404 for i := start; i < len(src); i++ {
405 switch src[i] {
406 case '{':
407 depth++
408 case '}':
409 if depth--; depth == 0 {
410 return src[start:i]
411 }
412 }
413 }
414 return src[start:]
415}
416