0 | ||| Collection of validation functions for HTML element attributes.
1 | |||
2 | ||| All these methodes return an `Either` data type.
3 | ||| If there are no errors, the provided input is returnd unchanged as `Right`
4 | ||| value.
5 | ||| If there is an error, an error message is returned as `Left` value.
18 | %hide Prelude.Stream.(::)
19 | %hide Prelude.Interfaces.(>>)
20 | %hide Data.String.(::)
21 | %hide Language.Reflection.TT.Name
24 | %language ElabReflection
26 | --------------------------------------------------------------------------------
27 | -- Helpers
28 | --------------------------------------------------------------------------------
30 | ||| `True` if the provided attribute name is a meta attribute.
35 | ||| `True` if the provided attribute name is found.
44 | ||| Extract the list of meta attributes.
49 | --------------------------------------------------------------------------------
50 | -- Unit attributes checks
51 | --------------------------------------------------------------------------------
53 | ||| Ensures that the attributes are all among the provided allowed attributes.
54 | |||
55 | ||| An error message is returned if one or more of the attributes are not among
56 | ||| the list of allowed attributes.
66 | ||| Similar to `allowed` but also allow custom global attributes.
67 | |||
68 | ||| The additional allowed attributes are the ones starting with "aria-" or
69 | ||| "data-".
80 | ||| Ensures that an attribute is only used if another attribute is present.
83 | Name
92 | ||| Check for a minimal number of attributes present among the provided list.
93 | |||
94 | ||| An error message is returned if the number of such attributes is not at
95 | ||| least of the provided length.
110 | where
122 | ||| Ensures that the attribute value is not empty.
123 | |||
124 | ||| An error message is returned if the attribute value is empty or only
125 | ||| composed of white spaces.
133 | ||| Ensures that there a no other attributes among the one provided.
134 | |||
135 | ||| An error is returned if all the `attrs` are present in `allAttrs` and
136 | ||| there is another attribute in `allAttrs` which is also present in `among`.
137 | |||
138 | ||| @attrs Attributes to check which are a subset of `among`.
139 | ||| @among Attributes against which are checked all attributes.
140 | ||| @allAttrs All the attributes.
158 | ||| Returns either the attributes' list or an error message.
159 | |||
160 | ||| An error message is returned if the required attribute is not present in the
161 | ||| list.
169 | ||| Returns either the attributes' list or an error message.
170 | |||
171 | ||| An error message is returned if the provided attribute is present in the
172 | ||| list while the required attribute is missing.
185 | ||| Ensures that all attributes are unique.
186 | |||
187 | ||| An error message is returned if a duplicate attribute name is found.
191 | where
199 | ||| Ensures that all the attributes are global attributes.
204 | ||| Global attributes and unique attributes validation.
205 | |||
206 | ||| This validation is the minimal one for any HTML element.
211 | --------------------------------------------------------------------------------
212 | -- Elab
213 | --------------------------------------------------------------------------------
215 | ||| Generates the validation function for the base case.
216 | |||
217 | ||| The base case is a function allowing global attributes and a list of
218 | ||| attributes defined in the `import Text.Html.Attributes.Types` module as
219 | ||| `<element's name>Attributes`.
220 | |||
221 | ||| Each attribute must also be unique (declared once).
222 | |||
223 | ||| For example, for the **a** element, the generated function is:
224 | |||
225 | ||| ```idris2
226 | ||| aAttributes : List Attribute -> Either String (List Attribute)
227 | ||| aAttributes attrs =
228 | ||| unique attrs >>= allowed (Type.globalAttributes ++ Type.aAttributes)
229 | ||| ```
230 | private
233 | let
237 | in
238 | declare
244 | ]
245 | ]
247 | ||| Generates validation functions based on the provided list of elements.
248 | private
284 | ]
286 | ||| Attributes allowed in a **area** element.
296 | ||| Attributes allowed in a **base** element.
304 | ||| Attributes allowed in a **data** element.
311 | ||| Attributes allowed in a **embed** element.
319 | ||| Attributes allowed in a **img** element.
320 | |||
321 | ||| There must be:
322 | |||
323 | ||| - an **alt** attribute;
324 | ||| - a **src** and/or **srcset**.
333 | ||| Attributes allowed in a **label** element.
340 | where
350 | "ERROR: for a **label** element the maximal number of arguments is 1."
353 | ||| Attributes allowed in a **li** element.
360 | ||| Attributes allowed in a **html** element.
365 | ||| Ensures that the attributes of a **meta** element are correctly structured.
366 | ||| There must be:
367 | |||
368 | ||| - only global or meta attributes;
369 | ||| - at least one meta attribute;
370 | ||| - only the meta attribute **charset** as meta attribute if present;
371 | ||| - both and only meta attributes **name** and **content** if one of them is
372 | ||| present;
380 | )
385 | ||| Attributes allowed in a **script** element.
394 | ||| Attributes allowed in a **source** element.
402 | ||| Attributes allowed in a **track** element.