0 | ||| Collection of validation functions for HTML elements.
   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.Elements.Validation
   7 |
   8 | import Data.Either
   9 | import Data.String
  10 |
  11 | import Text.Html.Attributes.Types
  12 | import Text.Html.Attributes.Validation
  13 | import Text.Html.Elements.Types
  14 | import Text.Html.Utils
  15 | import Text.Html.Types
  16 |
  17 | %default total
  18 |
  19 | --------------------------------------------------------------------------------
  20 | -- Unit elements validations
  21 | --------------------------------------------------------------------------------
  22 |
  23 | ||| Returns `True` if the element's name can be found in the list of elements.
  24 | public export
  25 | required' : Name -> List Element -> Bool
  26 | required' n [] = False
  27 | required' n (x :: xs) = if n == name x then True else required' n xs
  28 |
  29 | ||| Returns `True` if one of the elements' name can be found in the list.
  30 | private
  31 | requireds' : List Name -> List Element -> Bool
  32 | requireds' [] _ = False
  33 | requireds' _ [] = False
  34 | requireds' (n :: ns) xs =
  35 |   if required' n xs
  36 |   then True
  37 |   else requireds' ns xs
  38 |
  39 | ||| Returns either the elements' list or an error message.
  40 | |||
  41 | ||| An error message is returned if the element's list is empty or if no element
  42 | ||| have the provided name.
  43 | public export
  44 | required : Name -> List Element -> Either String (List Element)
  45 | required n xs =
  46 |   if required' n xs
  47 |   then Right xs
  48 |   else Left $ "ERROR: **\{n}** element is missing."
  49 |
  50 | ||| Returns either the elements' list or an error message.
  51 | |||
  52 | ||| An error message is returned if the element's list does not contain at least
  53 | ||| one element of the provided list of name.
  54 | public export
  55 | requireds : List Name -> List Element -> Either String (List Element)
  56 | requireds ns xs =
  57 |   if requireds' ns xs
  58 |   then Right xs
  59 |   else Left $  "ERROR: at least one of the following element must be present: "
  60 |             ++ "**\{show ns}**."
  61 |
  62 | public export
  63 | forbiden : Name -> List Element -> Either String (List Element)
  64 | forbiden n xs =
  65 |   if required' n xs
  66 |   then Left $ "ERROR: **\{n}** element is not allowed."
  67 |   else Right xs
  68 |
  69 | public export
  70 | forbidens : List Name -> List Element -> Either String (List Element)
  71 | forbidens ns xs =
  72 |   if requireds' ns xs
  73 |   then Left
  74 |     $ "ERROR: one of the provided element **\{show ns}** is not allowed."
  75 |   else Right xs
  76 |
  77 | ||| Returns either the elements' list or an error message.
  78 | |||
  79 | ||| An error message is returned if the first element of the list doesn't match
  80 | ||| the provided name.
  81 | public export
  82 | first : Name -> List Element -> Either String (List Element)
  83 | first n [] = Left $ "ERROR: **\{n}** element is missing."
  84 | first n elems@(x :: _) =
  85 |   if n == name x
  86 |   then Right elems
  87 |   else Left $  "ERROR: **\{n}** element is missing in first position."
  88 |
  89 | ||| Returns either the elements' list or an error message.
  90 | |||
  91 | ||| An error message is returned if the second element of the list doesn't match
  92 | ||| the provided name.
  93 | public export
  94 | second : Name -> List Element -> Either String (List Element)
  95 | second n [] = Left $ "ERROR: **\{n}** element is missing."
  96 | second n (_ :: []) = Left $ "ERROR: \"\{n}\" element is missing."
  97 | second n elems@(_ :: y :: _) =
  98 |   if n == name y
  99 |   then Right elems
 100 |   else Left $  "ERROR: **\{n}** element is missing in first position."
 101 |
 102 | ||| Returns either the elements' list or an error message.
 103 | |||
 104 | ||| An error message is returned if the list's length doesn't match the provided
 105 | ||| length.
 106 | public export
 107 | length : (length : Nat) -> List Element -> Either String (List Element)
 108 | length l xs =
 109 |   let l' = List.length xs in
 110 |   if l == l'
 111 |   then Right xs
 112 |   else Left $
 113 |   "ERROR: expected length \{natToStr l}, got \{natToStr l}."
 114 |
 115 | ||| Returns either the elements' list or an error message.
 116 | |||
 117 | ||| An error message is returned if the number of occurences of the provided
 118 | ||| element name doesn't match the provided count using the provided comparison
 119 | ||| operator.
 120 | public export
 121 | compareWith :
 122 |      (Nat -> Nat -> Bool)
 123 |   -> Name
 124 |   -> (count : Nat)
 125 |   -> List Element
 126 |   -> Either String (List Element)
 127 | compareWith op nm cnt elems =
 128 |   let result = countHelper 0 nm elems in
 129 |   if op result cnt
 130 |   then Right elems
 131 |   else Left $
 132 |         "ERROR: **\{nm}** element expected \{natToStr cnt} times, "
 133 |     <+> "got \{natToStr result}."
 134 |   where
 135 |     countHelper : (count : Nat) -> Name -> List Element -> Nat
 136 |     countHelper c n []        = c
 137 |     countHelper c n (x :: xs) = if n == name x
 138 |                                 then countHelper (c + 1) n xs
 139 |                                 else countHelper c n xs
 140 |
 141 | ||| Returns either the elements' list or an error message.
 142 | |||
 143 | ||| An error message is returned if the number of occurences of the provided
 144 | ||| element name doesn't match the provided count.
 145 | public export
 146 | count : Name -> (count : Nat) -> List Element -> Either String (List Element)
 147 | count = compareWith (==)
 148 |
 149 | ||| Returns either the elements' list or an error message.
 150 | |||
 151 | ||| An error message is returned if the number of occurences of the provided
 152 | ||| element is higher than the provided count.
 153 | public export
 154 | max : Name -> (count : Nat) -> List Element -> Either String (List Element)
 155 | max = compareWith (<=)
 156 |
 157 | ||| Ensures that the element is the expected one.
 158 | public export
 159 | is : (name : String) -> Element -> Either String Element
 160 | is n el =
 161 |   if n == name el
 162 |   then Right el
 163 |   else Left $
 164 |     "ERROR: element **\{name el}** is not the expected **\{n}** element."
 165 |
 166 | ||| Ensures that the `Element`'s name is part of the provided list.
 167 | |||
 168 | ||| @ listName the name of the list of elements. For example flow content
 169 | |||            elements, phrasing content elements, etc.
 170 | public export
 171 | elemOf :
 172 |      List String
 173 |   -> (listName : String)
 174 |   -> Element
 175 |   -> Either String Element
 176 | elemOf xs listName el =
 177 |   let n = name el in
 178 |   if elem n xs
 179 |   then Right el
 180 |   else Left $
 181 |         "ERROR: element **\{n}** is not a \{listName} element. "
 182 |     <+> "The \{listName} elements are: \{joinBy ", " xs}."
 183 |
 184 | ||| Returns either the elements' list or an error message.
 185 | |||
 186 | ||| An error message is returned if:
 187 | |||
 188 | ||| - the first element is not the optional element or an element of the list;
 189 | ||| - a further element is not an element of the list.
 190 | public export
 191 | firstOptional :
 192 |      (optionalElem : Name)
 193 |   -> (listElems : List String)
 194 |   -> (setName : String)
 195 |   -> List Element
 196 |   -> Either String (List Element)
 197 | firstOptional n es setName []        = Right []
 198 | firstOptional n es setName (x :: xs) =
 199 |   if n == name x || elem (name x) es
 200 |   then Right (x :: !(checkTail es setName xs))
 201 |   else Left $  "ERROR: **\{show $ name x}** is not authorised. The allowed "
 202 |             ++ "elements in first position are: **\{show n}** and \{setName}."
 203 |   where
 204 |     checkTail :
 205 |          (listElems : List String)
 206 |       -> (setName : String)
 207 |       -> List Element
 208 |       -> Either String (List Element)
 209 |     checkTail es setName xs = mapM (elemOf es setName) xs
 210 |
 211 | ||| Returns either the elements' list or an error message.
 212 | |||
 213 | ||| An error message is returned if:
 214 | |||
 215 | ||| - the first elements are not the optional element or an element of the list;
 216 | ||| - a further element is not an element of the list.
 217 | public export
 218 | firstsOptional :
 219 |      (optionalElem : Name)
 220 |   -> (listElems : List String)
 221 |   -> (setName : String)
 222 |   -> List Element
 223 |   -> Either String (List Element)
 224 | firstsOptional n es setName []        = Right []
 225 | firstsOptional n es setName (x :: xs) =
 226 |   if n == name x || elem (name x) es
 227 |   then Right (x :: !(firstsOptional n es setName xs))
 228 |   else Left $  "ERROR: **\{show $ name x}** is not authorised. The allowed "
 229 |             ++ "elements in first position are: **\{show n}** and \{setName}."
 230 |   where
 231 |     checkTail :
 232 |          (listElems : List String)
 233 |       -> (setName : String)
 234 |       -> List Element
 235 |       -> Either String (List Element)
 236 |     checkTail es setName xs = mapM (elemOf es setName) xs
 237 |
 238 | ||| Returns either the elements' list or an error message.
 239 | |||
 240 | ||| An error message is returned if:
 241 | |||
 242 | ||| - the first element is not the optional element or an element of the list;
 243 | ||| - a further element is not an element of the list;
 244 | ||| - the last element is not the optional element or an element of the list;
 245 | ||| - the optional element is the first and last element of the list.
 246 | public export
 247 | firstOrLastOptional :
 248 |      (optionalElem : Name)
 249 |   -> (listElems : List String)
 250 |   -> (setName : String)
 251 |   -> List Element
 252 |   -> Either String (List Element)
 253 | firstOrLastOptional n es setName []        = Right []
 254 | firstOrLastOptional n es setName (x :: xs) =
 255 |   if n == name x || elem (name x) es
 256 |   then Right (x :: !(checkTail (name x) n es setName xs))
 257 |   else Left $  "ERROR: **\{show $ name x}** is not authorised. The allowed "
 258 |             ++ "elements in first position are: **\{show n}** or \{setName}."
 259 |   where
 260 |     checkTail :
 261 |          (firstElem : Name)
 262 |       -> (optionalElem : Name)
 263 |       -> (listElems : List String)
 264 |       -> (setName : String)
 265 |       -> List Element
 266 |       -> Either String (List Element)
 267 |     checkTail first n es setName (x :: []) =
 268 |       if n == name x || elem (name x) es
 269 |       then
 270 |         if first == name x && first == n
 271 |         then Left $  "ERROR: **\{show n}** cannot be the first and last "
 272 |                   ++ "element."
 273 |         else Right [x]
 274 |       else Left $  "ERROR: **\{show $ name x}** is not authorised. The allowed "
 275 |                 ++ "elements in last position are: **\{show n}** or \{setName}."
 276 |     checkTail first n es setName xs        = mapM (elemOf es setName) xs
 277 |
 278 | ||| Ensures that the `Element`s are flow content elements.
 279 | public export
 280 | flowContentElems : List Element -> Either String (List Element)
 281 | flowContentElems = mapM (elemOf flowContent "flow content")
 282 |
 283 | ||| Ensures that the `Element` is a list content element.
 284 | public export
 285 | listContentElems : Element -> Either String Element
 286 | listContentElems = elemOf listContent "list content"
 287 |
 288 | ||| Ensures that the `Element` is a meta data element.
 289 | public export
 290 | meta : Element -> Either String Element
 291 | meta = elemOf metadata "meta data"
 292 |
 293 | ||| Ensures that all `Element`s are phrasing content elements.
 294 | public export
 295 | phrasingContentElems : List Element -> Either String (List Element)
 296 | phrasingContentElems = mapM (elemOf phrasingContent "phrasing content")
 297 |
 298 | ||| Ensures that all `Element`s are flow or phrasing content elements.
 299 | public export
 300 | flowOrPhrasingContentElems : List Element -> Either String (List Element)
 301 | flowOrPhrasingContentElems =
 302 |   mapM (elemOf (flowContent ++ phrasingContent) "flow or phrasing content")
 303 |
 304 | --------------------------------------------------------------------------------
 305 | -- Combined elements and attributes validations
 306 | --------------------------------------------------------------------------------
 307 |
 308 | ||| Ensures that **li** elements have only global attributes.
 309 | |||
 310 | ||| Except for ordered list (**ol**), the **value** attribute has no meaning.
 311 | ||| Consequently, only global attributes are allowed for non-ordered lists
 312 | ||| (**ul**) and menus (**menu**).
 313 | public export
 314 | liGlobalAttr : List Element -> Either String (List Element)
 315 | liGlobalAttr [] = Right []
 316 | liGlobalAttr (x :: xs) =
 317 |   if name x /= "li"
 318 |   then liGlobalAttr xs
 319 |   else
 320 |     case Validation.globalAttributes (attributes x) of
 321 |       Left e  => Left e
 322 |       Right _ => liGlobalAttr xs
 323 |
 324 | ||| Ensures that the first element is **meta** with a **charset** attribute.
 325 | public export
 326 | metaCharset : List Element -> Either String (List Element)
 327 | metaCharset [] = Left $
 328 |       "ERROR: no elements provided."
 329 |   <+> "A **meta** element with **charset** attribute must be the first **head**"
 330 |   <+> "element."
 331 | metaCharset elems@(x :: _) =
 332 |   if name x /= "meta"
 333 |   then Left $
 334 |         "ERROR: **\{name x}** element provided."
 335 |     <+> "A **meta** element with **charset** attribute must be the first **head**"
 336 |     <+> "element."
 337 |   else
 338 |     case Validation.required "charset" (attributes x) of
 339 |       Left _ => Left $
 340 |             "ERROR: **meta** attribute provided without a **charset** "
 341 |         <+> "attribute."
 342 |         <+> "A **meta** element with **charset** attribute must be the first "
 343 |         <+> "**head** element."
 344 |       Right _ => Right elems
 345 |
 346 | --------------------------------------------------------------------------------
 347 | -- Specific validations
 348 | --------------------------------------------------------------------------------
 349 |
 350 | ||| Validates an **a** element.
 351 | |||
 352 | ||| At the exception of **a** elements, accepts:
 353 | |||
 354 | ||| - flow content
 355 | ||| - phrasing content
 356 | ||| - interactive content
 357 | public export
 358 | aElems : List Element -> Either String (List Element)
 359 | aElems elems =
 360 |   mapM
 361 |     ( elemOf
 362 |       (flowContent ++ phrasingContent ++ interactiveContent)
 363 |       "flow, phrasing or interactive content"
 364 |     )
 365 |     elems
 366 |   *> forbiden "a" elems
 367 |
 368 | ||| Validates an **address** element.
 369 | |||
 370 | ||| Accepts flow content except for the below elements:
 371 | |||
 372 | ||| - **address**
 373 | ||| - **hgroup**
 374 | ||| - **h1** to **h6**
 375 | ||| - **article**
 376 | ||| - **aside**
 377 | ||| - **section**
 378 | ||| - **nav**
 379 | ||| - **header**
 380 | ||| - **footer**
 381 | public export
 382 | addressElems : List Element -> Either String (List Element)
 383 | addressElems elems =
 384 |      flowContentElems elems
 385 |   *> forbidens
 386 |        [ "address"
 387 |        , "hgroup"
 388 |        , "h1"
 389 |        , "h2"
 390 |        , "h3"
 391 |        , "h4"
 392 |        , "h5"
 393 |        , "h6"
 394 |        , "article"
 395 |        , "aside"
 396 |        , "section"
 397 |        , "nav"
 398 |        , "header"
 399 |        , "footer"
 400 |        ]
 401 |        elems
 402 |
 403 | ||| Validates an **audio** element.
 404 | |||
 405 | ||| Accepts all content but **audio** or **video**.
 406 | |||
 407 | ||| Note: this is a simplification, as there are other constraints based on
 408 | ||| the presence of a **src* or *source** attribute and the accepted content
 409 | ||| is transparent. For more details, see [MDN documentation][1].
 410 | |||
 411 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/audio
 412 | public export
 413 | audioElems : List Element -> Either String (List Element)
 414 | audioElems elems =
 415 |      forbiden "audio" elems
 416 |   *> forbiden "video" elems
 417 |
 418 | ||| Validates a **canvas** element.
 419 | |||
 420 | ||| Accepts all content but interactive content and the **button** element.
 421 | |||
 422 | ||| Note: this is a simplification, as this is a transparent element. Also,
 423 | ||| interactive content includes other elements depending on the presence of
 424 | ||| certain attributes. More details can be found in [MDN documentation][1].
 425 | |||
 426 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Guides/Content_categories#interactive_content
 427 | public export
 428 | canvasElems : List Element -> Either String (List Element)
 429 | canvasElems elems =
 430 |   forbidens
 431 |     [ "details"
 432 |     , "embed"
 433 |     , "iframe"
 434 |     , "label"
 435 |     , "select"
 436 |     , "textarea"
 437 |     ] elems
 438 |
 439 | ||| Validates a **colgroup** element.
 440 | |||
 441 | ||| Accepts only **col** elements.
 442 | |||
 443 | ||| Note: this is a simplification, as **col** elements are not accepted if
 444 | ||| a **span** attribute is present in the **colgroup** element. More details
 445 | ||| can be found in [MDN documentation][1].
 446 | |||
 447 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/colgroup
 448 | public export
 449 | colgroupElems : List Element -> Either String (List Element)
 450 | colgroupElems elems = mapM (is "col") elems
 451 |
 452 | ||| Validates a **footer** element.
 453 | |||
 454 | ||| Accepts flow content except for the **header** and **footer** elements.
 455 | public export
 456 | footerElems : List Element -> Either String (List Element)
 457 | footerElems elems =
 458 |      flowContentElems elems
 459 |   *> forbiden "header" elems
 460 |   *> forbiden "footer" elems
 461 |
 462 | ||| Validates a **datalist** element.
 463 | |||
 464 | ||| Accepts phrasing content or **option** elements.
 465 | public export
 466 | datalistElems : List Element -> Either String (List Element)
 467 | datalistElems =
 468 |   mapM (elemOf ("option" :: phrasingContent) "phrasing content or option")
 469 |
 470 | ||| Validates a **details** element.
 471 | |||
 472 | ||| Accepts a **summary** element followed by flow content.
 473 | public export
 474 | detailsElems : List Element -> Either String (List Element)
 475 | detailsElems elems =
 476 |      first "summary" elems
 477 |   *> requireds flowContent elems
 478 |
 479 | ||| Validates a **dfn** element.
 480 | |||
 481 | ||| Accepts phrasing content except for the **dfn** element.
 482 | public export
 483 | dfnElems : List Element -> Either String (List Element)
 484 | dfnElems elems =
 485 |      phrasingContentElems elems
 486 |   *> forbiden "dfn" elems
 487 |
 488 | ||| Validates a **div** element.
 489 | |||
 490 | ||| Accepts flow content as well as the below additional elements:
 491 | |||
 492 | ||| - **dt**
 493 | ||| - *dd**
 494 | ||| - **script**
 495 | ||| - **template**.
 496 | |||
 497 | ||| Note: this is a simplification, as normally those additional elements can
 498 | ||| only be present if the parent is a **dl** element. Also, the **script** and
 499 | ||| are intermixed with **dd** elements. See [MDN documentation][1] for more
 500 | ||| details.
 501 | |||
 502 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/div
 503 | public export
 504 | divElems : List Element -> Either String (List Element)
 505 | divElems elems =
 506 |   let content = flowContent ++ ["dt", "dd", "script", "template"] in
 507 |   mapM
 508 |     (elemOf content "flow content, **dt**, **dd**, **script**, or **template")
 509 |     elems
 510 |
 511 | ||| Validates a **dl** element.
 512 | |||
 513 | ||| Accepts **dt**, *dd**, **script** or **template** elements.
 514 | |||
 515 | ||| This is a supplication. See [MDN documentation][1] for more details.
 516 | |||
 517 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/dl
 518 | public export
 519 | dlElems : List Element -> Either String (List Element)
 520 | dlElems elems =
 521 |   let content = ["dt", "dd", "script", "template"] in
 522 |   mapM (elemOf content "**dt**, **dd**, **script**, or **template") elems
 523 |
 524 | ||| Validates a **dt** element.
 525 | |||
 526 | ||| Accepts flow content except for the below:
 527 | |||
 528 | ||| - **header** element
 529 | ||| - **footer** element
 530 | ||| - sectioning content
 531 | ||| - heading content.
 532 | public export
 533 | dtElems : List Element -> Either String (List Element)
 534 | dtElems elems =
 535 |      flowContentElems elems
 536 |   *> forbidens
 537 |        [ "header"
 538 |        , "footer"
 539 |        , "article"
 540 |        , "aside"
 541 |        , "nav"
 542 |        , "section"
 543 |        , "h1"
 544 |        , "h2"
 545 |        , "h3"
 546 |        , "h4"
 547 |        , "h5"
 548 |        , "h6"
 549 |        , "hgroup"
 550 |        ] elems
 551 |
 552 | ||| Validates a **fieldset** element.
 553 | |||
 554 | ||| Accepts an optional **legend** followed by flow content.
 555 | public export
 556 | fieldsetElems : List Element -> Either String (List Element)
 557 | fieldsetElems = firstOptional "legend" flowContent "flow content"
 558 |
 559 | ||| Validates a **figure** element.
 560 | |||
 561 | ||| Accepts an optional **figcaption** element which must either be at the
 562 | ||| beginning or the end of the children list.
 563 | ||| The other children must be flow content elements.
 564 | public export
 565 | figureElems : List Element -> Either String (List Element)
 566 | figureElems = firstOrLastOptional "figcaption" flowContent "flow content"
 567 |
 568 | ||| Validates a **form** element.
 569 | |||
 570 | ||| Accepts flow content but no **form** element.
 571 | public export
 572 | formElems : List Element -> Either String (List Element)
 573 | formElems elems =
 574 |      flowContentElems elems
 575 |   *> forbiden "form" elems
 576 |
 577 | ||| Validates a **head** element.
 578 | |||
 579 | ||| Accepts meta elements and one **title** element.
 580 | ||| Also, the first element must be **meta** with a **charset** attribute.
 581 | public export
 582 | headElems : List Element -> Either String (List Element)
 583 | headElems elems =
 584 |      mapM meta elems
 585 |   *> metaCharset elems
 586 |   *> count "title" 1 elems
 587 |
 588 | ||| Validates a **header** element.
 589 | |||
 590 | ||| Accepts flow content except for **header** or **footer** elements.
 591 | public export
 592 | headerElems : List Element -> Either String (List Element)
 593 | headerElems elems =
 594 |      flowContentElems elems
 595 |   *> forbiden "header" elems
 596 |   *> forbiden "footer" elems
 597 |
 598 | ||| Validates a **hgroup** element.
 599 | |||
 600 | ||| Accepts zero or more **p** elements followed by one **h1** to **h6**
 601 | ||| element, followed by zero or more **p** elements.
 602 | public export
 603 | hgroupElems : List Element -> Either String (List Element)
 604 | hgroupElems elems =
 605 |   let
 606 |     sectionHeading : List Name
 607 |     sectionHeading = ["h1", "h2", "h3", "h4", "h5", "h6"]
 608 |   in
 609 |   case view (names elems) (Right ([], [], [])) of
 610 |     Left e                  => Left e
 611 |     Right (_, []      , _)  => Left "ERROR: at least one **h1** to **h6** element must be provided."
 612 |     Right (_, (h :: _), _)  => if elem h sectionHeading
 613 |                                then Right elems
 614 |                                else Left $
 615 |                                "ERROR: **\{show h}** provided. "
 616 |                             ++ "There can be only one **h1** to **h6** element."
 617 |   where
 618 |     (<+>) :
 619 |          Either String (List Name, List Name, List Name)
 620 |       -> Either String (List Name, List Name, List Name)
 621 |       -> Either String (List Name, List Name, List Name)
 622 |     Left e             <+> _                  = Left e
 623 |     _                  <+> Left e             = Left e
 624 |     Right (x1, x2, x3) <+> Right (y1, y2, y3) = Right
 625 |                                                   (x1 ++ y1, x2 ++ y2, x3 ++ y3)
 626 |
 627 |     view :
 628 |          List Name
 629 |       -> Either String (List Name, List Name, List Name)
 630 |       -> Either String (List Name, List Name, List Name)
 631 |     view _           (Left e)             = Left e
 632 |     view []          (Right acc)          = Right acc
 633 |     view ("p" :: xs) (Right (a1, [], [])) = view xs $ Right ("p" :: a1, [], [])
 634 |     view (e   :: xs) (Right (a1, [], [])) = view xs $ Right (a1, [e], [])
 635 |     view ("p" :: xs) (Right (a1, a2, a3)) = view xs $ Right (a1, a2, "p" :: a3)
 636 |     view (e   :: xs) (Right (a1, a2, [])) =
 637 |       Left $ "ERROR: **\{show e}** provided. "
 638 |         ++ "There can be only one **h1** to **h6** element."
 639 |     view (e   :: xs) (Right (a1, a2, a3)) =
 640 |       Left
 641 |         "ERROR: \{show e} provided. **h1** to **h6** element already present."
 642 |
 643 | ||| Validates the **html** element.
 644 | |||
 645 | ||| Accepts one **head** element followed by a **body** element.
 646 | public export
 647 | htmlElems : List Element -> Either String (List Element)
 648 | htmlElems elems = (first "head" elems >>= second "body" >>= length 2)
 649 |
 650 | ||| Validates a **label** element.
 651 | |||
 652 | ||| Accepts phrasing content except for **label** elements.
 653 | |||
 654 | ||| Note: it is a simplification, as actually no labelable elements other than
 655 | ||| the labeled control are allowed.
 656 | ||| See [MDN documentation][1] for more details.
 657 | |||
 658 | ||| [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/label
 659 | public export
 660 | labelElems : List Element -> Either String (List Element)
 661 | labelElems elems = phrasingContentElems elems *> forbiden "label" elems
 662 |
 663 | ||| Validates a **legend** element.
 664 | |||
 665 | ||| Accepts phrasing content as well as **h1** to **h6** elements.
 666 | public export
 667 | legendElems : List Element -> Either String (List Element)
 668 | legendElems elems =
 669 |   let legendContent = phrasingContent ++
 670 |     [ "h1"
 671 |     , "h2"
 672 |     , "h3"
 673 |     , "h4"
 674 |     , "h5"
 675 |     , "h6"
 676 |     ]
 677 |   in
 678 |   mapM (elemOf legendContent "phrasing content or **h1** to **h6**") elems
 679 |
 680 | ||| Validates a **meter** element.
 681 | |||
 682 | ||| Accepts phrasing content except for **meter** elements.
 683 | public export
 684 | meterElems : List Element -> Either String (List Element)
 685 | meterElems elems = phrasingContentElems elems *> forbiden "meter" elems
 686 |
 687 | ||| Validates a **noscript** element.
 688 | |||
 689 | ||| Accepts flow content, phrasing content as well as **link**, **style** and
 690 | ||| **meta** elements.
 691 | |||
 692 | ||| Note: it is a simplification, as the allowed content depends on the parent
 693 | ||| of the **noscript** element. See [MDN documentation][1] for more details.
 694 | |||
 695 | ||| [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/noscript
 696 | public export
 697 | noscriptElems : List Element -> Either String (List Element)
 698 | noscriptElems elems =
 699 |   let
 700 |     noscriptContent =
 701 |       flowContent ++ phrasingContent ++ ["link" , "style" , "meta"]
 702 |     msg = "flow content, phrasing content, **link**, **style** or **meta**"
 703 |   in
 704 |   mapM (elemOf noscriptContent msg) elems
 705 |
 706 | ||| Validates the **object** element.
 707 | |||
 708 | ||| Accepts zero or more **param** elements followed by flow or phrasing
 709 | ||| content.
 710 | |||
 711 | ||| Note: it is a simplification, as normally the allowed content is
 712 | ||| transparent. See [MDN documentation][1] for more details.
 713 | |||
 714 | ||| [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/object
 715 | public export
 716 | objectElems : List Element -> Either String (List Element)
 717 | objectElems = firstOptional "param" flowContent "flow or phrasing content"
 718 |
 719 | ||| Validates the **optgroup** element.
 720 | |||
 721 | ||| Accepts zero or more **option** elements.
 722 | |||
 723 | ||| Note: no **legend** element is authorised as customizable select elements is
 724 | ||| not considered as "baseline" due to the lack of support by some browsers.
 725 | ||| See [MDN documentation][1] for more details.
 726 | |||
 727 | ||| [1]: https://developer.mozilla.org/docs/Learn_web_development/Extensions/Forms/Customizable_select
 728 | public export
 729 | optgroupElems : List Element -> Either String (List Element)
 730 | optgroupElems = mapM (is "option")
 731 |
 732 | ||| Validates the **picture** element.
 733 | |||
 734 | ||| Accepts zero or more **source** elements followed by one **img** element.
 735 | ||| Those elements can be mixed with script-supporting elements.
 736 | public export
 737 | pictureElems : List Element -> Either String (List Element)
 738 | pictureElems elems =
 739 |      count "img" 1 elems
 740 |   *> check elems
 741 |   where
 742 |     check : List Element -> Either String (List Element)
 743 |     check []          = Right []
 744 |     check l@(x :: xs) =
 745 |       case name x of
 746 |         "img"      => mapM
 747 |                         ( elemOf
 748 |                             ["script", "template"]
 749 |                             "**script** or **template**"
 750 |                         )
 751 |                         xs
 752 |         "source"   => check xs
 753 |         "script"   => check xs
 754 |         "template" => check xs
 755 |         n          => mapM
 756 |                         ( elemOf
 757 |                             ["img", "source", "script", "template"]
 758 |                             "**source**, **img**, **script** or **template**"
 759 |                         )
 760 |                         l
 761 |
 762 | ||| Accepts only plain text.
 763 | |||
 764 | ||| Used for the validation of the **option** and **rp** elements.
 765 | public export
 766 | plaintextElems : List Element -> Either String (List Element)
 767 | plaintextElems = mapM (is "plaintext")
 768 |
 769 | ||| Validates a **progress** element.
 770 | |||
 771 | ||| Accepts phrasing content elements, except **progress** elements.
 772 | public export
 773 | progressElems : List Element -> Either String (List Element)
 774 | progressElems elems = phrasingContentElems elems *> forbiden "progress" elems
 775 |
 776 | ||| Validates a **ruby** element.
 777 | |||
 778 | ||| Accepts phrasing content as well as **rp** and **rt** elements.
 779 | |||
 780 | ||| Note: it is a simplification, as the allowed content is in reality one or
 781 | ||| more groups consisting of two parts. See [MDN documentation][1] for more
 782 | ||| details.
 783 | |||
 784 | ||| [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/ruby
 785 | public export
 786 | rubyElems : List Element -> Either String (List Element)
 787 | rubyElems elems =
 788 |   let
 789 |     rubyContent = phrasingContent ++ ["rp" , "rt"]
 790 |     msg = "phrasing content, **rp** or **rt**"
 791 |   in
 792 |   mapM (elemOf rubyContent msg) elems
 793 |
 794 | ||| Validates a **ul** element.
 795 | |||
 796 | ||| Accepts list content elements. Also, the list elements within **ul** can
 797 | ||| only have global attributes.
 798 | public export
 799 | ulElems : List Element -> Either String (List Element)
 800 | ulElems elems =
 801 |      mapM listContentElems elems
 802 |   *> liGlobalAttr elems
 803 |
 804 | ||| Validates a **script** element.
 805 | |||
 806 | ||| Accepts only script content.
 807 | public export
 808 | scriptElems : List Element -> Either String (List Element)
 809 | scriptElems = mapM (is "scriptContent")
 810 |
 811 | ||| Validates a **select** element.
 812 | |||
 813 | ||| Accept only **option**, **optgroup**, **hr**, **button**, **div**,
 814 | ||| **script**, **template**, **noscript** or **select** elements.
 815 | |||
 816 | ||| Also, there can be no more than one **button** element.
 817 | |||
 818 | ||| Note: this is a simplification, as the **button** must precede the
 819 | ||| **option**, **optgroup** and **hr** elements. See [MDN documention][1] for
 820 | ||| more details.
 821 | |||
 822 | ||| [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/select
 823 | public export
 824 | selectElems : List Element -> Either String (List Element)
 825 | selectElems elems =
 826 |      max "button" 1 elems
 827 |   *> mapM (elemOf
 828 |           [ "option"
 829 |           , "optgroup"
 830 |           , "hr"
 831 |           , "button"
 832 |           , "div"
 833 |           , "script"
 834 |           , "template"
 835 |           , "noscript"
 836 |           ] "select"
 837 |        ) elems
 838 |
 839 | ||| Validates a **slot** element.
 840 | |||
 841 | ||| Accepts flow or phrasing content.
 842 | |||
 843 | ||| Note: this is a simplification as the content is transparent.
 844 | public export
 845 | slotElems : List Element -> Either String (List Element)
 846 | slotElems =
 847 |   mapM (elemOf (flowContent ++ phrasingContent) "flow or phrasing content")
 848 |
 849 | ||| Validates a **style** element.
 850 | |||
 851 | ||| Accepts only CSS content.
 852 | public export
 853 | styleElems : List Element -> Either String (List Element)
 854 | styleElems = mapM (is "cssContent")
 855 |
 856 | ||| Validates a **summary** element.
 857 | |||
 858 | ||| Accepts phrasing or heading content.
 859 | public export
 860 | summaryElems : List Element -> Either String (List Element)
 861 | summaryElems = mapM
 862 |   (elemOf (phrasingContent ++ headingContent) "phrasing or heading content")
 863 |
 864 | ||| Validates a **table** element.
 865 | |||
 866 | ||| Accepts:
 867 | |||
 868 | ||| - first, an optional **caption** element;
 869 | ||| - then, zero ore more **colgroup** elements;
 870 | ||| - then, an optional **thead** element;
 871 | ||| - then, either one or more:
 872 | |||   - **tbody** elements; or
 873 | |||   - **tr** elements; or
 874 | ||| - then, an optional **tfoot** element.
 875 | public export
 876 | tableElems : List Element -> Either String (List Element)
 877 | tableElems elems = check (names elems) elems
 878 |   where
 879 |     checkTbody : List String -> List Element -> Either String (List Element)
 880 |     checkTbody ("tbody" :: xs) es = checkTbody xs es
 881 |     checkTbody ("tfoot" :: []) es = Right es
 882 |     checkTbody []              es = Right es
 883 |     checkTbody _               _  = Left $
 884 |           "ERROR: the last element of a table must be a **tbody** or **tfoot** "
 885 |       <+> "element, further elements provided."
 886 |
 887 |     checkTr : List String -> List Element -> Either String (List Element)
 888 |     checkTr ("tr"    :: xs) es = checkTbody xs es
 889 |     checkTr ("tfoot" :: []) es = Right es
 890 |     checkTr []              es = Right es
 891 |     checkTr _               _  = Left $
 892 |           "ERROR: the last element of a table must be a **tr** or **tfoot** "
 893 |       <+> "element, further elements provided."
 894 |
 895 |     checkCore : List String -> List Element -> Either String (List Element)
 896 |     checkCore ("tbody" :: xs) es = checkTbody xs es
 897 |     checkCore ("tr"    :: xs) es = checkTr xs es
 898 |     checkCore (x       :: _)  _  = Left $
 899 |           "ERROR: the core of a **table** must be either **tbody** or **tr**"
 900 |       <+> "elements. **\{x}** provided."
 901 |     checkCore []              _ = Left $
 902 |           "ERROR: the core of a **table** must be either **tbody** or **tr**"
 903 |       <+> "elements. Nothing provided."
 904 |
 905 |     checkColgroup : List String -> List Element -> Either String (List Element)
 906 |     checkColgroup ("colgroup" :: xs) es = checkColgroup xs es
 907 |     checkColgroup ("thead"    :: xs) es = checkCore xs es
 908 |     checkColgroup ("tbody"    :: xs) es = checkTbody xs es
 909 |     checkColgroup ("tr"       :: xs) es = checkTr xs es
 910 |     checkColgroup (x          :: _ ) _  = Left $
 911 |           "ERROR: the second element of a **table** must be either a"
 912 |       <+> ", **colgroup**, **thead**, **tbody** or **tr** element. **\{x}** "
 913 |       <+> "provided."
 914 |     checkColgroup []                 _  = Left $
 915 |           "ERROR: the second element of a **table** must be either a "
 916 |       <+> ", **colgroup**, **thead**, **tbody** or **tr** element. Nothing "
 917 |       <+> "provided."
 918 |
 919 |     check : List String -> List Element -> Either String (List Element)
 920 |     check ("caption"  :: xs) es = checkColgroup xs es
 921 |     check ("colgroup" :: xs) es = checkColgroup xs es
 922 |     check ("thead"    :: xs) es = checkCore xs es
 923 |     check ("tbody"    :: xs) es = checkTbody xs es
 924 |     check ("tr"       :: xs) es = checkTr xs es
 925 |     check (x          :: _ ) _  = Left $
 926 |           "ERROR: the first element of a **table** must be either a **caption**"
 927 |       <+> ", **colgroup**, **thead**, **tbody** or **tr** element. **\{x}** "
 928 |       <+> "provided."
 929 |     check []                 _  = Left $
 930 |           "ERROR: the first element of a **table** must be either a **caption**"
 931 |       <+> ", **colgroup**, **thead**, **tbody** or **tr** element. Nothing "
 932 |       <+> "provided."
 933 |
 934 | ||| Validates a **tbody** element.
 935 | |||
 936 | ||| Accepts only **tr** elements.
 937 | public export
 938 | tbodyElems : List Element -> Either String (List Element)
 939 | tbodyElems = mapM (is "tr")
 940 |
 941 | ||| Validates a **template** element.
 942 | |||
 943 | ||| Accepts all elements except **html**, **head** and **body**.
 944 | public export
 945 | templateElems : List Element -> Either String (List Element)
 946 | templateElems = forbidens ["html" , "head" , "body"]
 947 |
 948 | ||| Validates a **th** element.
 949 | |||
 950 | ||| Accepts flow content elements except **header** or **footer** elements or
 951 | ||| heading or sectioning content.
 952 | public export
 953 | thElems : List Element -> Either String (List Element)
 954 | thElems = forbidens $
 955 |      headingContent
 956 |   ++ sectioningContent
 957 |   ++ [ "header"
 958 |      , "footer"
 959 |      ]
 960 |
 961 | ||| Validates a **tr** element.
 962 | |||
 963 | ||| Accepts **th**, **td**, **script** or **template** elements
 964 | public export
 965 | trElems : List Element -> Either String (List Element)
 966 | trElems = mapM (elemOf ["th" , "td" , "script" , "template"] "tr")
 967 |
 968 | ||| Validates a **video** element.
 969 | |||
 970 | ||| If the **video** element has a **src** attribute, accepts:
 971 | |||
 972 | ||| - zero or more **source** elements; and then
 973 | ||| - zero or more **track** elements; and then
 974 | ||| - flow, phrasing, embedded or interactive content.
 975 | |||
 976 | ||| None of the above can contain **video** or **audio** elements.
 977 | |||
 978 | ||| Note: this is a simplification, as this element can be transparent and
 979 | ||| the allowed elements depend on its attributes.
 980 | public export
 981 | videoElems : List Element -> Either String (List Element)
 982 | videoElems elems =
 983 |   case checkSource (names elems) elems *> forbidens ["audio", "video"] elems of
 984 |     Right _ => Right elems
 985 |     e       => e
 986 |   where
 987 |     checkTail : List Element -> Either String (List Element)
 988 |     checkTail es = mapM (elemOf
 989 |         (  flowContent
 990 |         ++ phrasingContent
 991 |         ++ embeddedContent
 992 |         ++ interactiveContent
 993 |         )
 994 |         "video content"
 995 |       ) es
 996 |
 997 |     checkTrack : List String -> List Element -> Either String (List Element)
 998 |     checkTrack ("track" :: []) _          = Right []
 999 |     checkTrack ("track" :: xs) (e :: es)  = checkTrack xs es
1000 |     checkTrack _               es         = checkTail es
1001 |
1002 |     checkSource : List String -> List Element -> Either String (List Element)
1003 |     checkSource ("source" :: []) es        = Right []
1004 |     checkSource ("source" :: xs) (e :: es) = checkSource xs es
1005 |     checkSource ("track"  :: []) (e :: es) = Right es
1006 |     checkSource ("track"  :: xs) (e :: es) = checkTrack xs es
1007 |     checkSource _                es        = checkTail es
1008 |