diff --git a/library/HTMLPurifier/Lexer.php b/library/HTMLPurifier/Lexer.php index 031e8e3d..0da29766 100644 --- a/library/HTMLPurifier/Lexer.php +++ b/library/HTMLPurifier/Lexer.php @@ -137,6 +137,31 @@ class HTMLPurifier_Lexer return htmlspecialchars($matches[1], ENT_COMPAT, 'UTF-8'); } + /** + * Takes a piece of HTML and normalizes it by converting entities, fixing + * encoding, extracting bits, and other good stuff. + */ + function normalize($html, $config) { + + // extract body from document if applicable + if ($config->get('Core', 'AcceptFullDocuments')) { + $html = $this->extractBody($html); + } + + // escape CDATA + $html = $this->escapeCDATA($html); + + // expand entities that aren't the big five + $html = $this->_encoder->substituteNonSpecialEntities($html); + + // clean into wellformed UTF-8 string for an SGML context: this has + // to be done after entity expansion because the entities sometimes + // represent non-SGML characters (horror, horror!) + $html = $this->_encoder->cleanUTF8($html); + + return $html; + } + /** * Takes a string of HTML (fragment or document) and returns the content */ diff --git a/library/HTMLPurifier/Lexer/DOMLex.php b/library/HTMLPurifier/Lexer/DOMLex.php index e408fa84..202b4c8a 100644 --- a/library/HTMLPurifier/Lexer/DOMLex.php +++ b/library/HTMLPurifier/Lexer/DOMLex.php @@ -37,24 +37,7 @@ class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer public function tokenizeHTML($string, $config = null) { if (!$config) $config = HTMLPurifier_Config::createDefault(); - if ($config->get('Core', 'AcceptFullDocuments')) { - $string = $this->extractBody($string); - } - - $doc = new DOMDocument(); - $doc->encoding = 'UTF-8'; // technically does nothing, but whatever - - // replace and escape the CDATA sections, since parsing under HTML - // mode won't get 'em. - $string = $this->escapeCDATA($string); - - // substitute non-special entities. While DOM is perfectly capable - // of doing this, we need to get at the UTF-8 characters in - // cleanUTF8 - $string = $this->_encoder->substituteNonSpecialEntities($string); - - // clean it into well-formed UTF-8 string - $string = $this->_encoder->cleanUTF8($string); + $string = $this->normalize($string, $config); // preprocess string, essential for UTF-8 $string = @@ -66,6 +49,8 @@ class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer ' charset=utf-8" />'. '