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.
  6 | module Text.Html.Attributes.Validation
  7 |
  8 | import Data.Either
  9 | import Data.String
 10 | import Language.Reflection
 11 | import Language.Reflection.Syntax
 12 | import Language.Reflection.Syntax.Ops
 13 |
 14 | import Text.Html.Attributes.Types
 15 | import Text.Html.Types
 16 | import Text.Html.Utils
 17 |
 18 | %hide Prelude.Stream.(::)
 19 | %hide Prelude.Interfaces.(>>)
 20 | %hide Data.String.(::)
 21 | %hide Language.Reflection.TT.Name
 22 |
 23 | %default total
 24 | %language ElabReflection
 25 |
 26 | --------------------------------------------------------------------------------
 27 | -- Helpers
 28 | --------------------------------------------------------------------------------
 29 |
 30 | ||| `True` if the provided attribute name is a meta attribute.
 31 | public export
 32 | isMeta : Name -> Bool
 33 | isMeta attr = elem attr Types.metaAttributes
 34 |
 35 | ||| `True` if the provided attribute name is found.
 36 | public export
 37 | isAttrOf : Name -> List Attribute -> Bool
 38 | isAttrOf n [] = False
 39 | isAttrOf n attrs@(x :: xs) =
 40 |   if n == x.name
 41 |   then True
 42 |   else isAttrOf n xs
 43 |
 44 | ||| Extract the list of meta attributes.
 45 | public export
 46 | metaAttrs : List Attribute -> List Name
 47 | metaAttrs attrs = filter isMeta (attributesNames attrs)
 48 |
 49 | --------------------------------------------------------------------------------
 50 | -- Unit attributes checks
 51 | --------------------------------------------------------------------------------
 52 |
 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.
 57 | public export
 58 | allowed : List Name -> List Attribute -> Either String (List Attribute)
 59 | allowed [] _ = Left "ERROR: no attributes are allowed."
 60 | allowed ns [] = Right []
 61 | allowed ns (x :: xs) =
 62 |   if elem x.name ns
 63 |   then Right (x :: !(allowed ns xs))
 64 |   else Left "ERROR: attribute **\{x.name}** is not allowed."
 65 |
 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-".
 70 | public export
 71 | allowedGlobal : List Name -> List Attribute -> Either String (List Attribute)
 72 | allowedGlobal ns [] = Right []
 73 | allowedGlobal ns (x :: xs) =
 74 |   if    strSubstr 0 5 x.name == "data-"
 75 |      || strSubstr 0 5 x.name == "aria-"
 76 |      || elem x.name ns
 77 |   then Right (x :: !(allowedGlobal ns xs))
 78 |   else Left "ERROR: attribute **\{x.name}** is not allowed."
 79 |
 80 | ||| Ensures that an attribute is only used if another attribute is present.
 81 | public export
 82 | allowedIfPresent :
 83 |      Name
 84 |   -> (otherAttr : Name)
 85 |   -> List Attribute
 86 |   -> Either String (List Attribute)
 87 | allowedIfPresent n o attrs =
 88 |   if (isAttrOf n attrs && isAttrOf o attrs) || (isAttrOf n attrs == False)
 89 |   then Right attrs
 90 |   else Left "ERROR: attribute **\{n}** is present while **\{o}** is missing."
 91 |
 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.
 96 | public export
 97 | lengthAmong :
 98 |      (length : Nat)
 99 |   -> (among : List Name)
100 |   -> (allAttrs : List Attribute)
101 |   -> Either String (List Attribute)
102 | lengthAmong l among allAttrs =
103 |   let n = length (amongAttrs allAttrs) in
104 |   if n >= l
105 |   then Right allAttrs
106 |   else Left $
107 |         "ERROR: expected at least \{show l} attributes among the below list: "
108 |     <+> "\{joinBy ", " among} - got \{show n}. "
109 |     <+> endErrorMsg allAttrs
110 |   where
111 |     isAmong : Name -> Bool
112 |     isAmong attr = elem attr among
113 |
114 |     amongAttrs : List Attribute -> List Name
115 |     amongAttrs attrs = filter isAmong (attributesNames attrs)
116 |
117 |     endErrorMsg : List Attribute -> String
118 |     endErrorMsg []    = "No attributes were provided."
119 |     endErrorMsg attrs = "Here's the list of provided attributes: "
120 |                          <+> "\{joinBy ", " (attributesNames attrs)}"
121 |
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.
126 | public export
127 | nonEmpty : Attribute -> Either String Attribute
128 | nonEmpty attr =
129 |   if trim (show attr.value) == ""
130 |   then Left "ERROR: empty value for **\{attr.name}** attribute."
131 |   else Right attr
132 |
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.
141 | public export
142 | noOtherAttrs :
143 |      (attrs : List Name)
144 |   -> (among : List Name)
145 |   -> (allAttrs : List Attribute)
146 |   -> Either String (List Attribute)
147 | noOtherAttrs attrs among allAttrs =
148 |   let mAttrs = metaAttrs allAttrs in
149 |    if (all (\x => elem x among) mAttrs && length attrs == length mAttrs)
150 |       || (any (\x => elem x attrs) mAttrs /= True)
151 |   then Right allAttrs
152 |   else Left $
153 |         "ERROR: if present, \{joinBy ", " attrs} can be the only attribute(s) "
154 |     <+> "among the below attributes: \{joinBy ", " among}. "
155 |     <+> "Here's the full list of attributes provided part of the above list: "
156 |     <+> "\{joinBy ", " mAttrs}"
157 |
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.
162 | public export
163 | required : Name -> List Attribute -> Either String (List Attribute)
164 | required n attrs =
165 |   if isAttrOf n attrs
166 |   then Right attrs
167 |   else Left "ERROR: required attribute **\{n}** is missing."
168 |
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.
173 | public export
174 | requiredIfPresent :
175 |      (requiredAtr : Name)
176 |   -> Name
177 |   -> List Attribute
178 |   -> Either String (List Attribute)
179 | requiredIfPresent r n attrs =
180 |   if isAttrOf n attrs && (isAttrOf r attrs == False)
181 |   then Left
182 |     "ERROR: attribute **\{n}** is present while attribute **\{n}** is missing."
183 |   else Right attrs
184 |
185 | ||| Ensures that all attributes are unique.
186 | |||
187 | ||| An error message is returned if a duplicate attribute name is found.
188 | public export
189 | unique : List Attribute -> Either String (List Attribute)
190 | unique attrs = helper [] (attributesNames attrs)
191 |   where
192 |     helper : List String -> List String -> Either String (List Attribute)
193 |     helper _ []                = Right attrs
194 |     helper seenSoFar (x :: xs) =
195 |       if x `elem` seenSoFar
196 |       then Left "ERROR: duplicate attribute **\{x}**."
197 |       else helper (x :: seenSoFar) xs
198 |
199 | ||| Ensures that all the attributes are global attributes.
200 | public export
201 | globalAttributes : List Attribute -> Either String (List Attribute)
202 | globalAttributes = allowedGlobal Types.globalAttributes
203 |
204 | ||| Global attributes and unique attributes validation.
205 | |||
206 | ||| This validation is the minimal one for any HTML element.
207 | public export
208 | standard : List Attribute -> Either String (List Attribute)
209 | standard attrs = unique attrs >>= globalAttributes
210 |
211 | --------------------------------------------------------------------------------
212 | -- Elab
213 | --------------------------------------------------------------------------------
214 |
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
231 | mkAttributeValidation : (name : String) -> Elab ()
232 | mkAttributeValidation name =
233 |   let
234 |     functionName := UN . Basic $ name ++ "Attributes"
235 |     argList      := arg $ var "List" .$ var "Attribute"
236 |     listAttrs    := var "++" .$ var "globalAttributes" .$ var functionName
237 |   in
238 |   declare
239 |    [ public' functionName $ argList .-> `(Either String (List Attribute))
240 |    , def functionName [var functionName .$ var "attrs" .=
241 |      var ">>="
242 |        .$ (var "unique" .$ var "attrs")
243 |        .$ (var "allowed" .$ listAttrs)
244 |      ]
245 |    ]
246 |
247 | ||| Generates validation functions based on the provided list of elements.
248 | private
249 | mkAttributesValidation : List String -> Elab ()
250 | mkAttributesValidation names = mconcatMapM mkAttributeValidation names
251 |
252 | %runElab mkAttributesValidation
253 |   [ "a"
254 |   , "audio"
255 |   , "button"
256 |   , "canvas"
257 |   , "change"
258 |   , "citation"
259 |   , "col"
260 |   , "details"
261 |   , "dialog"
262 |   , "fieldset"
263 |   , "form"
264 |   , "iframe"
265 |   , "input"
266 |   , "link"
267 |   , "map"
268 |   , "meter"
269 |   , "object"
270 |   , "ol"
271 |   , "optgroup"
272 |   , "option"
273 |   , "output"
274 |   , "progress"
275 |   , "select"
276 |   , "slot"
277 |   , "style"
278 |   , "td"
279 |   , "template"
280 |   , "textarea"
281 |   , "th"
282 |   , "time"
283 |   , "video"
284 |   ]
285 |
286 | ||| Attributes allowed in a **area** element.
287 | public export
288 | areaAttributes : List Attribute -> Either String (List Attribute)
289 | areaAttributes attrs =
290 |       unique attrs
291 |   >>= allowed (Types.globalAttributes ++ Types.areaAttributes)
292 |   >>= requiredIfPresent "alt" "href"
293 |   >>= allowedIfPresent "rel" "href"
294 |   >>= allowedIfPresent "target" "href"
295 |
296 | ||| Attributes allowed in a **base** element.
297 | public export
298 | baseAttributes : List Attribute -> Either String (List Attribute)
299 | baseAttributes attrs =
300 |       unique attrs
301 |   >>= allowed (Types.globalAttributes ++ Types.baseAttributes)
302 |   >>= lengthAmong 1 ["href", "target"]
303 |
304 | ||| Attributes allowed in a **data** element.
305 | public export
306 | dataAttributes : List Attribute -> Either String (List Attribute)
307 | dataAttributes attrs =
308 |       unique attrs
309 |   >>= allowed ("value" :: Types.globalAttributes)
310 |
311 | ||| Attributes allowed in a **embed** element.
312 | public export
313 | embedAttributes : List Attribute -> Either String (List Attribute)
314 | embedAttributes attrs =
315 |       unique attrs
316 |   >>= allowed (Types.globalAttributes ++ Types.embedAttributes)
317 |   >>= required "src"
318 |
319 | ||| Attributes allowed in a **img** element.
320 | |||
321 | ||| There must be:
322 | |||
323 | ||| - an **alt** attribute;
324 | ||| - a **src** and/or **srcset**.
325 | public export
326 | imgAttributes : List Attribute -> Either String (List Attribute)
327 | imgAttributes attrs =
328 |       unique attrs
329 |   >>= allowed (Types.globalAttributes ++ Types.imgAttributes)
330 |   >>= lengthAmong 1 ["src", "srcset"]
331 |   >>= required "alt"
332 |
333 | ||| Attributes allowed in a **label** element.
334 | public export
335 | labelAttributes : List Attribute -> Either String (List Attribute)
336 | labelAttributes attrs =
337 |       unique attrs
338 |   >>= allowed (Types.globalAttributes ++ Types.labelAttributes)
339 |   >>= checkFor
340 |   where
341 |     attrValLength : Maybe EscapedValue -> Nat
342 |     attrValLength Nothing    = 0
343 |     attrValLength (Just val) = length $ words $ show val
344 |
345 |     checkFor : List Attribute -> Either String (List Attribute)
346 |     checkFor []        = Right []
347 |     checkFor (x :: xs) =
348 |       if x.name == "for" && attrValLength x.value > 1
349 |       then Left
350 |         "ERROR: for a **label** element the maximal number of arguments is 1."
351 |       else Right xs
352 |
353 | ||| Attributes allowed in a **li** element.
354 | public export
355 | liAttributes : List Attribute -> Either String (List Attribute)
356 | liAttributes attrs =
357 |       unique attrs
358 |   >>= allowed ("value" :: Types.globalAttributes)
359 |
360 | ||| Attributes allowed in a **html** element.
361 | public export
362 | htmlAttributes : List Attribute -> Either String (List Attribute)
363 | htmlAttributes attrs = standard attrs >>= required "lang"
364 |
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;
373 | public export
374 | metaAttributes : List Attribute -> Either String (List Attribute)
375 | metaAttributes attrs =
376 |       unique attrs
377 |   >>= allowed
378 |         (  Types.globalAttributes
379 |         ++ Types.metaAttributes
380 |         )
381 |   >>= lengthAmong 1 metaAttributes
382 |   >>= noOtherAttrs ["charset"] metaAttributes
383 |   >>= noOtherAttrs ["name", "content"] metaAttributes
384 |
385 | ||| Attributes allowed in a **script** element.
386 | public export
387 | scriptAttributes : List Attribute -> Either String (List Attribute)
388 | scriptAttributes attrs =
389 |       unique attrs
390 |   >>= allowed (Types.globalAttributes ++ Types.scriptAttributes)
391 |   >>= allowedIfPresent "async" "src"
392 |   >>= allowedIfPresent "defer" "src"
393 |
394 | ||| Attributes allowed in a **source** element.
395 | public export
396 | sourceAttributes : List Attribute -> Either String (List Attribute)
397 | sourceAttributes attrs =
398 |       unique attrs
399 |   >>= allowed (Types.globalAttributes ++ Types.sourceAttributes)
400 |   >>= lengthAmong 1 ["src", "srcset"]
401 |
402 | ||| Attributes allowed in a **track** element.
403 | public export
404 | trackAttributes : List Attribute -> Either String (List Attribute)
405 | trackAttributes attrs =
406 |       unique attrs
407 |   >>= allowed (Types.globalAttributes ++ Types.trackAttributes)
408 |   >>= required "src"
409 |