Deal with old libxml incompatibilities.
[htmlpurifier.git] / library / HTMLPurifier / ChildDef / List.php
blob4fc70e0efa763887596e3ef9af1bd998bc6104a4
1 <?php
3 /**
4 * Definition for list containers ul and ol.
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.
12 class HTMLPurifier_ChildDef_List extends HTMLPurifier_ChildDef
14 /**
15 * @type string
17 public $type = 'list';
18 /**
19 * @type array
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);
25 /**
26 * @param array $children
27 * @param HTMLPurifier_Config $config
28 * @param HTMLPurifier_Context $context
29 * @return array
31 public function validateChildren($children, $config, $context)
33 // Flag for subclasses
34 $this->whitespace = false;
36 // if there are no tokens, delete parent node
37 if (empty($children)) {
38 return false;
41 // if li is not allowed, delete parent node
42 if (!isset($config->getHTMLDefinition()->info['li'])) {
43 trigger_error("Cannot allow ul/ol without allowing li", E_USER_WARNING);
44 return false;
47 // the new set of children
48 $result = array();
50 // a little sanity check to make sure it's not ALL whitespace
51 $all_whitespace = true;
53 $current_li = null;
55 foreach ($children as $node) {
56 if (!empty($node->is_whitespace)) {
57 $result[] = $node;
58 continue;
60 $all_whitespace = false; // phew, we're not talking about whitespace
62 if ($node->name === 'li') {
63 // good
64 $current_li = $node;
65 $result[] = $node;
66 } else {
67 // we want to tuck this into the previous li
68 // Invariant: we expect the node to be ol/ul
69 // ToDo: Make this more robust in the case of not ol/ul
70 // by distinguishing between existing li and li created
71 // to handle non-list elements; non-list elements should
72 // not be appended to an existing li; only li created
73 // for non-list. This distinction is not currently made.
74 if ($current_li === null) {
75 $current_li = new HTMLPurifier_Node_Element('li');
76 $result[] = $current_li;
78 $current_li->children[] = $node;
79 $current_li->empty = false; // XXX fascinating! Check for this error elsewhere ToDo
82 if (empty($result)) {
83 return false;
85 if ($all_whitespace) {
86 return false;
88 return $result;
92 // vim: et sw=4 sts=4