mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-14 01:08:41 +00:00
f63e870300
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@138 48356398-32a2-884e-a903-53898d9a118a
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
class HTMLPurifier_Generator
|
|
{
|
|
|
|
function generateFromTokens($tokens) {
|
|
$html = '';
|
|
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 htmlspecialchars($token->data, ENT_COMPAT, 'UTF-8');
|
|
|
|
} else {
|
|
return '';
|
|
|
|
}
|
|
}
|
|
|
|
function generateAttributes($assoc_array_of_attributes) {
|
|
$html = '';
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
|
$html .= $key.'="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" ';
|
|
}
|
|
return rtrim($html);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|