code wiki / _hdl_build / nx_css_vars_gate.nx

nx_css_vars_gate.nx source

↩ module page · 135 lines · 7337 B

1// nx_css_vars_gate.nx -- KATs for CSS custom-property (var()) resolution (nx_css_vars). Proves Nishi Browser's 2// engine can now resolve the fleet-standard --nx-color-* tokens NATIVELY: basic sub, fallback, indirection, 3// embedded-in-shorthand, real office-shape two-level, and the CRITICAL no-op (a page without var() must come 4// out byte-identical so every existing reader/browser/nishios gate stays green). license_tier: ORIGINAL 5// expect_exit: 0 6import "nx_css_vars.nx" 7 8func p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func g_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 10// substring present in out[0..olen)? 11func g_has(out: *u8, olen: i64, needle: *u8) -> i64 { 12 let nl: i64 = g_slen(needle) 13 if nl == 0 { return 0 } 14 var i: i64 = 0 15 while i + nl <= olen { 16 var k: i64 = 0 17 var hit: i64 = 1 18 while k < nl { if out[i + k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } } 19 if hit == 1 { return 1 } 20 i = i + 1 21 } 22 return 0 23} 24func g_ck(name: *u8, cond: i64, pass: *i64, fail: *i64) -> i64 { 25 if cond == 1 { p(" PASS " as *u8); p(name); p("\n" as *u8); pass[0] = pass[0] + 1; return 1 } 26 p(" FAIL " as *u8); p(name); p("\n" as *u8); fail[0] = fail[0] + 1 27 return 0 28} 29func g_run(css: *u8, out: *u8) -> i64 { 30 let n: i64 = g_slen(css) 31 let m: i64 = cx_expand(css, n, out, 262144) 32 out[m] = 0 as u8 33 return m 34} 35func main() -> i64 { 36 p("=== NX-CSS-VARS GATE (native var() resolution for Nishi Browser / reader / NishiOS) ===\n" as *u8) 37 let pass: *i64 = sys_mmap(16) as *i64 38 let fail: *i64 = sys_mmap(16) as *i64 39 pass[0] = 0 40 fail[0] = 0 41 let out: *u8 = sys_mmap(262144) 42 43 // T1 basic substitution 44 var m: i64 = g_run(":root{--a:rgb(1,2,3)}p{color:var(--a)}" as *u8, out) 45 var t1: i64 = 0 46 if g_has(out, m, "color:rgb(1,2,3)" as *u8) == 1 { if g_has(out, m, "var(" as *u8) == 0 { t1 = 1 } } 47 g_ck("T1 basic var(--a) -> rgb value, no var( left" as *u8, t1, pass, fail) 48 49 // T2 fallback for an undefined token 50 m = g_run("p{color:var(--missing,rgb(9,9,9))}" as *u8, out) 51 g_ck("T2 var(--missing,fallback) -> fallback" as *u8, g_has(out, m, "color:rgb(9,9,9)" as *u8), pass, fail) 52 53 // T3 one level of indirection (token defined as var of another) -- the office --bg:var(--nx-color-bg) shape 54 m = g_run(":root{--x:rgb(4,5,6);--y:var(--x)}p{color:var(--y)}" as *u8, out) 55 var t3: i64 = 0 56 if g_has(out, m, "color:rgb(4,5,6)" as *u8) == 1 { if g_has(out, m, "var(" as *u8) == 0 { t3 = 1 } } 57 g_ck("T3 indirection --y:var(--x) resolves through" as *u8, t3, pass, fail) 58 59 // T4 embedded in a shorthand value (border) with a nested rgb() -- paren balance 60 m = g_run(":root{--line:rgb(2,2,2)}a{border:1px solid var(--line)}" as *u8, out) 61 g_ck("T4 embedded var in shorthand -> border:1px solid rgb(2,2,2)" as *u8, g_has(out, m, "border:1px solid rgb(2,2,2)" as *u8), pass, fail) 62 63 // T5 real office two-level: canonical token + alias + usage 64 m = g_run(":root{--nx-color-accent:rgb(41,84,164);--acc:var(--nx-color-accent)}a{color:var(--acc)}" as *u8, out) 65 var t5: i64 = 0 66 if g_has(out, m, "color:rgb(41,84,164)" as *u8) == 1 { if g_has(out, m, "var(" as *u8) == 0 { t5 = 1 } } 67 g_ck("T5 office shape --acc:var(--nx-color-accent) -> real color" as *u8, t5, pass, fail) 68 69 // T6 CRITICAL no-op: a page with NO var() must come out byte-identical (existing gates depend on this) 70 let src6: *u8 = "h1{color:#2a2a33}p{margin-top:6px}a{color:#3366cc}" as *u8 71 m = g_run(src6, out) 72 var t6: i64 = 1 73 let l6: i64 = g_slen(src6) 74 if m != l6 { t6 = 0 } else { 75 var i: i64 = 0 76 while i < l6 { if out[i] != src6[i] { t6 = 0; i = l6 } else { i = i + 1 } } 77 } 78 g_ck("T6 no-op: page without var() is byte-identical" as *u8, t6, pass, fail) 79 80 // T7 fallback NOT used when the token IS defined 81 m = g_run(":root{--a:rgb(5,5,5)}p{color:var(--a,rgb(9,9,9))}" as *u8, out) 82 var t7: i64 = 0 83 if g_has(out, m, "color:rgb(5,5,5)" as *u8) == 1 { if g_has(out, m, "rgb(9,9,9)" as *u8) == 0 { t7 = 1 } } 84 g_ck("T7 defined token wins over its fallback" as *u8, t7, pass, fail) 85 86 // T8 multiple uses of the same token in one rule 87 m = g_run(":root{--c:rgb(7,8,9)}.x{color:var(--c);border-color:var(--c)}" as *u8, out) 88 var t8: i64 = 0 89 if g_has(out, m, "color:rgb(7,8,9)" as *u8) == 1 { if g_has(out, m, "border-color:rgb(7,8,9)" as *u8) == 1 { t8 = 1 } } 90 g_ck("T8 repeated token use both resolve" as *u8, t8, pass, fail) 91 92 // ---- clamp() resolution at a fixed viewport (vw=1000: 1vw=10px) ---- 93 // C1 the exact office body-type clamp: clamp(15px, 1vw + 12px, 17px) -> pref=10+12=22 -> clamp to max 17 94 var cm: i64 = clamp_resolve(":root{}body{font-size:clamp(15px,1vw + 12px,17px)}" as *u8, g_slen(":root{}body{font-size:clamp(15px,1vw + 12px,17px)}" as *u8), out, 262144, 1000) 95 out[cm] = 0 as u8 96 var c1: i64 = 0 97 if g_has(out, cm, "font-size:17px" as *u8) == 1 { if g_has(out, cm, "clamp(" as *u8) == 0 { c1 = 1 } } 98 g_ck("C1 clamp(15px,1vw+12px,17px)@1000 -> 17px (max-clamped)" as *u8, c1, pass, fail) 99 100 // C2 preferred falls WITHIN range: clamp(15px, 1vw + 5px, 30px) -> pref=10+5=15 -> 15px 101 cm = clamp_resolve("h1{font-size:clamp(15px,1vw + 5px,30px)}" as *u8, g_slen("h1{font-size:clamp(15px,1vw + 5px,30px)}" as *u8), out, 262144, 1000) 102 out[cm] = 0 as u8 103 g_ck("C2 clamp(15px,1vw+5px,30px)@1000 -> 15px (preferred in range)" as *u8, g_has(out, cm, "font-size:15px" as *u8), pass, fail) 104 105 // C3 preferred BELOW min: clamp(20px, 1vw, 40px) -> pref=10 -> min 20px 106 cm = clamp_resolve("h2{font-size:clamp(20px,1vw,40px)}" as *u8, g_slen("h2{font-size:clamp(20px,1vw,40px)}" as *u8), out, 262144, 1000) 107 out[cm] = 0 as u8 108 g_ck("C3 clamp(20px,1vw,40px)@1000 -> 20px (min-clamped)" as *u8, g_has(out, cm, "font-size:20px" as *u8), pass, fail) 109 110 // C4 decimal vw: clamp(20px, 2.6vw, 26px)@1000 -> 2.6vw=26 -> 26px (== max) 111 cm = clamp_resolve("h1{font-size:clamp(20px,2.6vw,26px)}" as *u8, g_slen("h1{font-size:clamp(20px,2.6vw,26px)}" as *u8), out, 262144, 1000) 112 out[cm] = 0 as u8 113 g_ck("C4 clamp(20px,2.6vw,26px)@1000 -> 26px (decimal vw)" as *u8, g_has(out, cm, "font-size:26px" as *u8), pass, fail) 114 115 // C5 no-op: a clamp-less sheet is byte-identical 116 let cs5: *u8 = "p{font-size:16px;color:#222}" as *u8 117 cm = clamp_resolve(cs5, g_slen(cs5), out, 262144, 1000) 118 var c5: i64 = 1 119 let l5: i64 = g_slen(cs5) 120 if cm != l5 { c5 = 0 } else { var ci5: i64 = 0; while ci5 < l5 { if out[ci5] != cs5[ci5] { c5 = 0; ci5 = l5 } else { ci5 = ci5 + 1 } } } 121 g_ck("C5 clamp-less sheet is byte-identical (no-op)" as *u8, c5, pass, fail) 122 123 p("NX-CSS-VARS GATE pass=" as *u8) 124 var pv: i64 = pass[0] 125 let tmp: *u8 = sys_mmap(16) 126 var kk: i64 = 0 127 if pv == 0 { tmp[0] = 48 as u8; kk = 1 } 128 while pv > 0 { tmp[kk] = (48 + (pv % 10)) as u8; pv = pv / 10; kk = kk + 1 } 129 var z: i64 = 0 130 while z < kk { let ch: *u8 = sys_mmap(2); ch[0] = tmp[kk - 1 - z]; sys_write(1, ch, 1); z = z + 1 } 131 p(" fail=" as *u8) 132 if fail[0] == 0 { p("0 verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 133 p("N verdict=RED\n" as *u8); sys_exit(1) 134 return 1 135}