From b81c26c45b15819de245a5e7e061f2bd8881b8be Mon Sep 17 00:00:00 2001 From: giles Date: Tue, 14 Apr 2026 12:58:23 +0000 Subject: [PATCH] HS tests: sync textContent when innerHTML is set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When innerHTML is set on a mock element, textContent now updates to match (with HTML tags stripped). Many HS tests do `put "foo" into me` (which sets innerHTML) then check textContent. Previously textContent stayed empty because only innerHTML was updated. Also fixes innerHTML="" to fully detach children from parent. 393 → 408/831 HS tests (+15). Co-Authored-By: Claude Opus 4.6 (1M context) --- hosts/ocaml/bin/run_tests.ml | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/hosts/ocaml/bin/run_tests.ml b/hosts/ocaml/bin/run_tests.ml index ba3f43fa..d8c7f544 100644 --- a/hosts/ocaml/bin/run_tests.ml +++ b/hosts/ocaml/bin/run_tests.ml @@ -1669,18 +1669,25 @@ let run_spec_tests env test_files = | Some (Dict _cl) -> () (* classes live in className *) | _ -> ()) | "innerHTML" -> - (* Setting innerHTML clears children (like a real browser) *) + (* Setting innerHTML clears children and syncs textContent (like a browser) *) + let kids = match Hashtbl.find_opt d "children" with Some (List l) -> l | _ -> [] in + List.iter (fun c -> match c with Dict cd -> + Hashtbl.replace cd "parentElement" Nil; + Hashtbl.replace cd "parentNode" Nil | _ -> ()) kids; + Hashtbl.replace d "children" (List []); + Hashtbl.replace d "childNodes" (List []); + (* Approximate textContent: strip HTML tags from innerHTML *) (match stored with - | String "" -> - (* Detach children *) - let kids = match Hashtbl.find_opt d "children" with Some (List l) -> l | _ -> [] in - List.iter (fun c -> match c with Dict cd -> - Hashtbl.replace cd "parentElement" Nil; - Hashtbl.replace cd "parentNode" Nil | _ -> ()) kids; - Hashtbl.replace d "children" (List []); - Hashtbl.replace d "childNodes" (List []); - Hashtbl.replace d "textContent" (String "") - | _ -> ()) + | String s -> + let buf = Buffer.create (String.length s) in + let in_tag = ref false in + String.iter (fun c -> + if c = '<' then in_tag := true + else if c = '>' then in_tag := false + else if not !in_tag then Buffer.add_char buf c + ) s; + Hashtbl.replace d "textContent" (String (Buffer.contents buf)) + | _ -> Hashtbl.replace d "textContent" (String "")) | "textContent" -> (* Setting textContent clears children *) Hashtbl.replace d "children" (List []);