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 []);