Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Generator.php
blobb6a9aa24d74cba48dc2b848298285bdc068c2b4f
1 <?php
3 require_once 'HTMLPurifier/Lexer.php';
5 HTMLPurifier_ConfigSchema::define(
6 'Core', 'CleanUTF8DuringGeneration', false, 'bool',
7 'When true, HTMLPurifier_Generator will also check all strings it '.
8 'escapes for UTF-8 well-formedness as a defense in depth measure. '.
9 'This could cause a considerable performance impact, and is not '.
10 'strictly necessary due to the fact that the Lexers should have '.
11 'ensured that all the UTF-8 strings were well-formed. Note that '.
12 'the configuration value is only read at the beginning of '.
13 'generateFromTokens.'
16 HTMLPurifier_ConfigSchema::define(
17 'Core', 'XHTML', true, 'bool',
18 'Determines whether or not output is XHTML or not. When disabled, HTML '.
19 'Purifier goes into HTML 4.01 removes XHTML-specific markup constructs, '.
20 'such as boolean attribute expansion and trailing slashes in empty tags. '.
21 'This directive was available since 1.1.'
24 // extension constraints could be factored into ConfigSchema
25 HTMLPurifier_ConfigSchema::define(
26 'Core', 'TidyFormat', false, 'bool',
27 '<p>Determines whether or not to run Tidy on the final output for pretty '.
28 'formatting reasons, such as indentation and wrap.</p><p>This can greatly '.
29 'improve readability for editors who are hand-editing the HTML, but is '.
30 'by no means necessary as HTML Purifier has already fixed all major '.
31 'errors the HTML may have had. Tidy is a non-default extension, and this directive '.
32 'will silently fail if Tidy is not available.</p><p>If you are looking to make '.
33 'the overall look of your page\'s source better, I recommend running Tidy '.
34 'on the entire page rather than just user-content (after all, the '.
35 'indentation relative to the containing blocks will be incorrect).</p><p>This '.
36 'directive was available since 1.1.1.</p>'
39 /**
40 * Generates HTML from tokens.
42 class HTMLPurifier_Generator
45 /**
46 * Bool cache of %Core.CleanUTF8DuringGeneration
47 * @private
49 var $_clean_utf8 = false;
51 /**
52 * Bool cache of %Core.XHTML
53 * @private
55 var $_xhtml = true;
57 /**
58 * Generates HTML from an array of tokens.
59 * @param $tokens Array of HTMLPurifier_Token
60 * @param $config HTMLPurifier_Config object
61 * @return Generated HTML
63 function generateFromTokens($tokens, $config, &$context) {
64 $html = '';
65 if (!$config) $config = HTMLPurifier_Config::createDefault();
66 $this->_clean_utf8 = $config->get('Core', 'CleanUTF8DuringGeneration');
67 $this->_xhtml = $config->get('Core', 'XHTML');
68 if (!$tokens) return '';
69 foreach ($tokens as $token) {
70 $html .= $this->generateFromToken($token);
72 if ($config->get('Core', 'TidyFormat') && extension_loaded('tidy')) {
74 $tidy_options = array(
75 'indent'=> true,
76 'output-xhtml' => $this->_xhtml,
77 'show-body-only' => true,
78 'indent-spaces' => 2,
79 'wrap' => 68,
81 if (version_compare(PHP_VERSION, '5', '<')) {
82 tidy_set_encoding('utf8');
83 foreach ($tidy_options as $key => $value) {
84 tidy_setopt($key, $value);
86 tidy_parse_string($html);
87 tidy_clean_repair();
88 $html = tidy_get_output();
89 } else {
90 $tidy = new Tidy;
91 $tidy->parseString($html, $tidy_options, 'utf8');
92 $tidy->cleanRepair();
93 $html = (string) $tidy;
96 return $html;
99 /**
100 * Generates HTML from a single token.
101 * @param $token HTMLPurifier_Token object.
102 * @return Generated HTML
104 function generateFromToken($token) {
105 if (!isset($token->type)) return '';
106 if ($token->type == 'start') {
107 $attr = $this->generateAttributes($token->attr);
108 return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
110 } elseif ($token->type == 'end') {
111 return '</' . $token->name . '>';
113 } elseif ($token->type == 'empty') {
114 $attr = $this->generateAttributes($token->attr);
115 return '<' . $token->name . ($attr ? ' ' : '') . $attr .
116 ( $this->_xhtml ? ' /': '' )
117 . '>';
119 } elseif ($token->type == 'text') {
120 return $this->escape($token->data);
122 } else {
123 return '';
129 * Generates attribute declarations from attribute array.
130 * @param $assoc_array_of_attributes Attribute array
131 * @return Generate HTML fragment for insertion.
133 function generateAttributes($assoc_array_of_attributes) {
134 $html = '';
135 foreach ($assoc_array_of_attributes as $key => $value) {
136 if (!$this->_xhtml) {
137 // remove namespaced attributes
138 if (strpos($key, ':') !== false) continue;
139 // also needed: check for attribute minimization
141 $html .= $key.'="'.$this->escape($value).'" ';
143 return rtrim($html);
147 * Escapes raw text data.
148 * @param $string String data to escape for HTML.
149 * @return String escaped data.
151 function escape($string) {
152 if ($this->_clean_utf8) $string = HTMLPurifier_Lexer::cleanUTF8($string);
153 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');