code wiki / _hdl_build / nx_browser_bg_cascade_gate.nx

nx_browser_bg_cascade_gate.nx source

↩ module page · 75 lines · 5015 B

1// nx_browser_bg_cascade_gate.nx -- PROOF (re-runnable, adversary-controlled) of the S21 dark-theme fix: the 2// Nishi browser's br_comp_bg must respect CSS CASCADE ORDER across `background-color` AND the `background` 3// shorthand. The bug: it tried background-color FIRST, so the UA sheet's body{background-color:#fff} always 4// beat an author body{background:var(--bg)} -> our dark pages rendered light-text-on-white (invisible). This 5// is not "I can see it fixed" -- it's a GATE with a NEG-CONTROL that reproduces the old white result, so a 6// regression to first-background-color-wins turns T2 RED. license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_browser_render.nx" 9 10func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func pn(v: i64) -> i64 { let t: *u8=sys_mmap(24); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let b: *u8=sys_mmap(24); var j: i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 } 12 13func setcd(c: *CssComputedDecl, idx: i64, e: i64, po: i64, pl: i64, vo: i64, vl: i64) -> i64 { 14 let cd: *CssComputedDecl = ((c as i64) + idx * NX_CSS_COMPUTED_DECL_BYTES) as *CssComputedDecl 15 cd.element_idx = e 16 cd.prop_off = po; cd.prop_len = pl 17 cd.val_kind = NX_CSS_VAL_HEXCOLOR 18 cd.val_off = vo; cd.val_len = vl 19 cd.unit_off = 0; cd.unit_len = 0 20 return 0 21} 22 23func main() -> i64 { 24 hw("=== nx_browser_bg_cascade_gate -- PROOF br_comp_bg respects cascade order (S21 dark-theme fix) ===\n" as *u8) 25 var fails: i64 = 0 26 let DARK: i64 = 724756 // 0x0b0f14 (the fleet dark --bg) 27 let WHITE: i64 = 16777215 // 0xffffff (the UA body background-color) 28 29 // hex stored WITHOUT '#' (nx_css_color_decode reads 6 hex digits). offsets: 30 // background-color(0,16) ffffff(16,6) background(22,10) 0b0f14(32,6) 31 let src: *u8 = "background-colorffffffbackground0b0f14\x00" as *u8 32 let comp: *CssComputedDecl = sys_mmap(NX_CSS_COMPUTED_DECL_BYTES * 8) as *CssComputedDecl 33 setcd(comp, 0, 0, 0, 16, 16, 6) // background-color:#ffffff (UA rule, EARLIER in cascade) 34 setcd(comp, 1, 0, 22, 10, 32, 6) // background:#0b0f14 (author rule, LATER) 35 36 // T1: the fix -- the LATER author `background` shorthand wins over the earlier UA background-color 37 let bg: i64 = br_comp_bg(src, comp, 2, 0) 38 hw(" br_comp_bg -> " as *u8); pn(bg); hw(" (expect " as *u8); pn(DARK); hw(" dark)\n" as *u8) 39 var t1: i64 = 0 40 if bg == DARK { t1 = 1 } 41 if t1 == 1 { hw("T1 PASS author `background` shorthand wins over UA `background-color` (cascade order)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 42 43 // T2 NEG-CONTROL: the OLD logic (background-color FIRST) returns WHITE. Proves the bug was real + the fix 44 // is load-bearing: fixed(DARK) MUST differ from old(WHITE). If someone reverts, T1 goes white == T2 -> RED. 45 let old: i64 = br_comp_color(src, comp, 2, 0, "background-color\x00" as *u8, 16) 46 hw(" OLD (background-color-first) -> " as *u8); pn(old); hw("\n" as *u8) 47 var t2: i64 = 0 48 if old == WHITE { if bg != old { t2 = 1 } } 49 if t2 == 1 { hw("T2 PASS neg-control: old=WHITE, fixed=DARK -> the fix genuinely changed behavior (not a no-op)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL neg-control did not reproduce the white bug\n" as *u8) } 50 51 // T3 NO-REGRESSION: a page with ONLY background-color (Wikipedia-class) still resolves that color 52 let src3: *u8 = "background-color0e1116\x00" as *u8 53 let c3: *CssComputedDecl = sys_mmap(NX_CSS_COMPUTED_DECL_BYTES * 4) as *CssComputedDecl 54 setcd(c3, 0, 0, 0, 16, 16, 6) 55 let bg3: i64 = br_comp_bg(src3, c3, 1, 0) 56 hw(" pure background-color -> " as *u8); pn(bg3); hw("\n" as *u8) 57 var t3: i64 = 0 58 if bg3 == 921878 { t3 = 1 } // 0x0e1116 59 if t3 == 1 { hw("T3 PASS pure background-color unaffected (no regression on non-shorthand pages)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL\n" as *u8) } 60 61 // T4 cascade WITHIN background-color: the LAST declaration wins 62 let src4: *u8 = "background-colorffffffbackground-color123456\x00" as *u8 63 let c4: *CssComputedDecl = sys_mmap(NX_CSS_COMPUTED_DECL_BYTES * 4) as *CssComputedDecl 64 setcd(c4, 0, 0, 0, 16, 16, 6) // ffffff first 65 setcd(c4, 1, 0, 22, 16, 38, 6) // 123456 last 66 let bg4: i64 = br_comp_bg(src4, c4, 2, 0) 67 hw(" two background-color, last -> " as *u8); pn(bg4); hw("\n" as *u8) 68 var t4: i64 = 0 69 if bg4 == 1193046 { t4 = 1 } // 0x123456 70 if t4 == 1 { hw("T4 PASS last background-color wins (cascade order within one property)\n" as *u8) } else { fails=fails+1; hw("T4 FAIL\n" as *u8) } 71 72 if fails == 0 { hw("NX-BG-CASCADE GREEN -- cascade-order background resolution PROVEN + neg-control load-bearing\n" as *u8); sys_exit(0); return 0 } 73 hw("NX-BG-CASCADE RED fails=" as *u8); pn(fails); hw("\n" as *u8) 74 sys_exit(1); return 1 75}