diff --git a/docs/usage.md b/docs/usage.md index 45a525f..3e4b247 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -261,6 +261,35 @@ Attributes via id/class shorthand, keyword arguments and dictionary can be combi ``` +### Escaping of Attributes + +Attributes are always escaped. This makes it possible to pass arbitrary HTML +fragments or scripts as attributes. The output may look a bit obfuscated since +all unsafe characters are escaped but the browser will interpret it correctly: + +```pycon +>>> from htpy import button +>>> print(button(id="example", onclick="let name = 'andreas'; alert('hi' + name);")["Say hi"]) + +``` + +In the browser, the parsed attribute as returned by +`document.getElementById("example").getAttribute("onclick")` will be the +original string `let name = 'andreas'; alert('hi' + name);`. + +Escaping will happen whether or not the value is wrapped in `markupsafe.Markup` +or not. This may seem confusing at first but is useful when embedding HTML +snippets as attributes: + +```pycon title="Escaping of Markup" +>>> from htpy import ul +>>> from markupsafe import Markup +>>> # This markup may come from another library/template engine +>>> some_markup = Markup("""
""") +>>> print(ul(data_li_template=some_markup)) +