Multiple inner whitespaces are collapsed to a single space (except in pre tags): Whitespace surrounding block-level tags is removed. Self-closing tags are converted to open tags. Attributes are sorted and lowercased. References are converted to unicode, except that '<', '>', '&', and '"' are rendered using entities. We work around HTMLParser's limitations parsing CDATA by breaking the input into chunks and passing CDATA chunks through verbatim. return html # on error, return unnormalized HTML