List.php 2.7 KB

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