OCaml HTML5 parser/serialiser based on Python's JustHTML
1(** Test for nesting checker functionality via public API *)
2
3let check_html html =
4 let reader = Bytesrw.Bytes.Reader.of_string html in
5 Htmlrw_check.check reader
6
7let () =
8 (* Test 1: <a> cannot contain another <a> *)
9 Printf.printf "Test 1: Checking <a href> inside <a href>\n";
10 let result1 = check_html "<a href='#'><a href='#'>nested</a></a>" in
11 let errors1 = Htmlrw_check.errors result1 in
12 Printf.printf " Found %d error(s)\n" (List.length errors1);
13 List.iter (fun msg ->
14 Printf.printf " - %s\n" msg.Htmlrw_check.text
15 ) errors1;
16
17 (* Test 2: <button> inside <a> *)
18 Printf.printf "\nTest 2: Checking <button> inside <a href>\n";
19 let result2 = check_html "<a href='#'><button>click</button></a>" in
20 let errors2 = Htmlrw_check.errors result2 in
21 Printf.printf " Found %d error(s)\n" (List.length errors2);
22 List.iter (fun msg ->
23 Printf.printf " - %s\n" msg.Htmlrw_check.text
24 ) errors2;
25
26 (* Test 3: form inside form *)
27 Printf.printf "\nTest 3: Checking <form> inside <form>\n";
28 let result3 = check_html "<form><form>nested</form></form>" in
29 let errors3 = Htmlrw_check.errors result3 in
30 Printf.printf " Found %d error(s)\n" (List.length errors3);
31 List.iter (fun msg ->
32 Printf.printf " - %s\n" msg.Htmlrw_check.text
33 ) errors3;
34
35 (* Test 4: header inside footer (should be allowed) *)
36 Printf.printf "\nTest 4: Checking <header> inside <footer>\n";
37 let result4 = check_html "<footer><header>test</header></footer>" in
38 let errors4 = Htmlrw_check.errors result4 in
39 Printf.printf " Found %d error(s)\n" (List.length errors4);
40 if List.length errors4 > 0 then
41 List.iter (fun msg ->
42 Printf.printf " - %s\n" msg.Htmlrw_check.text
43 ) errors4
44 else
45 Printf.printf " OK: No errors (header inside footer is valid)\n";
46
47 (* Test 5: input inside button *)
48 Printf.printf "\nTest 5: Checking <input type=text> inside <button>\n";
49 let result5 = check_html "<button><input type='text'></button>" in
50 let errors5 = Htmlrw_check.errors result5 in
51 Printf.printf " Found %d error(s)\n" (List.length errors5);
52 List.iter (fun msg ->
53 Printf.printf " - %s\n" msg.Htmlrw_check.text
54 ) errors5;
55
56 (* Test 6: valid nesting - should not error *)
57 Printf.printf "\nTest 6: Checking valid nesting: <div> inside <div>\n";
58 let result6 = check_html "<div><div>nested</div></div>" in
59 let errors6 = Htmlrw_check.errors result6 in
60 Printf.printf " Found %d error(s)\n" (List.length errors6);
61 if List.length errors6 = 0 then
62 Printf.printf " OK: No errors as expected\n"
63 else
64 List.iter (fun msg ->
65 Printf.printf " - %s\n" msg.Htmlrw_check.text
66 ) errors6;
67
68 Printf.printf "\nAll tests completed!\n"