List.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // if li is not allowed, delete parent node
  38. if (!isset($config->getHTMLDefinition()->info['li'])) {
  39. trigger_error("Cannot allow ul/ol without allowing li", E_USER_WARNING);
  40. return false;
  41. }
  42. // the new set of children
  43. $result = array();
  44. // a little sanity check to make sure it's not ALL whitespace
  45. $all_whitespace = true;
  46. $current_li = null;
  47. foreach ($children as $node) {
  48. if (!empty($node->is_whitespace)) {
  49. $result[] = $node;
  50. continue;
  51. }
  52. $all_whitespace = false; // phew, we're not talking about whitespace
  53. if ($node->name === 'li') {
  54. // good
  55. $current_li = $node;
  56. $result[] = $node;
  57. } else {
  58. // we want to tuck this into the previous li
  59. // Invariant: we expect the node to be ol/ul
  60. // ToDo: Make this more robust in the case of not ol/ul
  61. // by distinguishing between existing li and li created
  62. // to handle non-list elements; non-list elements should
  63. // not be appended to an existing li; only li created
  64. // for non-list. This distinction is not currently made.
  65. if ($current_li === null) {
  66. $current_li = new HTMLPurifier_Node_Element('li');
  67. $result[] = $current_li;
  68. }
  69. $current_li->children[] = $node;
  70. $current_li->empty = false; // XXX fascinating! Check for this error elsewhere ToDo
  71. }
  72. }
  73. if (empty($result)) {
  74. return false;
  75. }
  76. if ($all_whitespace) {
  77. return false;
  78. }
  79. return $result;
  80. }
  81. }
  82. // vim: et sw=4 sts=4