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 '.
|
2007-06-21 14:44:26 +00:00
|
|
|
'directive was available since 2.0.0.'
|
2007-05-15 01:17:10 +00:00
|
|
|
);
|
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
|
|
|
|
2007-06-25 19:18:55 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define('Output', 'Newline', null, 'string/null', '
|
|
|
|
<p>
|
|
|
|
Newline string to format final output with. If left null, HTML Purifier
|
|
|
|
will auto-detect the default newline type of the system and use that;
|
|
|
|
you can manually override it here. Remember, \r\n is Windows, \r
|
|
|
|
is Mac, and \n is Unix. This directive was available since 2.0.1.
|
|
|
|
</p>
|
|
|
|
');
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from tokens.
|
2007-06-26 02:49:21 +00:00
|
|
|
* @todo Refactor interface so that configuration/context is determined
|
2007-11-25 02:24:39 +00:00
|
|
|
* upon instantiation, no need for messy generateFromTokens() calls
|
|
|
|
* @todo Make some of the more internal functions protected, and have
|
|
|
|
* unit tests work around that
|
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
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_xhtml = true;
|
2006-09-16 00:37:33 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_scriptFix = false;
|
2007-05-15 01:17:10 +00:00
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
/**
|
|
|
|
* Cache of HTMLDefinition
|
|
|
|
* @private
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_def;
|
2007-05-21 01:36:15 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2008-01-05 00:10:43 +00:00
|
|
|
public 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-21 01:36:15 +00:00
|
|
|
$this->_def = $config->getHTMLDefinition();
|
2007-05-23 03:27:36 +00:00
|
|
|
$this->_xhtml = $this->_def->doctype->xml;
|
2007-05-21 01:36:15 +00:00
|
|
|
|
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++) {
|
2007-06-21 14:44:26 +00:00
|
|
|
if ($this->_scriptFix && $tokens[$i]->name === 'script'
|
2008-01-19 20:23:01 +00:00
|
|
|
&& $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) {
|
2007-05-15 01:17:10 +00:00
|
|
|
// script special case
|
2007-06-21 14:44:26 +00:00
|
|
|
// the contents of the script block must be ONE token
|
|
|
|
// for this to work
|
2007-05-15 01:17:10 +00:00
|
|
|
$html .= $this->generateFromToken($tokens[$i++]);
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
2007-06-21 14:44:26 +00:00
|
|
|
// We're not going to do this: it wouldn't be valid anyway
|
|
|
|
//while ($tokens[$i]->name != 'script') {
|
|
|
|
// $html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
//}
|
2007-05-15 01:17:10 +00:00
|
|
|
}
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2007-06-25 19:18:55 +00:00
|
|
|
// normalize newlines to system
|
|
|
|
$nl = $config->get('Output', 'Newline');
|
|
|
|
if ($nl === null) $nl = PHP_EOL;
|
|
|
|
$html = str_replace("\n", $nl, $html);
|
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
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function generateFromToken($token) {
|
2008-01-19 20:23:01 +00:00
|
|
|
if (!$token instanceof HTMLPurifier_Token) return '';
|
|
|
|
if ($token instanceof HTMLPurifier_Token_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 . '>';
|
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_End) {
|
2006-07-23 00:11:03 +00:00
|
|
|
return '</' . $token->name . '>';
|
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_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
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_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.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function generateScriptFromToken($token) {
|
2008-01-19 20:23:01 +00:00
|
|
|
if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token);
|
2007-06-25 19:18:55 +00:00
|
|
|
// return '<!--' . "\n" . trim($token->data) . "\n" . '// -->';
|
2007-05-15 01:17:10 +00:00
|
|
|
// more advanced version:
|
2007-06-21 14:44:26 +00:00
|
|
|
// thanks <http://lachy.id.au/log/2005/05/script-comments>
|
|
|
|
$data = preg_replace('#//\s*$#', '', $token->data);
|
2007-06-25 19:18:55 +00:00
|
|
|
return '<!--//--><![CDATA[//><!--' . "\n" . trim($data) . "\n" . '//--><!]]>';
|
2007-05-15 01:17:10 +00:00
|
|
|
}
|
|
|
|
|
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-11-25 02:24:39 +00:00
|
|
|
public 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.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function escape($string) {
|
2006-08-19 17:53:59 +00:00
|
|
|
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|