File indexing completed on 2024-12-22 05:36:19

0001 <?php
0002 
0003 /**
0004  * Definition for list containers ul and ol.
0005  *
0006  * What does this do?  The big thing is to handle ol/ul at the top
0007  * level of list nodes, which should be handled specially by /folding/
0008  * them into the previous list node.  We generally shouldn't ever
0009  * see other disallowed elements, because the autoclose behavior
0010  * in MakeWellFormed handles it.
0011  */
0012 class HTMLPurifier_ChildDef_List extends HTMLPurifier_ChildDef
0013 {
0014     /**
0015      * @type string
0016      */
0017     public $type = 'list';
0018     /**
0019      * @type array
0020      */
0021     // lying a little bit, so that we can handle ul and ol ourselves
0022     // XXX: This whole business with 'wrap' is all a bit unsatisfactory
0023     public $elements = array('li' => true, 'ul' => true, 'ol' => true);
0024 
0025     /**
0026      * @param array $children
0027      * @param HTMLPurifier_Config $config
0028      * @param HTMLPurifier_Context $context
0029      * @return array
0030      */
0031     public function validateChildren($children, $config, $context)
0032     {
0033         // Flag for subclasses
0034         $this->whitespace = false;
0035 
0036         // if there are no tokens, delete parent node
0037         if (empty($children)) {
0038             return false;
0039         }
0040 
0041         // if li is not allowed, delete parent node
0042         if (!isset($config->getHTMLDefinition()->info['li'])) {
0043             trigger_error("Cannot allow ul/ol without allowing li", E_USER_WARNING);
0044             return false;
0045         }
0046 
0047         // the new set of children
0048         $result = array();
0049 
0050         // a little sanity check to make sure it's not ALL whitespace
0051         $all_whitespace = true;
0052 
0053         $current_li = null;
0054 
0055         foreach ($children as $node) {
0056             if (!empty($node->is_whitespace)) {
0057                 $result[] = $node;
0058                 continue;
0059             }
0060             $all_whitespace = false; // phew, we're not talking about whitespace
0061 
0062             if ($node->name === 'li') {
0063                 // good
0064                 $current_li = $node;
0065                 $result[] = $node;
0066             } else {
0067                 // we want to tuck this into the previous li
0068                 // Invariant: we expect the node to be ol/ul
0069                 // ToDo: Make this more robust in the case of not ol/ul
0070                 // by distinguishing between existing li and li created
0071                 // to handle non-list elements; non-list elements should
0072                 // not be appended to an existing li; only li created
0073                 // for non-list. This distinction is not currently made.
0074                 if ($current_li === null) {
0075                     $current_li = new HTMLPurifier_Node_Element('li');
0076                     $result[] = $current_li;
0077                 }
0078                 $current_li->children[] = $node;
0079                 $current_li->empty = false; // XXX fascinating! Check for this error elsewhere ToDo
0080             }
0081         }
0082         if (empty($result)) {
0083             return false;
0084         }
0085         if ($all_whitespace) {
0086             return false;
0087         }
0088         return $result;
0089     }
0090 }
0091 
0092 // vim: et sw=4 sts=4