1<?php
2
3/**
4 * Definition for list containers ul and ol.
5 *
6 * What does this do?  The big thing is to handle ol/ul at the top
7 * level of list nodes, which should be handled specially by /folding/
8 * them into the previous list node.  We generally shouldn't ever
9 * see other disallowed elements, because the autoclose behavior
10 * in MakeWellFormed handles it.
11 */
12class HTMLPurifier_ChildDef_List extends HTMLPurifier_ChildDef
13{
14    /**
15     * @type string
16     */
17    public $type = 'list';
18    /**
19     * @type array
20     */
21    // lying a little bit, so that we can handle ul and ol ourselves
22    // XXX: This whole business with 'wrap' is all a bit unsatisfactory
23    public $elements = array('li' => true, 'ul' => true, 'ol' => true);
24
25    public $whitespace;
26
27    /**
28     * @param array $children
29     * @param HTMLPurifier_Config $config
30     * @param HTMLPurifier_Context $context
31     * @return array
32     */
33    public function validateChildren($children, $config, $context)
34    {
35        // Flag for subclasses
36        $this->whitespace = false;
37
38        // if there are no tokens, delete parent node
39        if (empty($children)) {
40            return false;
41        }
42
43        // if li is not allowed, delete parent node
44        if (!isset($config->getHTMLDefinition()->info['li'])) {
45            trigger_error("Cannot allow ul/ol without allowing li", E_USER_WARNING);
46            return false;
47        }
48
49        // the new set of children
50        $result = array();
51
52        // a little sanity check to make sure it's not ALL whitespace
53        $all_whitespace = true;
54
55        $current_li = null;
56
57        foreach ($children as $node) {
58            if (!empty($node->is_whitespace)) {
59                $result[] = $node;
60                continue;
61            }
62            $all_whitespace = false; // phew, we're not talking about whitespace
63
64            if ($node->name === 'li') {
65                // good
66                $current_li = $node;
67                $result[] = $node;
68            } else {
69                // we want to tuck this into the previous li
70                // Invariant: we expect the node to be ol/ul
71                // ToDo: Make this more robust in the case of not ol/ul
72                // by distinguishing between existing li and li created
73                // to handle non-list elements; non-list elements should
74                // not be appended to an existing li; only li created
75                // for non-list. This distinction is not currently made.
76                if ($current_li === null) {
77                    $current_li = new HTMLPurifier_Node_Element('li');
78                    $result[] = $current_li;
79                }
80                $current_li->children[] = $node;
81                $current_li->empty = false; // XXX fascinating! Check for this error elsewhere ToDo
82            }
83        }
84        if (empty($result)) {
85            return false;
86        }
87        if ($all_whitespace) {
88            return false;
89        }
90        return $result;
91    }
92}
93
94// vim: et sw=4 sts=4
95