Skip to content

5.3.0 - SafeString - Bypass html escaping

Compare
Choose a tag to compare
@thomasborgen thomasborgen released this 26 Aug 10:26
· 3 commits to main since this release
ca27349

Version 5.3.0

Feature

  • Adds SafeString type as a wrapper around the normal str class. This can be used to tell hypermedia not to html.escape the string in any element.
Div("this is always escaped")
Div(SafeString("This will not be escaped"))
  • Hypermedia is now cachable - All calls to .dump() now returns a SafeString. This means that caching a dumped element is now possible because we won't call html.escape on it again.
@lru_cache()
def stylesheets() -> SafeString:
    return ElementList(
        Link(
            rel="stylesheet",
            href="/static/css/normalize.css",
            type="text/css",
        ),
        Link(
            rel="stylesheet",
            href="/static/css/simple.min.css",
            type="text/css",
        )
    ).dump()

def base():
    """
    Once `stylesheets()` is called once, it will always use the cached String in subsequent
    calls to `base()`
    """
    return ElementList(
        Doctype(),
        Html(
            Head(
                Meta(charset="UTF-8"),
                stylesheets(),
                slot="head",
            ),
            Body(...)
            lan="en",
        ),
    )
  • Script and Style elements wraps input in SafeString by default.
Script("This is wrapped in `SafeString` by default")
Style("This is wrapped in `SafeString` by default")