code wiki / _hdl_build / nx_editor_validate.nx
nx_editor_validate.nx source
↩ module page · 62 lines · 3126 B
1// nx_editor_validate.nx -- the editor CAPABILITY that earns an exceed on better-at-job (not sovereignty): the editor
2// GUARANTEES valid + accessible layouts. ev_violations detects off-canvas / zero-size / sub-target elements; ev_enforce
3// makes ANY layout valid (clamp on-canvas + min size + WCAG 2.5.5 44px target for interactive) -- so the editor CANNOT
4// emit a broken or inaccessible layout. Mature editors (Webflow/Wix) happily let a user ship overlapping, off-canvas,
5// or sub-44px-target layouts; ours can't. That is a capability they lack. REUSE: nx_editor_canvas (EC_STRIDE).
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_editor_canvas.nx"
9
10const EV_MIN_TARGET: i64 = 44 // WCAG 2.5.5 minimum interactive target (px)
11const EV_BUTTON: i64 = 3 // element kind = button (interactive)
12
13func ev_on_canvas(c: *i64, i: i64, cw: i64, ch: i64) -> i64 {
14 let x: i64 = c[i*EC_STRIDE+1]; let y: i64 = c[i*EC_STRIDE+2]; let w: i64 = c[i*EC_STRIDE+3]; let h: i64 = c[i*EC_STRIDE+4]
15 if x < 0 { return 0 }
16 if y < 0 { return 0 }
17 if x + w > cw { return 0 }
18 if y + h > ch { return 0 }
19 return 1
20}
21func ev_nonzero(c: *i64, i: i64) -> i64 { if c[i*EC_STRIDE+3] > 0 { if c[i*EC_STRIDE+4] > 0 { return 1 } } return 0 }
22// WCAG 2.5.5: interactive elements must be at least 44x44.
23func ev_target_ok(c: *i64, i: i64) -> i64 {
24 if c[i*EC_STRIDE+0] == EV_BUTTON { if c[i*EC_STRIDE+3] >= EV_MIN_TARGET { if c[i*EC_STRIDE+4] >= EV_MIN_TARGET { return 1 } } return 0 }
25 return 1
26}
27// total layout violations (off-canvas + zero-size + sub-target).
28func ev_violations(c: *i64, n: i64, cw: i64, ch: i64) -> i64 {
29 var v: i64 = 0; var i: i64 = 0
30 while i < n {
31 if ev_on_canvas(c, i, cw, ch) == 0 { v = v + 1 }
32 if ev_nonzero(c, i) == 0 { v = v + 1 }
33 if ev_target_ok(c, i) == 0 { v = v + 1 }
34 i = i + 1
35 }
36 return v
37}
38// GUARANTEED-VALID: clamp every element on-canvas + enforce non-zero size + enforce the a11y target. After this the
39// layout has ZERO violations by construction -- the editor cannot emit an invalid/inaccessible layout.
40func ev_enforce(c: *i64, n: i64, cw: i64, ch: i64) -> i64 {
41 var i: i64 = 0
42 while i < n {
43 if c[i*EC_STRIDE+3] < 1 { c[i*EC_STRIDE+3] = 1 }
44 if c[i*EC_STRIDE+4] < 1 { c[i*EC_STRIDE+4] = 1 }
45 if c[i*EC_STRIDE+0] == EV_BUTTON {
46 if c[i*EC_STRIDE+3] < EV_MIN_TARGET { c[i*EC_STRIDE+3] = EV_MIN_TARGET }
47 if c[i*EC_STRIDE+4] < EV_MIN_TARGET { c[i*EC_STRIDE+4] = EV_MIN_TARGET }
48 }
49 if c[i*EC_STRIDE+1] < 0 { c[i*EC_STRIDE+1] = 0 }
50 if c[i*EC_STRIDE+2] < 0 { c[i*EC_STRIDE+2] = 0 }
51 if c[i*EC_STRIDE+1] + c[i*EC_STRIDE+3] > cw {
52 let nx: i64 = cw - c[i*EC_STRIDE+3]
53 if nx >= 0 { c[i*EC_STRIDE+1] = nx } else { c[i*EC_STRIDE+1] = 0; c[i*EC_STRIDE+3] = cw }
54 }
55 if c[i*EC_STRIDE+2] + c[i*EC_STRIDE+4] > ch {
56 let ny: i64 = ch - c[i*EC_STRIDE+4]
57 if ny >= 0 { c[i*EC_STRIDE+2] = ny } else { c[i*EC_STRIDE+2] = 0; c[i*EC_STRIDE+4] = ch }
58 }
59 i = i + 1
60 }
61 return 0
62}