############################# Cleaning and shrinking HTML ############################# Two jobs sit at the output edge of a pipeline: making untrusted markup safe to publish, and making trusted markup smaller to ship. This tutorial sanitizes a comment against an allowlist, renaming legacy tags as it goes, then minifies HTML, CSS, and JavaScript. ******************************* Sanitize against an allowlist ******************************* :func:`turbohtml.clean.sanitize` keeps only what a :class:`~turbohtml.clean.Policy` allows, and a non-overridable baseline drops scripting and ``javascript:`` URLs whatever the policy says. ``transform_tags`` renames a tag as it is cleaned -- here the deprecated ``
one
two
")) .. testoutput::one
two ******************* Minify CSS and JS ******************* The same module shrinks stylesheets and scripts. :func:`~turbohtml.clean.minify_css` and :func:`~turbohtml.clean.minify_js` are value-safe: they rewrite only what cannot change behavior, collapsing whitespace, shortening colors, and (for JS) renaming local bindings: .. testcode:: from turbohtml.clean import minify_css, minify_js print(minify_css("a { color: #ffffff; margin: 0px 0px; }")) print(minify_js("function f(x) { var half = x / 2; return half * half; }")) .. testoutput:: a{color:#fff;margin:0} function f(b){var a=b/2;return a*a} Sanitized for safety, minified for size. Next, :doc:`pipelines` transforms markup without ever building a full tree.