get('Core', 'LexerImpl'); } if (is_object($lexer)) { return $lexer; } if (is_null($lexer)) { do { // auto-detection algorithm // once PHP DOM implements native line numbers, or we // hack out something using XSLT, remove this stipulation $line_numbers = $config->get('Core', 'MaintainLineNumbers'); if ( $line_numbers === true || ($line_numbers === null && $config->get('Core', 'CollectErrors')) ) { $lexer = 'DirectLex'; break; } if (version_compare(PHP_VERSION, "5", ">=") && // check for PHP5 class_exists('DOMDocument')) { // check for DOM support $lexer = 'DOMLex'; } else { $lexer = 'DirectLex'; } } while(0); } // do..while so we can break // instantiate recognized string names switch ($lexer) { case 'DOMLex': return new HTMLPurifier_Lexer_DOMLex(); case 'DirectLex': return new HTMLPurifier_Lexer_DirectLex(); case 'PH5P': // experimental Lexer that must be manually included return new HTMLPurifier_Lexer_PH5P(); default: trigger_error("Cannot instantiate unrecognized Lexer type " . htmlspecialchars($lexer), E_USER_ERROR); } } // -- CONVENIENCE MEMBERS --------------------------------------------- public function __construct() { $this->_entity_parser = new HTMLPurifier_EntityParser(); } /** * Most common entity to raw value conversion table for special entities. */ protected $_special_entity2str = array( '"' => '"', '&' => '&', '<' => '<', '>' => '>', ''' => "'", ''' => "'", ''' => "'" ); /** * Parses special entities into the proper characters. * * This string will translate escaped versions of the special characters * into the correct ones. * * @warning * You should be able to treat the output of this function as * completely parsed, but that's only because all other entities should * have been handled previously in substituteNonSpecialEntities() * * @param $string String character data to be parsed. * @returns Parsed character data. */ public function parseData($string) { // following functions require at least one character if ($string === '') return ''; // subtracts amps that cannot possibly be escaped $num_amp = substr_count($string, '&') - substr_count($string, '& ') - ($string[strlen($string)-1] === '&' ? 1 : 0); if (!$num_amp) return $string; // abort if no entities $num_esc_amp = substr_count($string, '&'); $string = strtr($string, $this->_special_entity2str); // code duplication for sake of optimization, see above $num_amp_2 = substr_count($string, '&') - substr_count($string, '& ') - ($string[strlen($string)-1] === '&' ? 1 : 0); if ($num_amp_2 <= $num_esc_amp) return $string; // hmm... now we have some uncommon entities. Use the callback. $string = $this->_entity_parser->substituteSpecialEntities($string); return $string; } /** * Lexes an HTML string into tokens. * * @param $string String HTML. * @return HTMLPurifier_Token array representation of HTML. */ public function tokenizeHTML($string, $config, $context) { trigger_error('Call to abstract class', E_USER_ERROR); } /** * Translates CDATA sections into regular sections (through escaping). * * @param $string HTML string to process. * @returns HTML with CDATA sections escaped. */ protected static function escapeCDATA($string) { return preg_replace_callback( '//s', array('HTMLPurifier_Lexer', 'CDATACallback'), $string ); } /** * Special CDATA case that is especially convoluted for