Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
Introduction
In a couple recent episodes, we introduced the idea of domain specific languages, often abbreviated DSLs, which are languages that are highly tuned to a specific task. We then introduced the idea of embedded DSLs (or EDSLs), which are domain specific languages that are hosted, or written, in a more general purpose language, like Swift.
To get comfortable with the idea, we embedded some basic arithmetic in the Swift type system. Using a DSL helped us separate the pure, data-driven description of a domain problem from the impure, run-time interpretation of that data.
But arithmetic was a bit of a toy example, though, so today we’re going to embed a more real-world domain problem in Swift, HTML, by building the same DSL that we use to power the Point-Free web site!
Subscribe to Point-Free
Access this episode, plus all past and future episodes when you become a subscriber.
Already a subscriber? Log in
Exercises
Our
render
function currently prints an extra space when attributes aren’t present:"<header ></header>"
. Fix therender
function so thatrender(header([])) == "<header></header>"
.HTML specifies a number of “void elements” (elements that have no closing tag). This includes the
img
element in our example. Update therender
function to omit the closing tag on void elements.Our
render
function is currently unsafe: text node content isn’t escaped, which means it could be susceptible to cross-site scripting attacks. Ensure that text nodes are properly escaped during rendering.Ensure that attribute nodes are properly escaped during rendering.
Write a function
redacted
, which transforms aNode
and its children, replacing all non-whitespace characters with a redacted character:█
.Write a function
removingStyles
, which removes allstyle
nodes and attributes.Write a function
removingScripts
, which removes allscript
nodes and attributes with theon
prefix (likeonclick
).Write a function
plainText
, which transforms HTML into human-readable text, which might be useful for rendering plain-text emails from HTML content.One of the most popular way of rendering HTML is to use a templating language (Swift, for example, has Stencil). What are some of the pros and cons of using a templating language over a DSL.
References
Open sourcing swift-html: A Type-Safe Alternative to Templating Languages in Swift
Brandon Williams & Stephen Celis • Monday Nov 12, 2018After developing the ideas of DSLs in a series of episodes ( part 1 and part 2), we open sourced our own DSL library for constructing HTML in Swift. We use this library heavily for building every page on this very website, and it unlocks a lot of wonderful transformations and opportunities for code reuse.