OCaml HTML5 parser/serialiser based on Python's JustHTML
1(** Common attribute utilities used across checkers. *)
2
3type attrs = (string * string) list
4
5let has_attr name attrs =
6 List.exists (fun (n, _) -> Astring.String.Ascii.lowercase n = name) attrs
7
8let get_attr name attrs =
9 List.find_map (fun (n, v) ->
10 if Astring.String.Ascii.lowercase n = name then Some v else None
11 ) attrs
12
13let get_attr_or name ~default attrs =
14 Option.value ~default (get_attr name attrs)
15
16let is_non_empty_attr name attrs =
17 match get_attr name attrs with
18 | Some v -> String.trim v <> ""
19 | None -> false
20
21(** Create a unit hashtable from a list of keys for O(1) membership testing. *)
22let hashtbl_of_list items =
23 let tbl = Hashtbl.create (List.length items) in
24 List.iter (fun x -> Hashtbl.add tbl x ()) items;
25 tbl
26
27(** Check a list of attributes and report errors for any that are present. *)
28let check_disallowed_attrs ~element ~collector ~attrs disallowed =
29 List.iter (fun attr ->
30 if has_attr attr attrs then
31 Message_collector.add_typed collector
32 (`Attr (`Not_allowed (`Attr attr, `Elem element)))
33 ) disallowed