4 * Generates HTML from tokens.
5 * @todo Refactor interface so that configuration/context is determined
6 * upon instantiation, no need for messy generateFromTokens() calls
7 * @todo Make some of the more internal functions protected, and have
8 * unit tests work around that
10 class HTMLPurifier_Generator
14 * Whether or not generator should produce XML output
16 private $_xhtml = true;
19 * :HACK: Whether or not generator should comment the insides of <script> tags
21 private $_scriptFix = false;
24 * Cache of HTMLDefinition during HTML output to determine whether or
25 * not attributes should be minimized.
30 * Cache of %Output.SortAttr
35 * Cache of %Output.FlashCompat
37 private $_flashCompat;
40 * Cache of %Output.FixInnerHTML
42 private $_innerHTMLFix;
45 * Stack for keeping track of object information when outputting IE
48 private $_flashStack = array();
51 * Configuration for the generator
56 * @param $config Instance of HTMLPurifier_Config
57 * @param $context Instance of HTMLPurifier_Context
59 public function __construct($config, $context) {
60 $this->config
= $config;
61 $this->_scriptFix
= $config->get('Output.CommentScriptContents');
62 $this->_innerHTMLFix
= $config->get('Output.FixInnerHTML');
63 $this->_sortAttr
= $config->get('Output.SortAttr');
64 $this->_flashCompat
= $config->get('Output.FlashCompat');
65 $this->_def
= $config->getHTMLDefinition();
66 $this->_xhtml
= $this->_def
->doctype
->xml
;
70 * Generates HTML from an array of tokens.
71 * @param $tokens Array of HTMLPurifier_Token
72 * @param $config HTMLPurifier_Config object
73 * @return Generated HTML
75 public function generateFromTokens($tokens) {
76 if (!$tokens) return '';
80 for ($i = 0, $size = count($tokens); $i < $size; $i++
) {
81 if ($this->_scriptFix
&& $tokens[$i]->name
=== 'script'
82 && $i +
2 < $size && $tokens[$i+
2] instanceof HTMLPurifier_Token_End
) {
83 // script special case
84 // the contents of the script block must be ONE token
86 $html .= $this->generateFromToken($tokens[$i++
]);
87 $html .= $this->generateScriptFromToken($tokens[$i++
]);
89 $html .= $this->generateFromToken($tokens[$i]);
93 if (extension_loaded('tidy') && $this->config
->get('Output.TidyFormat')) {
95 $tidy->parseString($html, array(
97 'output-xhtml' => $this->_xhtml
,
98 'show-body-only' => true,
102 $tidy->cleanRepair();
103 $html = (string) $tidy; // explicit cast necessary
106 // Normalize newlines to system defined value
107 if ($this->config
->get('Core.NormalizeNewlines')) {
108 $nl = $this->config
->get('Output.Newline');
109 if ($nl === null) $nl = PHP_EOL
;
110 if ($nl !== "\n") $html = str_replace("\n", $nl, $html);
116 * Generates HTML from a single token.
117 * @param $token HTMLPurifier_Token object.
118 * @return Generated HTML
120 public function generateFromToken($token) {
121 if (!$token instanceof HTMLPurifier_Token
) {
122 trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING
);
125 } elseif ($token instanceof HTMLPurifier_Token_Start
) {
126 $attr = $this->generateAttributes($token->attr
, $token->name
);
127 if ($this->_flashCompat
) {
128 if ($token->name
== "object") {
129 $flash = new stdclass();
130 $flash->attr
= $token->attr
;
131 $flash->param
= array();
132 $this->_flashStack
[] = $flash;
135 return '<' . $token->name
. ($attr ?
' ' : '') . $attr . '>';
137 } elseif ($token instanceof HTMLPurifier_Token_End
) {
139 if ($this->_flashCompat
) {
140 if ($token->name
== "object" && !empty($this->_flashStack
)) {
141 // doesn't do anything for now
144 return $_extra . '</' . $token->name
. '>';
146 } elseif ($token instanceof HTMLPurifier_Token_Empty
) {
147 if ($this->_flashCompat
&& $token->name
== "param" && !empty($this->_flashStack
)) {
148 $this->_flashStack
[count($this->_flashStack
)-1]->param
[$token->attr
['name']] = $token->attr
['value'];
150 $attr = $this->generateAttributes($token->attr
, $token->name
);
151 return '<' . $token->name
. ($attr ?
' ' : '') . $attr .
152 ( $this->_xhtml ?
' /': '' ) // <br /> v. <br>
155 } elseif ($token instanceof HTMLPurifier_Token_Text
) {
156 return $this->escape($token->data
, ENT_NOQUOTES
);
158 } elseif ($token instanceof HTMLPurifier_Token_Comment
) {
159 return '<!--' . $token->data
. '-->';
167 * Special case processor for the contents of script tags
168 * @warning This runs into problems if there's already a literal
169 * --> somewhere inside the script contents.
171 public function generateScriptFromToken($token) {
172 if (!$token instanceof HTMLPurifier_Token_Text
) return $this->generateFromToken($token);
173 // Thanks <http://lachy.id.au/log/2005/05/script-comments>
174 $data = preg_replace('#//\s*$#', '', $token->data
);
175 return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>';
179 * Generates attribute declarations from attribute array.
180 * @note This does not include the leading or trailing space.
181 * @param $assoc_array_of_attributes Attribute array
182 * @param $element Name of element attributes are for, used to check
183 * attribute minimization.
184 * @return Generate HTML fragment for insertion.
186 public function generateAttributes($assoc_array_of_attributes, $element = false) {
188 if ($this->_sortAttr
) ksort($assoc_array_of_attributes);
189 foreach ($assoc_array_of_attributes as $key => $value) {
190 if (!$this->_xhtml
) {
191 // Remove namespaced attributes
192 if (strpos($key, ':') !== false) continue;
193 // Check if we should minimize the attribute: val="val" -> val
194 if ($element && !empty($this->_def
->info
[$element]->attr
[$key]->minimized
)) {
199 // Workaround for Internet Explorer innerHTML bug.
200 // Essentially, Internet Explorer, when calculating
201 // innerHTML, omits quotes if there are no instances of
202 // angled brackets, quotes or spaces. However, when parsing
203 // HTML (for example, when you assign to innerHTML), it
204 // treats backticks as quotes. Thus,
210 // Fortunately, all we need to do is trigger an appropriate
211 // quoting style, which we do by adding an extra space.
212 // This also is consistent with the W3C spec, which states
213 // that user agents may ignore leading or trailing
214 // whitespace (in fact, most don't, at least for attributes
215 // like alt, but an extra space at the end is barely
216 // noticeable). Still, we have a configuration knob for
217 // this, since this transformation is not necesary if you
218 // don't process user input with innerHTML or you don't plan
219 // on supporting Internet Explorer.
220 if ($this->_innerHTMLFix
) {
221 if (strpos($value, '`') !== false) {
222 // check if correct quoting style would not already be
224 if (strcspn($value, '"\' <>') === strlen($value)) {
230 $html .= $key.'="'.$this->escape($value).'" ';
236 * Escapes raw text data.
237 * @todo This really ought to be protected, but until we have a facility
238 * for properly generating HTML here w/o using tokens, it stays
240 * @param $string String data to escape for HTML.
241 * @param $quote Quoting style, like htmlspecialchars. ENT_NOQUOTES is
242 * permissible for non-attribute output.
243 * @return String escaped data.
245 public function escape($string, $quote = null) {
246 // Workaround for APC bug on Mac Leopard reported by sidepodcast
247 // http://htmlpurifier.org/phorum/read.php?3,4823,4846
248 if ($quote === null) $quote = ENT_COMPAT
;
249 return htmlspecialchars($string, $quote, 'UTF-8');
254 // vim: et sw=4 sts=4