louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#121 · main · 16 min 23 s
0Étoiles
0Forks
40Issues
| 1 | /// Server-rendered pages, as pug templates (ADR 0048), styled from disk |
| 2 | /// (`static/style.css`, ADR 0049). |
| 3 | |
| 4 | import std/html.{type Html} |
| 5 | import std/string |
| 6 | import std/template.{pug} |
| 7 | import std/test.{assert} |
| 8 | |
| 9 | fn page :: title: String body: Html -> Html |
| 10 | { |
| 11 | pug` |
| 12 | html(lang="en") |
| 13 | head |
| 14 | meta(charset="utf-8") |
| 15 | meta(name="viewport" content="width=device-width, initial-scale=1") |
| 16 | link(rel="stylesheet" href="/static/style.css") |
| 17 | title {title} |
| 18 | body |
| 19 | main.shell |
| 20 | header.brand |
| 21 | span.accent ~/ |
| 22 | span louise |
| 23 | span.dim · |
| 24 | span.dim demo |
| 25 | span.cursor ▌ |
| 26 | h1 {title} |
| 27 | {body} |
| 28 | ` |
| 29 | } |
| 30 | |
| 31 | /// The error paragraph, or nothing. |
| 32 | fn error_note :: error: String -> Html |
| 33 | { |
| 34 | if error == "" |
| 35 | { |
| 36 | html.Fragment([]) |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | pug`p.error {error}` |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn login :: error: String -> Html |
| 45 | { |
| 46 | page |
| 47 | "Log in" |
| 48 | pug` |
| 49 | {error_note error} |
| 50 | .card |
| 51 | form(method="post" action="/login") |
| 52 | input(name="name" placeholder="Name" required) |
| 53 | input(name="password" type="password" placeholder="Password" required) |
| 54 | button.primary(type="submit") Log in |
| 55 | h2 New here? |
| 56 | .card |
| 57 | form(method="post" action="/register") |
| 58 | input(name="name" placeholder="Name" required) |
| 59 | input(name="password" type="password" placeholder="Password (8+ characters)" required) |
| 60 | button.ghost(type="submit") Register |
| 61 | ` |
| 62 | } |
| 63 | |
| 64 | @test |
| 65 | fn the_login_page_shows_an_error_only_if_there_is_one :: |
| 66 | { |
| 67 | assert |
| 68 | (!string.contains (html.render (login "")) "class=\"error\"") |
| 69 | "no error paragraph" |
| 70 | assert |
| 71 | ( string.contains |
| 72 | (html.render (login "Try again.")) |
| 73 | "<p class=\"error\">Try again.</p>" |
| 74 | ) |
| 75 | "the error" |
| 76 | } |
| 77 | |
| 78 | pub fn home :: name: String -> Html |
| 79 | { |
| 80 | page |
| 81 | "Home" |
| 82 | pug` |
| 83 | .card |
| 84 | p Welcome, {name}! |
| 85 | p.muted |
| 86 | span.dim session |
| 87 | span.accent · live |
| 88 | p |
| 89 | a(href="/logout") Log out |
| 90 | ` |
| 91 | } |
| 92 | |
| 93 | @test |
| 94 | fn names_are_escaped_on_the_home_page :: |
| 95 | { |
| 96 | // Anyone can register any name. |
| 97 | let page = html.render (home "<script>alert(1)</script>") |
| 98 | assert |
| 99 | ( string.contains |
| 100 | page |
| 101 | "<p>Welcome, <script>alert(1)</script>!</p>" |
| 102 | ) |
| 103 | "escaped" |
| 104 | assert (!string.contains page "<script>") "no script element" |
| 105 | } |