mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-10 07:38:41 +00:00
973cc43b64
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@303 48356398-32a2-884e-a903-53898d9a118a
72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
// pretty-printing with indentation would be pretty cool
|
|
|
|
require_once 'HTMLPurifier/Lexer.php';
|
|
|
|
HTMLPurifier_ConfigDef::define(
|
|
'Core', 'CleanUTF8DuringGeneration', false,
|
|
'When true, HTMLPurifier_Generator will also check all strings it '.
|
|
'escapes for UTF-8 well-formedness as a defense in depth measure. '.
|
|
'This could cause a considerable performance impact, and is not '.
|
|
'strictly necessary due to the fact that the Lexers should have '.
|
|
'ensured that all the UTF-8 strings were well-formed. Note that '.
|
|
'the configuration value is only read at the beginning of '.
|
|
'generateFromTokens.'
|
|
);
|
|
|
|
class HTMLPurifier_Generator
|
|
{
|
|
|
|
var $clean_utf8 = false;
|
|
|
|
// only unit tests may omit configuration: internals MUST pass config
|
|
function generateFromTokens($tokens, $config = null) {
|
|
$html = '';
|
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
|
$this->clean_utf8 = $config->get('Core', 'CleanUTF8DuringGeneration');
|
|
if (!$tokens) return '';
|
|
foreach ($tokens as $token) {
|
|
$html .= $this->generateFromToken($token);
|
|
}
|
|
return $html;
|
|
}
|
|
|
|
function generateFromToken($token) {
|
|
if (!isset($token->type)) return '';
|
|
if ($token->type == 'start') {
|
|
$attr = $this->generateAttributes($token->attributes);
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
|
|
|
|
} elseif ($token->type == 'end') {
|
|
return '</' . $token->name . '>';
|
|
|
|
} elseif ($token->type == 'empty') {
|
|
$attr = $this->generateAttributes($token->attributes);
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . ' />';
|
|
|
|
} elseif ($token->type == 'text') {
|
|
return $this->escape($token->data);
|
|
|
|
} else {
|
|
return '';
|
|
|
|
}
|
|
}
|
|
|
|
function generateAttributes($assoc_array_of_attributes) {
|
|
$html = '';
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
|
$html .= $key.'="'.$this->escape($value).'" ';
|
|
}
|
|
return rtrim($html);
|
|
}
|
|
|
|
function escape($string) {
|
|
if ($this->clean_utf8) $string = HTMLPurifier_Lexer::cleanUTF8($string);
|
|
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
|
|
}
|
|
|
|
}
|
|
|
|
?>
|