From 8913239b7fcfb880598c54ea48644ee43264f5b5 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 28 May 2007 03:49:06 +0000 Subject: [PATCH] Document Printer_ConfigForm. Factor out form controls to printer. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1106 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/Printer/ConfigForm.css | 1 + library/HTMLPurifier/Printer/ConfigForm.php | 50 ++++++++++- smoketests/configForm.php | 4 - smoketests/printDefinition.php | 92 ++------------------- 4 files changed, 57 insertions(+), 90 deletions(-) diff --git a/library/HTMLPurifier/Printer/ConfigForm.css b/library/HTMLPurifier/Printer/ConfigForm.css index f9c9c936..23c7f999 100644 --- a/library/HTMLPurifier/Printer/ConfigForm.css +++ b/library/HTMLPurifier/Printer/ConfigForm.css @@ -5,3 +5,4 @@ .hp-config thead, .hp-config .namespace {background:#3C578C; color:#FFF;} .hp-config .namespace th {text-align:center;} .hp-config .verbose {display:none;} +.hp-config .controls {text-align:center;} diff --git a/library/HTMLPurifier/Printer/ConfigForm.php b/library/HTMLPurifier/Printer/ConfigForm.php index c579dd99..f6bc5867 100644 --- a/library/HTMLPurifier/Printer/ConfigForm.php +++ b/library/HTMLPurifier/Printer/ConfigForm.php @@ -7,14 +7,19 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer /** * Printers for specific fields + * @protected */ var $fields = array(); /** * Documentation URL, can have fragment tagged on end + * @protected */ var $docURL; + /** + * @param $doc_url String documentation URL, will have fragment tagged on + */ function HTMLPurifier_Printer_ConfigForm($doc_url = null) { parent::HTMLPurifier_Printer(); $this->docURL = $doc_url; @@ -22,9 +27,21 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer $this->fields['bool'] = new HTMLPurifier_Printer_ConfigForm_bool(); } - function render($config) { + /** + * Returns HTML output for a configuration form + * @param $config Configuration object of current form state + * @param $ns Optional namespace(s) to restrict form to + */ + function render($config, $ns = true) { $this->config = $config; - $all = $config->getAll(); + if ($ns === true) { + $all = $config->getAll(); + } else { + if (is_string($ns)) $ns = array($ns); + foreach ($ns as $n) { + $all = array($n => $config->getBatch($n)); + } + } $ret = ''; $ret .= $this->start('table', array('class' => 'hp-config')); $ret .= $this->start('thead'); @@ -36,10 +53,23 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer foreach ($all as $ns => $directives) { $ret .= $this->renderNamespace($ns, $directives); } + $ret .= $this->start('tfoot'); + $ret .= $this->start('tr'); + $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls')); + $ret .= ' [Reset]'; + $ret .= $this->end('td'); + $ret .= $this->end('tr'); + $ret .= $this->end('tfoot'); $ret .= $this->end('table'); return $ret; } + /** + * Renders a single namespace + * @param $ns String namespace name + * @param $directive Associative array of directives to values + * @protected + */ function renderNamespace($ns, $directives) { $ret = ''; $ret .= $this->start('tbody', array('class' => 'namespace')); @@ -78,8 +108,17 @@ class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer } +/** + * Printer decorator for directives that accept null + */ class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer { + /** + * Printer being decorated + */ var $obj; + /** + * @param $obj Printer to decorate + */ function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) { parent::HTMLPurifier_Printer(); $this->obj = $obj; @@ -107,8 +146,12 @@ class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer } } +/** + * Swiss-army knife configuration form field printer + */ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer { function render($ns, $directive, $value, $config) { + // this should probably be split up a little $ret = ''; $def = $config->def->info[$ns][$directive]; if (is_array($value)) { @@ -159,6 +202,9 @@ class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer { } } +/** + * Bool form field printer + */ class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer { function render($ns, $directive, $value, $config) { $ret = ''; diff --git a/smoketests/configForm.php b/smoketests/configForm.php index d488e5cc..db250e15 100644 --- a/smoketests/configForm.php +++ b/smoketests/configForm.php @@ -67,10 +67,6 @@ $printer = new HTMLPurifier_Printer_ConfigForm('?doc'); echo $printer->render($config); ?> -
- - [Reset] -
  $value) {
-    if (!strncmp($key, 'Null_', 5) && !empty($value)) {
-        unset($get[substr($key, 5)]);
-        unset($get[$key]);
-    }
-}
-
-@$config->loadArray($get);
-
 /* // sample local definition, obviously needs to be less clunky
 $html_definition =& $config->getHTMLDefinition(true);
 $module = new HTMLPurifier_HTMLModule();
@@ -45,9 +36,7 @@ echo '';
     HTML Purifier Printer Smoketest
     
     
-    
+    
+    
 
 
 
@@ -81,73 +67,11 @@ influences the internal workings of the definition objects.

list of items, HTML Purifier will take care of the rest (including transformation into a real array list or a lookup table).

-
- +getBatch('HTML'); - // can't handle hashes - foreach ($directives as $key => $value) { - $directive = "HTML.$key"; - if (is_array($value)) { - $keys = array_keys($value); - if ($keys === array_keys($keys)) { - $value = implode(',', $keys); - } else { - $new_value = ''; - foreach ($value as $name => $bool) { - if ($bool !== true) continue; - $new_value .= "$name,"; - } - $value = rtrim($new_value, ','); - } - } - $allow_null = $config->def->info['HTML'][$key]->allow_null; + $printer = new HTMLPurifier_Printer_ConfigForm('http://htmlpurifier.org/live/configdoc/plain.html'); + echo $printer->render($config, 'HTML'); ?> - - - - - - - - - -
- - - - - - checked="checked" />   - - checked="checked" /> - - - - - checked="checked" - /> or
- - - /> - -
- [Reset] - -

* Some configuration directives make a distinction between an empty variable and a null variable. A whitelist, for example, will take an empty array as meaning no allowed elements, while checking