3 // pretty-printing with indentation would be pretty cool
5 require_once 'HTMLPurifier/Lexer.php';
7 HTMLPurifier_ConfigSchema
::define(
8 'Core', 'CleanUTF8DuringGeneration', false, 'bool',
9 'When true, HTMLPurifier_Generator will also check all strings it '.
10 'escapes for UTF-8 well-formedness as a defense in depth measure. '.
11 'This could cause a considerable performance impact, and is not '.
12 'strictly necessary due to the fact that the Lexers should have '.
13 'ensured that all the UTF-8 strings were well-formed. Note that '.
14 'the configuration value is only read at the beginning of '.
18 HTMLPurifier_ConfigSchema
::define(
19 'Core', 'XHTML', true, 'bool',
20 'Determines whether or not output is XHTML or not. When disabled, HTML '.
21 'Purifier goes into HTML 4.01 removes XHTML-specific markup constructs, '.
22 'such as boolean attribute expansion and trailing slashes in empty tags. '.
23 'This directive was available since 1.1.'
26 // extension constraints could be factored into ConfigSchema
27 HTMLPurifier_ConfigSchema
::define(
28 'Core', 'TidyFormat', false, 'bool',
29 '<p>Determines whether or not to run Tidy on the final output for pretty '.
30 'formatting reasons, such as indentation and wrap.</p><p>This can greatly '.
31 'improve readability for editors who are hand-editing the HTML, but is '.
32 'by no means necessary as HTML Purifier has already fixed all major '.
33 'errors the HTML may have had. Tidy is a non-default extension, and this directive '.
34 'will silently fail if Tidy is not available.</p><p>If you are looking to make '.
35 'the overall look of your page\'s source better, I recommend running Tidy '.
36 'on the entire page rather than just user-content (after all, the '.
37 'indentation relative to the containing blocks will be incorrect).</p><p>This '.
38 'directive was available since 1.1.1.</p>'
42 * Generates HTML from tokens.
44 class HTMLPurifier_Generator
48 * Bool cache of %Core.CleanUTF8DuringGeneration
51 var $_clean_utf8 = false;
54 * Bool cache of %Core.XHTML
59 * Generates HTML from an array of tokens.
60 * @param $tokens Array of HTMLPurifier_Token
61 * @param $config HTMLPurifier_Config object
62 * @return Generated HTML
63 * @note Only unit tests may omit configuration: internals MUST pass config
65 function generateFromTokens($tokens, $config = null) {
67 if (!$config) $config = HTMLPurifier_Config
::createDefault();
68 $this->_clean_utf8
= $config->get('Core', 'CleanUTF8DuringGeneration');
69 $this->_xhtml
= $config->get('Core', 'XHTML');
70 if (!$tokens) return '';
71 foreach ($tokens as $token) {
72 $html .= $this->generateFromToken($token);
74 if ($config->get('Core', 'TidyFormat') && extension_loaded('tidy')) {
76 $tidy_options = array(
78 'output-xhtml' => $this->_xhtml
,
79 'show-body-only' => true,
83 if (version_compare(PHP_VERSION
, '5', '<')) {
84 tidy_set_encoding('utf8');
85 foreach ($tidy_options as $key => $value) {
86 tidy_setopt($key, $value);
88 tidy_parse_string($html);
90 $html = tidy_get_output();
93 $tidy->parseString($html, $tidy_options, 'utf8');
95 $html = (string) $tidy;
102 * Generates HTML from a single token.
103 * @param $token HTMLPurifier_Token object.
104 * @return Generated HTML
106 function generateFromToken($token) {
107 if (!isset($token->type
)) return '';
108 if ($token->type
== 'start') {
109 $attr = $this->generateAttributes($token->attributes
);
110 return '<' . $token->name
. ($attr ?
' ' : '') . $attr . '>';
112 } elseif ($token->type
== 'end') {
113 return '</' . $token->name
. '>';
115 } elseif ($token->type
== 'empty') {
116 $attr = $this->generateAttributes($token->attributes
);
117 return '<' . $token->name
. ($attr ?
' ' : '') . $attr .
118 ( $this->_xhtml ?
' /': '' )
121 } elseif ($token->type
== 'text') {
122 return $this->escape($token->data
);
131 * Generates attribute declarations from attribute array.
132 * @param $assoc_array_of_attributes Attribute array
133 * @return Generate HTML fragment for insertion.
135 function generateAttributes($assoc_array_of_attributes) {
137 foreach ($assoc_array_of_attributes as $key => $value) {
138 if (!$this->_xhtml
) {
139 // remove namespaced attributes
140 if (strpos($key, ':') !== false) continue;
141 // also needed: check for attribute minimization
143 $html .= $key.'="'.$this->escape($value).'" ';
149 * Escapes raw text data.
150 * @param $string String data to escape for HTML.
151 * @return String escaped data.
153 function escape($string) {
154 if ($this->_clean_utf8
) $string = HTMLPurifier_Lexer
::cleanUTF8($string);
155 return htmlspecialchars($string, ENT_COMPAT
, 'UTF-8');