2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2006-09-16 22:36:58 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
2007-05-19 00:24:23 +00:00
|
|
|
'Output', 'CommentScriptContents', true, 'bool',
|
2007-05-15 01:17:10 +00:00
|
|
|
'Determines whether or not HTML Purifier should attempt to fix up '.
|
|
|
|
'the contents of script tags for legacy browsers with comments. This '.
|
|
|
|
'directive was available since 1.7.'
|
|
|
|
);
|
2007-05-19 00:24:23 +00:00
|
|
|
HTMLPurifier_ConfigSchema::defineAlias('Core', 'CommentScriptContents', 'Output', 'CommentScriptContents');
|
2007-05-15 01:17:10 +00:00
|
|
|
|
2006-09-24 21:23:54 +00:00
|
|
|
// extension constraints could be factored into ConfigSchema
|
|
|
|
HTMLPurifier_ConfigSchema::define(
|
2007-05-19 00:24:23 +00:00
|
|
|
'Output', 'TidyFormat', false, 'bool', <<<HTML
|
|
|
|
<p>
|
|
|
|
Determines whether or not to run Tidy on the final output for pretty
|
|
|
|
formatting reasons, such as indentation and wrap.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
This can greatly improve readability for editors who are hand-editing
|
|
|
|
the HTML, but is by no means necessary as HTML Purifier has already
|
|
|
|
fixed all major errors the HTML may have had. Tidy is a non-default
|
|
|
|
extension, and this directive will silently fail if Tidy is not
|
|
|
|
available.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
If you are looking to make the overall look of your page's source
|
|
|
|
better, I recommend running Tidy on the entire page rather than just
|
|
|
|
user-content (after all, the indentation relative to the containing
|
|
|
|
blocks will be incorrect).
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
This directive was available since 1.1.1.
|
|
|
|
</p>
|
|
|
|
HTML
|
2006-09-24 21:23:54 +00:00
|
|
|
);
|
2007-05-19 00:24:23 +00:00
|
|
|
HTMLPurifier_ConfigSchema::defineAlias('Core', 'TidyFormat', 'Output', 'TidyFormat');
|
2006-09-24 21:23:54 +00:00
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from tokens.
|
2007-05-19 00:24:23 +00:00
|
|
|
* @todo Create a configuration-wide instance that all objects retrieve
|
2006-08-20 20:59:13 +00:00
|
|
|
*/
|
2006-07-23 00:11:03 +00:00
|
|
|
class HTMLPurifier_Generator
|
|
|
|
{
|
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
/**
|
2007-05-19 00:24:23 +00:00
|
|
|
* Bool cache of %HTML.XHTML
|
2006-10-09 16:07:35 +00:00
|
|
|
* @private
|
2006-09-16 00:37:33 +00:00
|
|
|
*/
|
|
|
|
var $_xhtml = true;
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
2007-05-19 00:24:23 +00:00
|
|
|
* Bool cache of %Output.CommentScriptContents
|
2007-05-15 01:17:10 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var $_scriptFix = false;
|
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
/**
|
|
|
|
* Cache of HTMLDefinition
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var $_def;
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from an array of tokens.
|
|
|
|
* @param $tokens Array of HTMLPurifier_Token
|
|
|
|
* @param $config HTMLPurifier_Config object
|
|
|
|
* @return Generated HTML
|
|
|
|
*/
|
2006-10-01 21:55:13 +00:00
|
|
|
function generateFromTokens($tokens, $config, &$context) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
2006-08-15 00:31:12 +00:00
|
|
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
2007-05-20 18:14:52 +00:00
|
|
|
$this->_scriptFix = $config->get('Output', 'CommentScriptContents');
|
2007-05-19 00:24:23 +00:00
|
|
|
|
2007-05-20 18:14:52 +00:00
|
|
|
$doctype = $config->getDoctype();
|
|
|
|
$this->_xhtml = $doctype->xml;
|
2007-05-19 00:24:23 +00:00
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
$this->_def = $config->getHTMLDefinition();
|
|
|
|
|
2006-07-30 18:37:42 +00:00
|
|
|
if (!$tokens) return '';
|
2007-05-15 01:17:10 +00:00
|
|
|
for ($i = 0, $size = count($tokens); $i < $size; $i++) {
|
|
|
|
if ($this->_scriptFix && $tokens[$i]->name === 'script') {
|
|
|
|
// script special case
|
|
|
|
$html .= $this->generateFromToken($tokens[$i++]);
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
while ($tokens[$i]->name != 'script') {
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$html .= $this->generateFromToken($tokens[$i]);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2007-05-19 00:24:23 +00:00
|
|
|
if ($config->get('Output', 'TidyFormat') && extension_loaded('tidy')) {
|
2006-09-24 21:23:54 +00:00
|
|
|
|
|
|
|
$tidy_options = array(
|
|
|
|
'indent'=> true,
|
|
|
|
'output-xhtml' => $this->_xhtml,
|
|
|
|
'show-body-only' => true,
|
|
|
|
'indent-spaces' => 2,
|
|
|
|
'wrap' => 68,
|
|
|
|
);
|
|
|
|
if (version_compare(PHP_VERSION, '5', '<')) {
|
|
|
|
tidy_set_encoding('utf8');
|
|
|
|
foreach ($tidy_options as $key => $value) {
|
|
|
|
tidy_setopt($key, $value);
|
|
|
|
}
|
|
|
|
tidy_parse_string($html);
|
|
|
|
tidy_clean_repair();
|
|
|
|
$html = tidy_get_output();
|
|
|
|
} else {
|
|
|
|
$tidy = new Tidy;
|
|
|
|
$tidy->parseString($html, $tidy_options, 'utf8');
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
$html = (string) $tidy;
|
|
|
|
}
|
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from a single token.
|
|
|
|
* @param $token HTMLPurifier_Token object.
|
|
|
|
* @return Generated HTML
|
|
|
|
*/
|
2006-08-19 17:53:59 +00:00
|
|
|
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') {
|
2007-05-21 01:36:15 +00:00
|
|
|
$attr = $this->generateAttributes($token->attr, $token->name);
|
2006-07-23 00:11:03 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
return '</' . $token->name . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'empty') {
|
2007-05-21 01:36:15 +00:00
|
|
|
$attr = $this->generateAttributes($token->attr, $token->name);
|
2006-09-16 00:37:33 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr .
|
|
|
|
( $this->_xhtml ? ' /': '' )
|
|
|
|
. '>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
} elseif ($token->type == 'text') {
|
2006-08-19 17:53:59 +00:00
|
|
|
return $this->escape($token->data);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
|
|
|
* Special case processor for the contents of script tags
|
|
|
|
* @warning This runs into problems if there's already a literal
|
|
|
|
* --> somewhere inside the script contents.
|
|
|
|
*/
|
|
|
|
function generateScriptFromToken($token) {
|
|
|
|
if (!$token->type == 'text') return $this->generateFromToken($token);
|
|
|
|
return '<!--' . PHP_EOL . $token->data . PHP_EOL . '// -->';
|
|
|
|
// more advanced version:
|
|
|
|
// return '<!--//--><![CDATA[//><!--' . PHP_EOL . $token->data . PHP_EOL . '//--><!]]>';
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates attribute declarations from attribute array.
|
|
|
|
* @param $assoc_array_of_attributes Attribute array
|
|
|
|
* @return Generate HTML fragment for insertion.
|
|
|
|
*/
|
2007-05-21 01:36:15 +00:00
|
|
|
function generateAttributes($assoc_array_of_attributes, $element) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
|
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
2006-09-16 00:37:33 +00:00
|
|
|
if (!$this->_xhtml) {
|
|
|
|
// remove namespaced attributes
|
|
|
|
if (strpos($key, ':') !== false) continue;
|
2007-05-21 01:36:15 +00:00
|
|
|
if (!empty($this->_def->info[$element]->attr[$key]->minimized)) {
|
|
|
|
$html .= $key . ' ';
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-16 00:37:33 +00:00
|
|
|
}
|
2006-08-19 17:53:59 +00:00
|
|
|
$html .= $key.'="'.$this->escape($value).'" ';
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
return rtrim($html);
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Escapes raw text data.
|
|
|
|
* @param $string String data to escape for HTML.
|
|
|
|
* @return String escaped data.
|
|
|
|
*/
|
2006-08-19 17:53:59 +00:00
|
|
|
function escape($string) {
|
|
|
|
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 03:27:40 +00:00
|
|
|
?>
|