2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-12 20:22:09 +00:00
|
|
|
// pretty-printing with indentation would be pretty cool
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
class HTMLPurifier_Generator
|
|
|
|
{
|
|
|
|
|
2006-08-15 00:31:12 +00:00
|
|
|
// only unit tests may omit configuration: internals MUST pass config
|
|
|
|
function generateFromTokens($tokens, $config = null) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
2006-08-15 00:31:12 +00:00
|
|
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
2006-07-30 18:37:42 +00:00
|
|
|
if (!$tokens) return '';
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($tokens as $token) {
|
2006-08-15 00:31:12 +00:00
|
|
|
$html .= $this->generateFromToken($token, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2006-08-15 00:31:12 +00:00
|
|
|
function generateFromToken($token, $config) {
|
2006-08-01 00:29:38 +00:00
|
|
|
if (!isset($token->type)) return '';
|
2006-07-23 00:11:03 +00:00
|
|
|
if ($token->type == 'start') {
|
2006-08-15 00:31:12 +00:00
|
|
|
$attr = $this->generateAttributes($token->attributes, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
return '</' . $token->name . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'empty') {
|
2006-08-15 00:31:12 +00:00
|
|
|
$attr = $this->generateAttributes($token->attributes, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . ' />';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'text') {
|
2006-08-01 00:29:38 +00:00
|
|
|
return htmlspecialchars($token->data, ENT_COMPAT, 'UTF-8');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-15 00:31:12 +00:00
|
|
|
function generateAttributes($assoc_array_of_attributes, $config) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
|
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
2006-08-01 00:29:38 +00:00
|
|
|
$html .= $key.'="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" ';
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
return rtrim($html);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-16 03:27:40 +00:00
|
|
|
?>
|