6 | module Text.Html.Elements.Validation
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
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
31 | requireds' : List Name -> List Element -> Bool
32 | requireds' [] _ = False
33 | requireds' _ [] = False
34 | requireds' (n :: ns) xs =
37 | else requireds' ns xs
44 | required : Name -> List Element -> Either String (List Element)
48 | else Left $
"ERROR: **\{n}** element is missing."
55 | requireds : List Name -> List Element -> Either String (List Element)
59 | else Left $
"ERROR: at least one of the following element must be present: "
60 | ++ "**\{show ns}**."
63 | forbiden : Name -> List Element -> Either String (List Element)
66 | then Left $
"ERROR: **\{n}** element is not allowed."
70 | forbidens : List Name -> List Element -> Either String (List Element)
74 | $
"ERROR: one of the provided element **\{show ns}** is not allowed."
82 | first : Name -> List Element -> Either String (List Element)
83 | first n [] = Left $
"ERROR: **\{n}** element is missing."
84 | first n elems@(x :: _) =
87 | else Left $
"ERROR: **\{n}** element is missing in first position."
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 :: _) =
100 | else Left $
"ERROR: **\{n}** element is missing in first position."
107 | length : (length : Nat) -> List Element -> Either String (List Element)
109 | let l' = List.length xs in
113 | "ERROR: expected length \{natToStr l}, got \{natToStr l}."
122 | (Nat -> Nat -> Bool)
126 | -> Either String (List Element)
127 | compareWith op nm cnt elems =
128 | let result = countHelper 0 nm elems in
132 | "ERROR: **\{nm}** element expected \{natToStr cnt} times, "
133 | <+> "got \{natToStr result}."
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
146 | count : Name -> (count : Nat) -> List Element -> Either String (List Element)
147 | count = compareWith (==)
154 | max : Name -> (count : Nat) -> List Element -> Either String (List Element)
155 | max = compareWith (<=)
159 | is : (name : String) -> Element -> Either String Element
164 | "ERROR: element **\{name el}** is not the expected **\{n}** element."
173 | -> (listName : String)
175 | -> Either String Element
176 | elemOf xs listName el =
181 | "ERROR: element **\{n}** is not a \{listName} element. "
182 | <+> "The \{listName} elements are: \{joinBy ", " xs}."
192 | (optionalElem : Name)
193 | -> (listElems : List String)
194 | -> (setName : String)
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}."
205 | (listElems : List String)
206 | -> (setName : String)
208 | -> Either String (List Element)
209 | checkTail es setName xs = mapM (elemOf es setName) xs
219 | (optionalElem : Name)
220 | -> (listElems : List String)
221 | -> (setName : String)
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}."
232 | (listElems : List String)
233 | -> (setName : String)
235 | -> Either String (List Element)
236 | checkTail es setName xs = mapM (elemOf es setName) xs
247 | firstOrLastOptional :
248 | (optionalElem : Name)
249 | -> (listElems : List String)
250 | -> (setName : String)
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}."
262 | -> (optionalElem : Name)
263 | -> (listElems : List String)
264 | -> (setName : String)
266 | -> Either String (List Element)
267 | checkTail first n es setName (x :: []) =
268 | if n == name x || elem (name x) es
270 | if first == name x && first == n
271 | then Left $
"ERROR: **\{show n}** cannot be the first and last "
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
280 | flowContentElems : List Element -> Either String (List Element)
281 | flowContentElems = mapM (elemOf flowContent "flow content")
285 | listContentElems : Element -> Either String Element
286 | listContentElems = elemOf listContent "list content"
290 | meta : Element -> Either String Element
291 | meta = elemOf metadata "meta data"
295 | phrasingContentElems : List Element -> Either String (List Element)
296 | phrasingContentElems = mapM (elemOf phrasingContent "phrasing content")
300 | flowOrPhrasingContentElems : List Element -> Either String (List Element)
301 | flowOrPhrasingContentElems =
302 | mapM (elemOf (flowContent ++ phrasingContent) "flow or phrasing content")
314 | liGlobalAttr : List Element -> Either String (List Element)
315 | liGlobalAttr [] = Right []
316 | liGlobalAttr (x :: xs) =
318 | then liGlobalAttr xs
320 | case Validation.globalAttributes (attributes x) of
322 | Right _ => liGlobalAttr xs
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**"
331 | metaCharset elems@(x :: _) =
332 | if name x /= "meta"
334 | "ERROR: **\{name x}** element provided."
335 | <+> "A **meta** element with **charset** attribute must be the first **head**"
338 | case Validation.required "charset" (attributes x) of
340 | "ERROR: **meta** attribute provided without a **charset** "
342 | <+> "A **meta** element with **charset** attribute must be the first "
343 | <+> "**head** element."
344 | Right _ => Right elems
358 | aElems : List Element -> Either String (List Element)
362 | (flowContent ++ phrasingContent ++ interactiveContent)
363 | "flow, phrasing or interactive content"
366 | *> forbiden "a" elems
382 | addressElems : List Element -> Either String (List Element)
383 | addressElems elems =
384 | flowContentElems elems
413 | audioElems : List Element -> Either String (List Element)
415 | forbiden "audio" elems
416 | *> forbiden "video" elems
428 | canvasElems : List Element -> Either String (List Element)
429 | canvasElems elems =
449 | colgroupElems : List Element -> Either String (List Element)
450 | colgroupElems elems = mapM (is "col") elems
456 | footerElems : List Element -> Either String (List Element)
457 | footerElems elems =
458 | flowContentElems elems
459 | *> forbiden "header" elems
460 | *> forbiden "footer" elems
466 | datalistElems : List Element -> Either String (List Element)
468 | mapM (elemOf ("option" :: phrasingContent) "phrasing content or option")
474 | detailsElems : List Element -> Either String (List Element)
475 | detailsElems elems =
476 | first "summary" elems
477 | *> requireds flowContent elems
483 | dfnElems : List Element -> Either String (List Element)
485 | phrasingContentElems elems
486 | *> forbiden "dfn" elems
504 | divElems : List Element -> Either String (List Element)
506 | let content = flowContent ++ ["dt", "dd", "script", "template"] in
508 | (elemOf content "flow content, **dt**, **dd**, **script**, or **template")
519 | dlElems : List Element -> Either String (List Element)
521 | let content = ["dt", "dd", "script", "template"] in
522 | mapM (elemOf content "**dt**, **dd**, **script**, or **template") elems
533 | dtElems : List Element -> Either String (List Element)
535 | flowContentElems elems
556 | fieldsetElems : List Element -> Either String (List Element)
557 | fieldsetElems = firstOptional "legend" flowContent "flow content"
565 | figureElems : List Element -> Either String (List Element)
566 | figureElems = firstOrLastOptional "figcaption" flowContent "flow content"
572 | formElems : List Element -> Either String (List Element)
574 | flowContentElems elems
575 | *> forbiden "form" elems
582 | headElems : List Element -> Either String (List Element)
585 | *> metaCharset elems
586 | *> count "title" 1 elems
592 | headerElems : List Element -> Either String (List Element)
593 | headerElems elems =
594 | flowContentElems elems
595 | *> forbiden "header" elems
596 | *> forbiden "footer" elems
603 | hgroupElems : List Element -> Either String (List Element)
604 | hgroupElems elems =
606 | sectionHeading : List Name
607 | sectionHeading = ["h1", "h2", "h3", "h4", "h5", "h6"]
609 | case view (names elems) (Right ([], [], [])) of
611 | Right (_, [] , _) => Left "ERROR: at least one **h1** to **h6** element must be provided."
612 | Right (_, (h :: _), _) => if elem h sectionHeading
615 | "ERROR: **\{show h}** provided. "
616 | ++ "There can be only one **h1** to **h6** element."
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)
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)) =
641 | "ERROR: \{show e} provided. **h1** to **h6** element already present."
647 | htmlElems : List Element -> Either String (List Element)
648 | htmlElems elems = (first "head" elems >>= second "body" >>= length 2)
660 | labelElems : List Element -> Either String (List Element)
661 | labelElems elems = phrasingContentElems elems *> forbiden "label" elems
667 | legendElems : List Element -> Either String (List Element)
668 | legendElems elems =
669 | let legendContent = phrasingContent ++
678 | mapM (elemOf legendContent "phrasing content or **h1** to **h6**") elems
684 | meterElems : List Element -> Either String (List Element)
685 | meterElems elems = phrasingContentElems elems *> forbiden "meter" elems
697 | noscriptElems : List Element -> Either String (List Element)
698 | noscriptElems elems =
701 | flowContent ++ phrasingContent ++ ["link" , "style" , "meta"]
702 | msg = "flow content, phrasing content, **link**, **style** or **meta**"
704 | mapM (elemOf noscriptContent msg) elems
716 | objectElems : List Element -> Either String (List Element)
717 | objectElems = firstOptional "param" flowContent "flow or phrasing content"
729 | optgroupElems : List Element -> Either String (List Element)
730 | optgroupElems = mapM (is "option")
737 | pictureElems : List Element -> Either String (List Element)
738 | pictureElems elems =
739 | count "img" 1 elems
742 | check : List Element -> Either String (List Element)
743 | check [] = Right []
744 | check l@(x :: xs) =
748 | ["script", "template"]
749 | "**script** or **template**"
752 | "source" => check xs
753 | "script" => check xs
754 | "template" => check xs
757 | ["img", "source", "script", "template"]
758 | "**source**, **img**, **script** or **template**"
766 | plaintextElems : List Element -> Either String (List Element)
767 | plaintextElems = mapM (is "plaintext")
773 | progressElems : List Element -> Either String (List Element)
774 | progressElems elems = phrasingContentElems elems *> forbiden "progress" elems
786 | rubyElems : List Element -> Either String (List Element)
789 | rubyContent = phrasingContent ++ ["rp" , "rt"]
790 | msg = "phrasing content, **rp** or **rt**"
792 | mapM (elemOf rubyContent msg) elems
799 | ulElems : List Element -> Either String (List Element)
801 | mapM listContentElems elems
802 | *> liGlobalAttr elems
808 | scriptElems : List Element -> Either String (List Element)
809 | scriptElems = mapM (is "scriptContent")
824 | selectElems : List Element -> Either String (List Element)
825 | selectElems elems =
826 | max "button" 1 elems
845 | slotElems : List Element -> Either String (List Element)
847 | mapM (elemOf (flowContent ++ phrasingContent) "flow or phrasing content")
853 | styleElems : List Element -> Either String (List Element)
854 | styleElems = mapM (is "cssContent")
860 | summaryElems : List Element -> Either String (List Element)
861 | summaryElems = mapM
862 | (elemOf (phrasingContent ++ headingContent) "phrasing or heading content")
876 | tableElems : List Element -> Either String (List Element)
877 | tableElems elems = check (names elems) elems
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."
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."
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."
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}** "
914 | checkColgroup [] _ = Left $
915 | "ERROR: the second element of a **table** must be either a "
916 | <+> ", **colgroup**, **thead**, **tbody** or **tr** element. Nothing "
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}** "
929 | check [] _ = Left $
930 | "ERROR: the first element of a **table** must be either a **caption**"
931 | <+> ", **colgroup**, **thead**, **tbody** or **tr** element. Nothing "
938 | tbodyElems : List Element -> Either String (List Element)
939 | tbodyElems = mapM (is "tr")
945 | templateElems : List Element -> Either String (List Element)
946 | templateElems = forbidens ["html" , "head" , "body"]
953 | thElems : List Element -> Either String (List Element)
954 | thElems = forbidens $
956 | ++ sectioningContent
965 | trElems : List Element -> Either String (List Element)
966 | trElems = mapM (elemOf ["th" , "td" , "script" , "template"] "tr")
981 | videoElems : List Element -> Either String (List Element)
983 | case checkSource (names elems) elems *> forbidens ["audio", "video"] elems of
984 | Right _ => Right elems
987 | checkTail : List Element -> Either String (List Element)
988 | checkTail es = mapM (elemOf
992 | ++ interactiveContent
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
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