2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_Generator
|
|
|
|
{
|
|
|
|
|
|
|
|
function generateFromTokens($tokens) {
|
|
|
|
$html = '';
|
2006-07-30 18:37:42 +00:00
|
|
|
if (!$tokens) return '';
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$html .= $this->generateFromToken($token);
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateFromToken($token) {
|
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') {
|
|
|
|
$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') {
|
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 '';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateAttributes($assoc_array_of_attributes) {
|
|
|
|
$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
|
|
|
?>
|