code / tomo-koans

Lines447 Tomo432 INI9 Markdown6
(29 lines)
1 # Langs (Safe Embedded Languages)
3 # `lang` defines custom text types with automatic escaping.
4 lang HTML
6 # Custom escaping rules can be created with `convert`
7 convert(t:Text -> HTML)
8 t = t.translate({"&": "&amp;", "<": "&lt;", ">": "&gt;"})
9 return HTML.from_text(t)
11 func paragraph(content:HTML -> HTML)
12 return $HTML"<p>$content</p>"
15 # Type safety prevents injection:
16 func greet(name:HTML -> HTML)
17 return $HTML"Hello $name!"
19 func main()
21 malicious_input := "<b>hello</b>"
23 safe := $HTML"User said: $malicious_input"
25 assert safe == ???
27 assert safe.paragraph() == ???
29 greeting := greet(malicious_input) # This won't compile