0 | module Text.Html.Elements.Types
  1 |
  2 | import Data.Either
  3 | import Data.String
  4 | import Derive.Prelude
  5 |
  6 | import Text.Html.Attributes.Types
  7 | import Text.Html.Attributes.Validation
  8 | import Text.Html.Types
  9 | import Text.Html.Utils
 10 |
 11 | %hide Language.Reflection.TT.Name
 12 |
 13 | %default total
 14 | %language ElabReflection
 15 |
 16 | --------------------------------------------------------------------------------
 17 | -- Utils
 18 | --------------------------------------------------------------------------------
 19 |
 20 | ||| Joins lists.
 21 | |||
 22 | ||| Using the `>>` operator allows the use of the `do` syntax.
 23 | public export
 24 | (>>) : List a -> List a -> List a
 25 | (>>) = (<+>)
 26 |
 27 | namespace Either
 28 |
 29 |   ||| Joins Either.
 30 |   |||
 31 |   ||| Using the `>>` operator allows the use of the `do` syntax.
 32 |   public export
 33 |   (>>) :
 34 |        Either String (List a)
 35 |     -> Either String (List a)
 36 |     -> Either String (List a)
 37 |   (>>) (Left str) _          = Left str
 38 |   (>>) _          (Left str) = Left str
 39 |   (>>) (Right xs) (Right ys) = Right (xs <+> ys)
 40 |
 41 | --------------------------------------------------------------------------------
 42 | -- Types
 43 | --------------------------------------------------------------------------------
 44 |
 45 | export
 46 | data EscapedText = Escaped String
 47 |
 48 | public export
 49 | Eq EscapedText where
 50 |   (Escaped x) == (Escaped y) = x == y
 51 |
 52 | public export
 53 | Show EscapedText where
 54 |   show (Escaped str) = str
 55 |
 56 | ||| Escape a `String` for use a attribute's value.
 57 | |||
 58 | ||| The following characters are escaped: '"', '\'', '&' and '<'.
 59 | ||| This function is the only way to construct a `EscapedValue` as the
 60 | ||| `EscapedValue` constructor is kept private.
 61 | export
 62 | escape : String -> EscapedText
 63 | escape = Escaped . pack . escapeHelper . unpack
 64 |   where
 65 |     escapeHelper : List Char -> List Char
 66 |     escapeHelper []           = []
 67 |     escapeHelper ('"' :: cs)  = unpack "&quot;" ++ escapeHelper cs
 68 |     escapeHelper ('\'' :: cs) = unpack "&#39;" ++ escapeHelper cs
 69 |     escapeHelper ('&' :: cs)  = unpack "&amp;" ++ escapeHelper cs
 70 |     escapeHelper ('<' :: cs)  = unpack "&lt;" ++ escapeHelper cs
 71 |     escapeHelper ('>' :: cs)  = unpack "&gt;" ++ escapeHelper cs
 72 |     escapeHelper (c :: cs)    = (c :: escapeHelper cs)
 73 |
 74 | ||| A dynamic script such as a Javascript.
 75 | export
 76 | data EscapedScript = MkScript String
 77 |
 78 | public export
 79 | Eq EscapedScript where
 80 |   (MkScript x) == (MkScript y) = x == y
 81 |
 82 | public export
 83 | Show EscapedScript where
 84 |   show (MkScript str) = str
 85 |
 86 | ||| Escapes the content of a script.
 87 | |||
 88 | ||| This follows the recommendations set in the [HTML specification][1].
 89 | |||
 90 | ||| [1]: https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
 91 | export
 92 | escapeScript : String -> EscapedScript
 93 | escapeScript = MkScript . pack . escapeHelper . unpack
 94 |   where
 95 |     escapeHelper : List Char -> List Char
 96 |     escapeHelper [] = []
 97 |     escapeHelper ('<' :: '!' :: '-' :: '-' :: cs) =
 98 |       unpack "\x3C!--" ++ escapeHelper cs
 99 |     escapeHelper ('<' :: 's' :: 'c' :: 'r' :: 'i' :: 'p' :: 't' :: cs) =
100 |       unpack "\x3Cscript" ++ escapeHelper cs
101 |     escapeHelper ('<' :: '/' :: 's' :: 'c' :: 'r' :: 'i' :: 'p' :: 't' :: cs) =
102 |       unpack "\x3C/script" ++ escapeHelper cs
103 |     escapeHelper (c :: cs) = (c :: escapeHelper cs)
104 |
105 | ||| CSS.
106 | export
107 | data EscapedCss = MkCss String
108 |
109 | public export
110 | Eq EscapedCss where
111 |   (MkCss x) == (MkCss y) = x == y
112 |
113 | public export
114 | Show EscapedCss where
115 |   show (MkCss str) = str
116 |
117 | ||| Escapes CSS content.
118 | export
119 | escapeCss : String -> EscapedCss
120 | escapeCss = MkCss . pack . escapeHelper . unpack
121 |   where
122 |     escapeHelper : List Char -> List Char
123 |     escapeHelper []          = []
124 |     escapeHelper ('<' :: cs) = unpack "&lt;" ++ escapeHelper cs
125 |     escapeHelper ('>' :: cs) = unpack "&gt;" ++ escapeHelper cs
126 |     escapeHelper ('&' :: cs) = unpack "&amp;" ++ escapeHelper cs
127 |     escapeHelper (c :: cs)   = (c :: escapeHelper cs)
128 |
129 | ||| A HTML element.
130 | public export
131 | data Element : Type where
132 |   ||| A normal element with attributes and children elements.
133 |   NormalElement : Name -> List Attribute -> List Element -> Element
134 |
135 |   ||| An element without any children elements.
136 |   VoidElement : Name -> List Attribute -> Element
137 |
138 |   ||| Plain text content.
139 |   PlainText : EscapedText -> Element
140 |
141 |   ||| Script content.
142 |   Script : EscapedScript -> Element
143 |
144 |   ||| Css content.
145 |   Css : EscapedCss -> Element
146 |
147 |   ||| Unescaped HTML code.
148 |   HtmlCode : String -> Element
149 |
150 | %runElab derive "Element" [Eq, Show]
151 |
152 | ||| Converts a string to a plain text element.
153 | |||
154 | ||| The string is escaped with the `escape` function.
155 | public export
156 | FromString Element where
157 |   fromString = PlainText . escape
158 |
159 | ||| Converts a string to a list with a single plain text element.
160 | |||
161 | ||| Notes:
162 | ||| - The string is escaped with the `escape` function.
163 | public export
164 | FromString (List Element) where
165 |   fromString "" = []
166 |   fromString str = [fromString str]
167 |
168 | ||| Returns an element's name.
169 | public export
170 | name : Element -> Name
171 | name (NormalElement n _ _) = n
172 | name (VoidElement n _) = n
173 | name (PlainText _) = "plaintext"
174 | name (Script _) = "scriptContent"
175 | name (Css _) = "cssContent"
176 | name (HtmlCode _) = "HTML code"
177 |
178 | ||| Returns names of an elements' list.
179 | public export
180 | names : List Element -> List Name
181 | names [] = []
182 | names (elem :: elems) = name elem :: names elems
183 |
184 | ||| Returns the list of attributes of an element.
185 | public export
186 | attributes : Element -> List Attribute
187 | attributes (NormalElement _ attrs _) = attrs
188 | attributes (VoidElement _ attrs)     = attrs
189 | attributes _                         = []
190 |
191 | ||| List of the metadata elements.
192 | |||
193 | ||| Such elements are accepted as children of the `head` element.
194 | public export
195 | metadata : List String
196 | metadata =
197 |   [ "title"
198 |   , "base"
199 |   , "link"
200 |   , "style"
201 |   , "meta"
202 |   , "script"
203 |   , "noscript"
204 |   , "template"
205 |   ]
206 |
207 | ||| Embedded content elements.
208 | |||
209 | ||| The list is as per [the one of MDN][1].
210 | |||
211 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Guides/Content_categories#embedded_content
212 | public export
213 | embeddedContent : List String
214 | embeddedContent =
215 |   [ "audio"
216 |   , "canvas"
217 |   , "embed"
218 |   , "iframe"
219 |   , "img"
220 |   , "math"
221 |   , "object"
222 |   , "picture"
223 |   , "svg"
224 |   , "video"
225 |   ]
226 |
227 | ||| Flow content elements including plain text.
228 | |||
229 | ||| The list is as per [the one of MDN][1].
230 | |||
231 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Guides/Content_categories#flow_content
232 | public export
233 | flowContent : List String
234 | flowContent =
235 |   [ "a"
236 |   , "abbr"
237 |   , "address"
238 |   , "article"
239 |   , "aside"
240 |   , "audio"
241 |   , "b"
242 |   , "bdi"
243 |   , "bdo"
244 |   , "blockquote"
245 |   , "br"
246 |   , "button"
247 |   , "canvas"
248 |   , "cite"
249 |   , "code"
250 |   , "data"
251 |   , "datalist"
252 |   , "del"
253 |   , "details"
254 |   , "dfn"
255 |   , "dialog"
256 |   , "div"
257 |   , "dl"
258 |   , "em"
259 |   , "embed"
260 |   , "fieldset"
261 |   , "figure"
262 |   , "footer"
263 |   , "form"
264 |   , "geolocation"
265 |   , "h1"
266 |   , "h2"
267 |   , "h3"
268 |   , "h4"
269 |   , "h5"
270 |   , "h6"
271 |   , "header"
272 |   , "hgroup"
273 |   , "hr"
274 |   , "i"
275 |   , "iframe"
276 |   , "img"
277 |   , "input"
278 |   , "ins"
279 |   , "kbd"
280 |   , "label"
281 |   , "main"
282 |   , "map"
283 |   , "mark"
284 |   , "math"
285 |   , "menu"
286 |   , "meter"
287 |   , "nav"
288 |   , "noscript"
289 |   , "object"
290 |   , "ol"
291 |   , "output"
292 |   , "p"
293 |   , "picture"
294 |   , "pre"
295 |   , "progress"
296 |   , "q"
297 |   , "ruby"
298 |   , "s"
299 |   , "samp"
300 |   , "script"
301 |   , "search"
302 |   , "section"
303 |   , "select"
304 |   , "slot"
305 |   , "small"
306 |   , "span"
307 |   , "strong"
308 |   , "sub"
309 |   , "sup"
310 |   , "svg"
311 |   , "table"
312 |   , "template"
313 |   , "textarea"
314 |   , "time"
315 |   , "u"
316 |   , "ul"
317 |   , "var"
318 |   , "video"
319 |   , "wbr"
320 |   , "plaintext"
321 |   ]
322 |
323 | ||| Interactive content elements.
324 | |||
325 | ||| The list is as per [the one of MDN][1].
326 | |||
327 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Guides/Content_categories#interactive_content
328 | public export
329 | interactiveContent : List String
330 | interactiveContent =
331 |   [ "button"
332 |   , "details"
333 |   , "embed"
334 |   , "iframe"
335 |   , "label"
336 |   , "select"
337 |   , "textarea"
338 |   ]
339 |
340 | ||| Phrasing content elements including plain text.
341 | |||
342 | ||| Elements valid only in certain conditions are also included.
343 | |||
344 | ||| The list is as per [the one of MDN][1].
345 | |||
346 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Guides/Content_categories#phrasing_content
347 | public export
348 | phrasingContent : List String
349 | phrasingContent =
350 |   [ "abbr"
351 |   , "audio"
352 |   , "b"
353 |   , "bdi"
354 |   , "bdo"
355 |   , "br"
356 |   , "button"
357 |   , "canvas"
358 |   , "cite"
359 |   , "code"
360 |   , "data"
361 |   , "datalist"
362 |   , "dfn"
363 |   , "em"
364 |   , "embed"
365 |   , "i"
366 |   , "iframe"
367 |   , "img"
368 |   , "input"
369 |   , "kbd"
370 |   , "label"
371 |   , "mark"
372 |   , "math"
373 |   , "meter"
374 |   , "noscript"
375 |   , "object"
376 |   , "output"
377 |   , "picture"
378 |   , "progress"
379 |   , "q"
380 |   , "ruby"
381 |   , "s"
382 |   , "samp"
383 |   , "script"
384 |   , "select"
385 |   , "slot"
386 |   , "small"
387 |   , "span"
388 |   , "strong"
389 |   , "sub"
390 |   , "sup"
391 |   , "svg"
392 |   , "template"
393 |   , "textarea"
394 |   , "time"
395 |   , "u"
396 |   , "var"
397 |   , "video"
398 |   , "wbr"
399 |   , "plaintext"
400 |   , "a"
401 |   , "area"
402 |   , "del"
403 |   , "ins"
404 |   , "link"
405 |   , "map"
406 |   , "meta"
407 |   ]
408 |
409 | ||| Heading content.
410 | |||
411 | ||| **h1** to **h6** or **hgroup** elements.
412 | public export
413 | headingContent : List String
414 | headingContent = ["h1", "h2", "h3", "h4", "h5", "h6", "hgroup"]
415 |
416 | ||| Sectioning content.
417 | |||
418 | ||| **article**, **aside**, **nav** or **section** elements.
419 | public export
420 | sectioningContent : List String
421 | sectioningContent = ["article", "aside", "nav", "section"]
422 |
423 | ||| Content accepted by lists type elements (**ul** or **ol**).
424 | public export
425 | listContent : List String
426 | listContent = ["li", "script", "template"]
427 |
428 | ||| A doctype preamble.
429 | |||
430 | ||| Note: currently only the HTML 5 one is available, but this could potentially
431 | ||| be extended.
432 | public export
433 | data Doctype = Html5
434 |
435 | ||| A HTML document.
436 | public export
437 | record Html where
438 |   constructor MkHtml
439 |
440 |   ||| Doctype preamble.
441 |   doctype : Doctype
442 |
443 |   ||| Root HTML element.
444 |   htmlElement : Element
445 |