List.php 3.0 KB

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