OCaml HTML5 parser/serialiser based on Python's JustHTML
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: MIT
4 ---------------------------------------------------------------------------*)
5
6(** Common attribute utilities used across checkers.
7
8 This module provides simple helper functions for working with raw
9 attribute lists (name-value pairs). These utilities are used by
10 checkers that need to inspect attributes without full typed parsing.
11
12 For typed attribute access, see the {!Attr} module.
13*)
14
15(** {1 Types} *)
16
17type attrs = (string * string) list
18(** Raw attribute list as name-value pairs. *)
19
20(** {1 Attribute Lookup} *)
21
22val has_attr : string -> attrs -> bool
23(** [has_attr name attrs] checks if an attribute exists.
24
25 The comparison is case-insensitive.
26
27 @param name The attribute name to look for (lowercase)
28 @param attrs The attribute list
29 @return [true] if the attribute is present *)
30
31val get_attr : string -> attrs -> string option
32(** [get_attr name attrs] gets an attribute value.
33
34 The comparison is case-insensitive.
35
36 @param name The attribute name to look for (lowercase)
37 @param attrs The attribute list
38 @return [Some value] if found, [None] otherwise *)
39
40val get_attr_or : string -> default:string -> attrs -> string
41(** [get_attr_or name ~default attrs] gets an attribute value with a default.
42
43 @param name The attribute name to look for (lowercase)
44 @param default The default value if not found
45 @param attrs The attribute list
46 @return The attribute value or the default *)
47
48val is_non_empty_attr : string -> attrs -> bool
49(** [is_non_empty_attr name attrs] checks if an attribute exists with non-empty value.
50
51 The value is considered non-empty if it contains non-whitespace characters.
52
53 @param name The attribute name to look for (lowercase)
54 @param attrs The attribute list
55 @return [true] if the attribute exists and has a non-empty value *)
56
57(** {1 Utility Functions} *)
58
59val hashtbl_of_list : 'a list -> ('a, unit) Hashtbl.t
60(** [hashtbl_of_list items] creates a hashtable for O(1) membership testing.
61
62 @param items List of keys to add
63 @return A hashtable where each item maps to unit *)
64
65val check_disallowed_attrs :
66 element:string ->
67 collector:Message_collector.t ->
68 attrs:attrs ->
69 string list ->
70 unit
71(** [check_disallowed_attrs ~element ~collector ~attrs disallowed] reports
72 errors for any disallowed attributes that are present.
73
74 This is a convenience function to reduce repetitive attribute checking
75 code in checkers.
76
77 @param element The element name for error messages
78 @param collector The message collector
79 @param attrs The attribute list to check
80 @param disallowed List of attribute names that are not allowed *)