code wiki / _hdl_build / nx_domtree_gate.nx

nx_domtree_gate.nx source

↩ module page · 96 lines · 4516 B

1// nx_domtree_gate.nx -- gate for the mutable DOM (rung A). Drives the REAL organ over a real fixture + 2// mutations: parse->serialize round-trip, getElementById (+ neg control), createElement/appendChild, 3// setAttribute (update-in-place), textContent= (replace children). GREEN only if ALL pass. 0=GREEN/1=RED. 4import "nx_syscalls.nx" 5import "nx_domtree.nx" 6 7func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func g_pn(v: i64) -> i64 { 9 var m: i64 = v 10 if m < 0 { g_p("-\x00" as *u8); m = 0 - m } 11 let t: *u8 = sys_mmap(24); var k: i64 = 0 12 if m == 0 { t[0] = 48 as u8; k = 1 } else { while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } } 13 let o: *u8 = sys_mmap(24); var j: i64 = 0 14 while j < k { o[j] = t[k-1-j]; j = j + 1 } 15 sys_write(1, o, k); return 0 16} 17// does buf[0..blen) contain the NUL-terminated needle? 18func g_contains(buf: *u8, blen: i64, needle: *u8) -> i64 { 19 let nl: i64 = dt_clen(needle) 20 if nl == 0 { return 1 } 21 var i: i64 = 0 22 while (i + nl) <= blen { 23 var j: i64 = 0 24 var ok: i64 = 1 25 while j < nl { if (buf[i + j] & 0xff) != (needle[j] & 0xff) { ok = 0; j = nl } else { j = j + 1 } } 26 if ok == 1 { return 1 } 27 i = i + 1 28 } 29 return 0 30} 31func chk(cond: i64, name: *u8, pp: *i64, tp: *i64) -> i64 { 32 tp[0] = tp[0] + 1 33 g_p(name) 34 if cond == 1 { pp[0] = pp[0] + 1; g_p(" ok\n\x00" as *u8) } else { g_p(" FAIL\n\x00" as *u8) } 35 return 0 36} 37 38func main() -> i64 { 39 let pp: *i64 = sys_mmap(8) as *i64; pp[0] = 0 40 let tp: *i64 = sys_mmap(8) as *i64; tp[0] = 0 41 42 let page: *u8 = "<html><body><h1 id=\"a\" class=\"t\">Hi</h1><ul><li>x</li><li>y</li></ul></body></html>\x00" as *u8 43 let t: *DomTree = dt_parse(page, dt_clen(page)) 44 let out: *u8 = sys_mmap(65536) 45 var ln: i64 = dt_serialize(t, out, 65536) 46 47 chk(g_contains(out, ln, "<h1\x00" as *u8), "C1 parse/ser <h1>\x00" as *u8, pp, tp) 48 chk(g_contains(out, ln, "id=\"a\"\x00" as *u8), "C2 attr id round-trip\x00" as *u8, pp, tp) 49 chk(g_contains(out, ln, "<li>x</li>\x00" as *u8), "C3 <li> nesting\x00" as *u8, pp, tp) 50 51 let h1: i64 = dt_get_element_by_id(t, "a\x00" as *u8) 52 chk(h1 != (0 - 1), "C4 getElementById hit\x00" as *u8, pp, tp) 53 chk(dt_get_element_by_id(t, "nope\x00" as *u8) == (0 - 1), "C5 getElementById miss(neg)\x00" as *u8, pp, tp) 54 55 let tc: *u8 = sys_mmap(256) 56 let tcl: i64 = dt_text_content(t, h1, tc, 256) 57 var c6: i64 = 0 58 if tcl == 2 { if (tc[0] & 0xff) == 72 { if (tc[1] & 0xff) == 105 { c6 = 1 } } } // "Hi" 59 chk(c6, "C6 textContent=Hi\x00" as *u8, pp, tp) 60 61 let body: i64 = dt_first_by_tag(t, "body\x00" as *u8) 62 let p: i64 = dt_create_element(t, "p\x00" as *u8) 63 dt_set_text_content(t, p, "world\x00" as *u8, 0, 5) 64 dt_append_child(t, body, p) 65 ln = dt_serialize(t, out, 65536) 66 chk(g_contains(out, ln, "<p>world</p>\x00" as *u8), "C7 createElement+append\x00" as *u8, pp, tp) 67 68 dt_set_attr(t, h1, "class\x00" as *u8, "big\x00" as *u8) 69 ln = dt_serialize(t, out, 65536) 70 chk(g_contains(out, ln, "class=\"big\"\x00" as *u8), "C8 setAttribute(update)\x00" as *u8, pp, tp) 71 72 dt_set_text_content(t, h1, "Bye\x00" as *u8, 0, 3) 73 ln = dt_serialize(t, out, 65536) 74 var c9: i64 = 0 75 if g_contains(out, ln, "Bye\x00" as *u8) == 1 { if g_contains(out, ln, ">Hi<\x00" as *u8) == 0 { c9 = 1 } } 76 chk(c9, "C9 textContent= replaces children\x00" as *u8, pp, tp) 77 78 let body2: i64 = dt_first_by_tag(t, "body\x00" as *u8) 79 let frag: *u8 = "<b>JS built this</b>\x00" as *u8 80 dt_set_inner_html(t, body2, frag, dt_clen(frag)) 81 ln = dt_serialize(t, out, 65536) 82 chk(g_contains(out, ln, "<b>JS built this</b>\x00" as *u8), "C10 innerHTML fragment\x00" as *u8, pp, tp) 83 84 let rawpage: *u8 = "<div>a</div><script>if(x<y){z=1}</script><p>b</p>\x00" as *u8 85 let t2: *DomTree = dt_parse(rawpage, dt_clen(rawpage)) 86 let out2: *u8 = sys_mmap(65536) 87 let ln2: i64 = dt_serialize(t2, out2, 65536) 88 var c11: i64 = 0 89 if g_contains(out2, ln2, "if(x<y){z=1}\x00" as *u8) == 1 { if g_contains(out2, ln2, "<p>b</p>\x00" as *u8) == 1 { c11 = 1 } } 90 chk(c11, "C11 RCDATA script not mistokenized\x00" as *u8, pp, tp) 91 92 g_p("DOMTREEGATE pass=\x00" as *u8); g_pn(pp[0]); g_p("/\x00" as *u8); g_pn(tp[0]); g_p("\n\x00" as *u8) 93 if pp[0] == tp[0] { g_p("DOMTREEGATE verdict=GREEN\n\x00" as *u8); return 0 } 94 g_p("DOMTREEGATE verdict=RED\n\x00" as *u8) 95 return 1 96}