HS tests: sync textContent when innerHTML is set

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) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 12:58:23 +00:00
parent 23e8379622
commit b81c26c45b

View File

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