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 *) | Some (Dict _cl) -> () (* classes live in className *)
| _ -> ()) | _ -> ())
| "innerHTML" -> | "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 (match stored with
| String "" -> | String s ->
(* Detach children *) let buf = Buffer.create (String.length s) in
let kids = match Hashtbl.find_opt d "children" with Some (List l) -> l | _ -> [] in let in_tag = ref false in
List.iter (fun c -> match c with Dict cd -> String.iter (fun c ->
Hashtbl.replace cd "parentElement" Nil; if c = '<' then in_tag := true
Hashtbl.replace cd "parentNode" Nil | _ -> ()) kids; else if c = '>' then in_tag := false
Hashtbl.replace d "children" (List []); else if not !in_tag then Buffer.add_char buf c
Hashtbl.replace d "childNodes" (List []); ) s;
Hashtbl.replace d "textContent" (String "") Hashtbl.replace d "textContent" (String (Buffer.contents buf))
| _ -> ()) | _ -> Hashtbl.replace d "textContent" (String ""))
| "textContent" -> | "textContent" ->
(* Setting textContent clears children *) (* Setting textContent clears children *)
Hashtbl.replace d "children" (List []); Hashtbl.replace d "children" (List []);