2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
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
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Whether or not generator should produce XML output.
|
|
|
|
* @type bool
|
2006-09-16 00:37:33 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_xhtml = true;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* :HACK: Whether or not generator should comment the insides of <script> tags.
|
|
|
|
* @type bool
|
2007-05-15 01:17:10 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_scriptFix = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
/**
|
2008-04-28 01:35:07 +00:00
|
|
|
* Cache of HTMLDefinition during HTML output to determine whether or
|
|
|
|
* not attributes should be minimized.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type HTMLPurifier_HTMLDefinition
|
2007-05-21 01:36:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_def;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-25 02:36:27 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Cache of %Output.SortAttr.
|
|
|
|
* @type bool
|
2008-06-25 02:36:27 +00:00
|
|
|
*/
|
|
|
|
private $_sortAttr;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2010-03-08 06:56:40 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Cache of %Output.FlashCompat.
|
|
|
|
* @type bool
|
2010-03-08 06:56:40 +00:00
|
|
|
*/
|
|
|
|
private $_flashCompat;
|
|
|
|
|
2011-03-27 10:50:52 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Cache of %Output.FixInnerHTML.
|
|
|
|
* @type bool
|
2011-03-27 10:50:52 +00:00
|
|
|
*/
|
|
|
|
private $_innerHTMLFix;
|
|
|
|
|
2010-03-08 06:56:40 +00:00
|
|
|
/**
|
|
|
|
* Stack for keeping track of object information when outputting IE
|
|
|
|
* compatibility code.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type array
|
2010-03-08 06:56:40 +00:00
|
|
|
*/
|
|
|
|
private $_flashStack = array();
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
/**
|
|
|
|
* Configuration for the generator
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type HTMLPurifier_Config
|
2008-04-28 01:35:07 +00:00
|
|
|
*/
|
2008-05-13 03:17:38 +00:00
|
|
|
protected $config;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
2008-04-28 01:35:07 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct($config, $context)
|
|
|
|
{
|
2008-05-13 03:17:38 +00:00
|
|
|
$this->config = $config;
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->_scriptFix = $config->get('Output.CommentScriptContents');
|
2011-03-27 10:50:52 +00:00
|
|
|
$this->_innerHTMLFix = $config->get('Output.FixInnerHTML');
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->_sortAttr = $config->get('Output.SortAttr');
|
2010-03-08 06:56:40 +00:00
|
|
|
$this->_flashCompat = $config->get('Output.FlashCompat');
|
2008-04-28 01:35:07 +00:00
|
|
|
$this->_def = $config->getHTMLDefinition();
|
|
|
|
$this->_xhtml = $this->_def->doctype->xml;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from an array of tokens.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Token[] $tokens Array of HTMLPurifier_Token
|
|
|
|
* @return string Generated HTML
|
2006-08-20 20:59:13 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function generateFromTokens($tokens)
|
|
|
|
{
|
|
|
|
if (!$tokens) {
|
|
|
|
return '';
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
// Basic algorithm
|
|
|
|
$html = '';
|
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
|
2008-04-28 01:35:07 +00:00
|
|
|
// for this to work.
|
2007-05-15 01:17:10 +00:00
|
|
|
$html .= $this->generateFromToken($tokens[$i++]);
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
}
|
|
|
|
$html .= $this->generateFromToken($tokens[$i]);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
// Tidy cleanup
|
2009-02-20 00:17:49 +00:00
|
|
|
if (extension_loaded('tidy') && $this->config->get('Output.TidyFormat')) {
|
2008-04-28 01:35:07 +00:00
|
|
|
$tidy = new Tidy;
|
2013-07-16 11:56:14 +00:00
|
|
|
$tidy->parseString(
|
|
|
|
$html,
|
|
|
|
array(
|
|
|
|
'indent'=> true,
|
|
|
|
'output-xhtml' => $this->_xhtml,
|
|
|
|
'show-body-only' => true,
|
|
|
|
'indent-spaces' => 2,
|
|
|
|
'wrap' => 68,
|
|
|
|
),
|
|
|
|
'utf8'
|
|
|
|
);
|
2008-04-28 01:35:07 +00:00
|
|
|
$tidy->cleanRepair();
|
2008-05-15 23:04:46 +00:00
|
|
|
$html = (string) $tidy; // explicit cast necessary
|
2006-09-24 21:23:54 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
// Normalize newlines to system defined value
|
2010-09-15 06:49:24 +00:00
|
|
|
if ($this->config->get('Core.NormalizeNewlines')) {
|
|
|
|
$nl = $this->config->get('Output.Newline');
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($nl === null) {
|
|
|
|
$nl = PHP_EOL;
|
|
|
|
}
|
|
|
|
if ($nl !== "\n") {
|
|
|
|
$html = str_replace("\n", $nl, $html);
|
|
|
|
}
|
2010-09-15 06:49:24 +00:00
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
return $html;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from a single token.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Token $token HTMLPurifier_Token object.
|
|
|
|
* @return string Generated HTML
|
2006-08-20 20:59:13 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function generateFromToken($token)
|
|
|
|
{
|
2008-04-28 01:35:07 +00:00
|
|
|
if (!$token instanceof HTMLPurifier_Token) {
|
|
|
|
trigger_error('Cannot generate HTML from non-HTMLPurifier_Token object', E_USER_WARNING);
|
|
|
|
return '';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_Start) {
|
2007-05-21 01:36:15 +00:00
|
|
|
$attr = $this->generateAttributes($token->attr, $token->name);
|
2010-03-08 06:56:40 +00:00
|
|
|
if ($this->_flashCompat) {
|
|
|
|
if ($token->name == "object") {
|
|
|
|
$flash = new stdclass();
|
|
|
|
$flash->attr = $token->attr;
|
|
|
|
$flash->param = array();
|
|
|
|
$this->_flashStack[] = $flash;
|
|
|
|
}
|
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_End) {
|
2010-03-08 06:56:40 +00:00
|
|
|
$_extra = '';
|
|
|
|
if ($this->_flashCompat) {
|
|
|
|
if ($token->name == "object" && !empty($this->_flashStack)) {
|
2010-05-21 16:56:37 +00:00
|
|
|
// doesn't do anything for now
|
2010-03-08 06:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $_extra . '</' . $token->name . '>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_Empty) {
|
2010-03-08 06:56:40 +00:00
|
|
|
if ($this->_flashCompat && $token->name == "param" && !empty($this->_flashStack)) {
|
|
|
|
$this->_flashStack[count($this->_flashStack)-1]->param[$token->attr['name']] = $token->attr['value'];
|
|
|
|
}
|
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 .
|
2008-04-28 01:35:07 +00:00
|
|
|
( $this->_xhtml ? ' /': '' ) // <br /> v. <br>
|
2006-09-16 00:37:33 +00:00
|
|
|
. '>';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_Text) {
|
2008-04-28 01:35:07 +00:00
|
|
|
return $this->escape($token->data, ENT_NOQUOTES);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-02-20 00:15:44 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_Comment) {
|
|
|
|
return '<!--' . $token->data . '-->';
|
2006-07-23 00:11:03 +00:00
|
|
|
} else {
|
|
|
|
return '';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
|
|
|
* Special case processor for the contents of script tags
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Token $token HTMLPurifier_Token object.
|
|
|
|
* @return string
|
2007-05-15 01:17:10 +00:00
|
|
|
* @warning This runs into problems if there's already a literal
|
|
|
|
* --> somewhere inside the script contents.
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function generateScriptFromToken($token)
|
|
|
|
{
|
|
|
|
if (!$token instanceof HTMLPurifier_Token_Text) {
|
|
|
|
return $this->generateFromToken($token);
|
|
|
|
}
|
2008-04-28 01:35:07 +00:00
|
|
|
// Thanks <http://lachy.id.au/log/2005/05/script-comments>
|
2007-06-21 14:44:26 +00:00
|
|
|
$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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates attribute declarations from attribute array.
|
2008-04-28 01:35:07 +00:00
|
|
|
* @note This does not include the leading or trailing space.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param array $assoc_array_of_attributes Attribute array
|
|
|
|
* @param string $element Name of element attributes are for, used to check
|
2008-04-28 01:35:07 +00:00
|
|
|
* attribute minimization.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @return string Generated HTML fragment for insertion.
|
2006-08-20 20:59:13 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function generateAttributes($assoc_array_of_attributes, $element = '')
|
|
|
|
{
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($this->_sortAttr) {
|
|
|
|
ksort($assoc_array_of_attributes);
|
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
2006-09-16 00:37:33 +00:00
|
|
|
if (!$this->_xhtml) {
|
2008-04-28 01:35:07 +00:00
|
|
|
// Remove namespaced attributes
|
2013-07-16 11:56:14 +00:00
|
|
|
if (strpos($key, ':') !== false) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-04-28 01:35:07 +00:00
|
|
|
// Check if we should minimize the attribute: val="val" -> val
|
|
|
|
if ($element && !empty($this->_def->info[$element]->attr[$key]->minimized)) {
|
2007-05-21 01:36:15 +00:00
|
|
|
$html .= $key . ' ';
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-16 00:37:33 +00:00
|
|
|
}
|
2011-03-27 10:50:52 +00:00
|
|
|
// Workaround for Internet Explorer innerHTML bug.
|
|
|
|
// Essentially, Internet Explorer, when calculating
|
|
|
|
// innerHTML, omits quotes if there are no instances of
|
|
|
|
// angled brackets, quotes or spaces. However, when parsing
|
|
|
|
// HTML (for example, when you assign to innerHTML), it
|
|
|
|
// treats backticks as quotes. Thus,
|
|
|
|
// <img alt="``" />
|
|
|
|
// becomes
|
|
|
|
// <img alt=`` />
|
|
|
|
// becomes
|
|
|
|
// <img alt='' />
|
|
|
|
// Fortunately, all we need to do is trigger an appropriate
|
|
|
|
// quoting style, which we do by adding an extra space.
|
|
|
|
// This also is consistent with the W3C spec, which states
|
|
|
|
// that user agents may ignore leading or trailing
|
|
|
|
// whitespace (in fact, most don't, at least for attributes
|
|
|
|
// like alt, but an extra space at the end is barely
|
|
|
|
// noticeable). Still, we have a configuration knob for
|
|
|
|
// this, since this transformation is not necesary if you
|
|
|
|
// don't process user input with innerHTML or you don't plan
|
|
|
|
// on supporting Internet Explorer.
|
|
|
|
if ($this->_innerHTMLFix) {
|
|
|
|
if (strpos($value, '`') !== false) {
|
|
|
|
// check if correct quoting style would not already be
|
|
|
|
// triggered
|
|
|
|
if (strcspn($value, '"\' <>') === strlen($value)) {
|
|
|
|
// protect!
|
|
|
|
$value .= ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-19 17:53:59 +00:00
|
|
|
$html .= $key.'="'.$this->escape($value).'" ';
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
return rtrim($html);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Escapes raw text data.
|
2008-04-28 01:35:07 +00:00
|
|
|
* @todo This really ought to be protected, but until we have a facility
|
|
|
|
* for properly generating HTML here w/o using tokens, it stays
|
|
|
|
* public.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $string String data to escape for HTML.
|
|
|
|
* @param int $quote Quoting style, like htmlspecialchars. ENT_NOQUOTES is
|
2008-04-28 01:35:07 +00:00
|
|
|
* permissible for non-attribute output.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @return string escaped data.
|
2006-08-20 20:59:13 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function escape($string, $quote = null)
|
|
|
|
{
|
2010-08-27 04:40:58 +00:00
|
|
|
// Workaround for APC bug on Mac Leopard reported by sidepodcast
|
|
|
|
// http://htmlpurifier.org/phorum/read.php?3,4823,4846
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($quote === null) {
|
|
|
|
$quote = ENT_COMPAT;
|
|
|
|
}
|
2008-04-28 01:35:07 +00:00
|
|
|
return htmlspecialchars($string, $quote, 'UTF-8');
|
2006-08-19 17:53:59 +00:00
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|