mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-05 13:47:24 +02:00
[3.1.1] Land vs's HTMLPurifier_Generator patch, and a number of other bugfixes for that change
- Convert a number of calls to use new constructor signature for Generator - Make generator require configuration; this exposes a number of latent bugs - Removed generator hack - Convert Printers to use new optimized ConfigSchema format - Hack with Printer configuration; pass an array(generator config, render config) to distinguish between output and target. - HTML/CSS Printers need to be primed, otherwise fatal errors - Convert a few test-cases to use member properties git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1770 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -55,10 +55,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
|
||||
$escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
|
||||
|
||||
// generator
|
||||
static $gen = null;
|
||||
if ($gen === null) {
|
||||
$gen = new HTMLPurifier_Generator();
|
||||
}
|
||||
$gen = new HTMLPurifier_Generator($config, $context);
|
||||
|
||||
foreach ($tokens_of_children as $token) {
|
||||
if (!empty($token->is_whitespace)) {
|
||||
@@ -83,7 +80,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
|
||||
$result[] = $token;
|
||||
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
||||
$result[] = new HTMLPurifier_Token_Text(
|
||||
$gen->generateFromToken($token, $config)
|
||||
$gen->generateFromToken($token)
|
||||
);
|
||||
}
|
||||
continue;
|
||||
@@ -94,7 +91,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
|
||||
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
||||
$result[] =
|
||||
new HTMLPurifier_Token_Text(
|
||||
$gen->generateFromToken( $token, $config )
|
||||
$gen->generateFromToken($token)
|
||||
);
|
||||
} else {
|
||||
// drop silently
|
||||
|
@@ -35,8 +35,7 @@ class HTMLPurifier_Generator
|
||||
* @param $config Instance of HTMLPurifier_Config
|
||||
* @param $context Instance of HTMLPurifier_Context
|
||||
*/
|
||||
public function __construct($config = null, $context = null) {
|
||||
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
||||
public function __construct($config, $context) {
|
||||
$this->config = $config;
|
||||
$this->_scriptFix = $config->get('Output', 'CommentScriptContents');
|
||||
$this->_def = $config->getHTMLDefinition();
|
||||
|
@@ -20,18 +20,15 @@ class HTMLPurifier_Printer
|
||||
* Initialize $generator.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->generator = new HTMLPurifier_Generator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Give generator necessary configuration if possible
|
||||
*/
|
||||
public function prepareGenerator($config) {
|
||||
// hack for smoketests/configForm.php
|
||||
$all = $config->getAll();
|
||||
if (empty($all['HTML'])) return;
|
||||
$context = new HTMLPurifier_Context();
|
||||
$this->generator->generateFromTokens(array(), $config, $context);
|
||||
$this->generator = new HTMLPurifier_Generator($config, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @todo Rewrite to use Interchange objects
|
||||
*/
|
||||
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
|
||||
{
|
||||
|
||||
@@ -38,8 +41,8 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
|
||||
$this->name = $name;
|
||||
$this->compress = $compress;
|
||||
// initialize sub-printers
|
||||
$this->fields['default'] = new HTMLPurifier_Printer_ConfigForm_default();
|
||||
$this->fields['bool'] = new HTMLPurifier_Printer_ConfigForm_bool();
|
||||
$this->fields[0] = new HTMLPurifier_Printer_ConfigForm_default();
|
||||
$this->fields[HTMLPurifier_VarParser::BOOL] = new HTMLPurifier_Printer_ConfigForm_bool();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,14 +71,23 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
|
||||
|
||||
/**
|
||||
* Returns HTML output for a configuration form
|
||||
* @param $config Configuration object of current form state
|
||||
* @param $config Configuration object of current form state, or an array
|
||||
* where [0] has an HTML namespace and [1] is being rendered.
|
||||
* @param $allowed Optional namespace(s) and directives to restrict form to.
|
||||
*/
|
||||
public function render($config, $allowed = true, $render_controls = true) {
|
||||
$this->config = $config;
|
||||
$this->prepareGenerator($config);
|
||||
if (is_array($config) && isset($config[0])) {
|
||||
$gen_config = $config[0];
|
||||
$config = $config[1];
|
||||
} else {
|
||||
$gen_config = $config;
|
||||
}
|
||||
|
||||
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
|
||||
$this->config = $config;
|
||||
$this->genConfig = $gen_config;
|
||||
$this->prepareGenerator($gen_config);
|
||||
|
||||
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
|
||||
$all = array();
|
||||
foreach ($allowed as $key) {
|
||||
list($ns, $directive) = $key;
|
||||
@@ -148,13 +160,19 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
|
||||
|
||||
$ret .= $this->start('td');
|
||||
$def = $this->config->def->info[$ns][$directive];
|
||||
$type = $def->type;
|
||||
if (!isset($this->fields[$type])) $type = 'default';
|
||||
if (is_int($def)) {
|
||||
$allow_null = $def < 0;
|
||||
$type = abs($def);
|
||||
} else {
|
||||
$type = $def->type;
|
||||
$allow_null = isset($def->allow_null);
|
||||
}
|
||||
if (!isset($this->fields[$type])) $type = 0; // default
|
||||
$type_obj = $this->fields[$type];
|
||||
if ($def->allow_null) {
|
||||
if ($allow_null) {
|
||||
$type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
|
||||
}
|
||||
$ret .= $type_obj->render($ns, $directive, $value, $this->name, $this->config);
|
||||
$ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
|
||||
$ret .= $this->end('td');
|
||||
$ret .= $this->end('tr');
|
||||
}
|
||||
@@ -180,7 +198,14 @@ class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer
|
||||
$this->obj = $obj;
|
||||
}
|
||||
public function render($ns, $directive, $value, $name, $config) {
|
||||
$this->prepareGenerator($config);
|
||||
if (is_array($config) && isset($config[0])) {
|
||||
$gen_config = $config[0];
|
||||
$config = $config[1];
|
||||
} else {
|
||||
$gen_config = $config;
|
||||
}
|
||||
$this->prepareGenerator($gen_config);
|
||||
|
||||
$ret = '';
|
||||
$ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
|
||||
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
|
||||
@@ -202,7 +227,7 @@ class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer
|
||||
$ret .= $this->elementEmpty('input', $attr);
|
||||
$ret .= $this->text(' or ');
|
||||
$ret .= $this->elementEmpty('br');
|
||||
$ret .= $this->obj->render($ns, $directive, $value, $name, $config);
|
||||
$ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
@@ -214,22 +239,33 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
||||
public $cols = 18;
|
||||
public $rows = 5;
|
||||
public function render($ns, $directive, $value, $name, $config) {
|
||||
$this->prepareGenerator($config);
|
||||
if (is_array($config) && isset($config[0])) {
|
||||
$gen_config = $config[0];
|
||||
$config = $config[1];
|
||||
} else {
|
||||
$gen_config = $config;
|
||||
}
|
||||
$this->prepareGenerator($gen_config);
|
||||
// this should probably be split up a little
|
||||
$ret = '';
|
||||
$def = $config->def->info[$ns][$directive];
|
||||
if (is_int($def)) {
|
||||
$type = abs($def);
|
||||
} else {
|
||||
$type = $def->type;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
switch ($def->type) {
|
||||
case 'lookup':
|
||||
switch ($type) {
|
||||
case HTMLPurifier_VarParser::LOOKUP:
|
||||
$array = $value;
|
||||
$value = array();
|
||||
foreach ($array as $val => $b) {
|
||||
$value[] = $val;
|
||||
}
|
||||
case 'list':
|
||||
case HTMLPurifier_VarParser::ALIST:
|
||||
$value = implode(PHP_EOL, $value);
|
||||
break;
|
||||
case 'hash':
|
||||
case HTMLPurifier_VarParser::HASH:
|
||||
$nvalue = '';
|
||||
foreach ($value as $i => $v) {
|
||||
$nvalue .= "$i:$v" . PHP_EOL;
|
||||
@@ -240,7 +276,7 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
||||
$value = '';
|
||||
}
|
||||
}
|
||||
if ($def->type === 'mixed') {
|
||||
if ($type === HTMLPurifier_VarParser::MIXED) {
|
||||
return 'Not supported';
|
||||
$value = serialize($value);
|
||||
}
|
||||
@@ -249,7 +285,7 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
||||
'id' => "$name:$ns.$directive"
|
||||
);
|
||||
if ($value === null) $attr['disabled'] = 'disabled';
|
||||
if (is_array($def->allowed)) {
|
||||
if (isset($def->allowed)) {
|
||||
$ret .= $this->start('select', $attr);
|
||||
foreach ($def->allowed as $val => $b) {
|
||||
$attr = array();
|
||||
@@ -258,8 +294,11 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
||||
}
|
||||
$ret .= $this->end('select');
|
||||
} elseif (
|
||||
$def->type == 'text' || $def->type == 'itext' ||
|
||||
$def->type == 'list' || $def->type == 'hash' || $def->type == 'lookup'
|
||||
$type === HTMLPurifier_VarParser::TEXT ||
|
||||
$type === HTMLPurifier_VarParser::ITEXT ||
|
||||
$type === HTMLPurifier_VarParser::ALIST ||
|
||||
$type === HTMLPurifier_VarParser::HASH ||
|
||||
$type === HTMLPurifier_VarParser::LOOKUP
|
||||
) {
|
||||
$attr['cols'] = $this->cols;
|
||||
$attr['rows'] = $this->rows;
|
||||
@@ -280,7 +319,13 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
||||
*/
|
||||
class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
|
||||
public function render($ns, $directive, $value, $name, $config) {
|
||||
$this->prepareGenerator($config);
|
||||
if (is_array($config) && isset($config[0])) {
|
||||
$gen_config = $config[0];
|
||||
$config = $config[1];
|
||||
} else {
|
||||
$gen_config = $config;
|
||||
}
|
||||
$this->prepareGenerator($gen_config);
|
||||
$ret = '';
|
||||
$ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
|
||||
|
||||
|
@@ -18,7 +18,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
||||
|
||||
// local variables
|
||||
$result = array();
|
||||
$generator = new HTMLPurifier_Generator();
|
||||
$generator = new HTMLPurifier_Generator($config, $context);
|
||||
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
|
||||
$e = $context->get('ErrorCollector', true);
|
||||
|
||||
@@ -169,7 +169,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
||||
if ($escape_invalid_tags) {
|
||||
if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text');
|
||||
$result[] = new HTMLPurifier_Token_Text(
|
||||
$generator->generateFromToken($token, $config, $context)
|
||||
$generator->generateFromToken($token)
|
||||
);
|
||||
} elseif ($e) {
|
||||
$e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed');
|
||||
@@ -209,7 +209,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
||||
if ($skipped_tags === false) {
|
||||
if ($escape_invalid_tags) {
|
||||
$result[] = new HTMLPurifier_Token_Text(
|
||||
$generator->generateFromToken($token, $config, $context)
|
||||
$generator->generateFromToken($token)
|
||||
);
|
||||
if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text');
|
||||
} elseif ($e) {
|
||||
|
@@ -13,7 +13,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
|
||||
|
||||
public function execute($tokens, $config, $context) {
|
||||
$definition = $config->getHTMLDefinition();
|
||||
$generator = new HTMLPurifier_Generator();
|
||||
$generator = new HTMLPurifier_Generator($config, $context);
|
||||
$result = array();
|
||||
|
||||
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
|
||||
@@ -101,7 +101,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
|
||||
// invalid tag, generate HTML representation and insert in
|
||||
if ($e) $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text');
|
||||
$token = new HTMLPurifier_Token_Text(
|
||||
$generator->generateFromToken($token, $config, $context)
|
||||
$generator->generateFromToken($token)
|
||||
);
|
||||
} else {
|
||||
// check if we need to destroy all of the tag's children
|
||||
|
Reference in New Issue
Block a user