PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/library/HTMLPurifier/ChildDef/Table.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 178 lines | 134 code | 20 blank | 24 comment | 22 complexity | c752e93138d277711f9ddfed0fe8037b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Definition for tables
  4. */
  5. class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
  6. {
  7. public $allow_empty = false;
  8. public $type = 'table';
  9. public $elements = array('tr' => true, 'tbody' => true, 'thead' => true, 'tfoot' => true, 'caption' => true,
  10. 'colgroup' => true, 'col' => true);
  11. public function __construct()
  12. {
  13. }
  14. public function validateChildren($tokens_of_children, $config, $context)
  15. {
  16. if (empty($tokens_of_children))
  17. return false;
  18. // this ensures that the loop gets run one last time before closing
  19. // up. It's a little bit of a hack, but it works! Just make sure you
  20. // get rid of the token later.
  21. $tokens_of_children[] = false;
  22. // only one of these elements is allowed in a table
  23. $caption = false;
  24. $thead = false;
  25. $tfoot = false;
  26. // as many of these as you want
  27. $cols = array();
  28. $content = array();
  29. $nesting = 0; // current depth so we can determine nodes
  30. $is_collecting = false; // are we globbing together tokens to package
  31. // into one of the collectors?
  32. $collection = array(); // collected nodes
  33. $tag_index = 0; // the first node might be whitespace,
  34. // so this tells us where the start tag is
  35. foreach ($tokens_of_children as $token)
  36. {
  37. $is_child = ($nesting == 0);
  38. if ($token === false)
  39. {
  40. // terminating sequence started
  41. }
  42. elseif ($token instanceof HTMLPurifier_Token_Start)
  43. {
  44. $nesting ++;
  45. }
  46. elseif ($token instanceof HTMLPurifier_Token_End)
  47. {
  48. $nesting --;
  49. }
  50. // handle node collection
  51. if ($is_collecting)
  52. {
  53. if ($is_child)
  54. {
  55. // okay, let's stash the tokens away
  56. // first token tells us the type of the collection
  57. switch ($collection[$tag_index]->name)
  58. {
  59. case 'tr' :
  60. case 'tbody' :
  61. $content[] = $collection;
  62. break;
  63. case 'caption' :
  64. if ($caption !== false)
  65. break;
  66. $caption = $collection;
  67. break;
  68. case 'thead' :
  69. case 'tfoot' :
  70. // access the appropriate variable, $thead or $tfoot
  71. $var = $collection[$tag_index]->name;
  72. if ($$var === false)
  73. {
  74. $$var = $collection;
  75. }
  76. else
  77. {
  78. // transmutate the first and less entries into
  79. // tbody tags, and then put into content
  80. $collection[$tag_index]->name = 'tbody';
  81. $collection[count($collection) - 1]->name = 'tbody';
  82. $content[] = $collection;
  83. }
  84. break;
  85. case 'colgroup' :
  86. $cols[] = $collection;
  87. break;
  88. }
  89. $collection = array();
  90. $is_collecting = false;
  91. $tag_index = 0;
  92. }
  93. else
  94. {
  95. // add the node to the collection
  96. $collection[] = $token;
  97. }
  98. }
  99. // terminate
  100. if ($token === false)
  101. break;
  102. if ($is_child)
  103. {
  104. // determine what we're dealing with
  105. if ($token->name == 'col')
  106. {
  107. // the only empty tag in the possie, we can handle it
  108. // immediately
  109. $cols[] = array_merge($collection, array($token));
  110. $collection = array();
  111. $tag_index = 0;
  112. continue;
  113. }
  114. switch ($token->name)
  115. {
  116. case 'caption' :
  117. case 'colgroup' :
  118. case 'thead' :
  119. case 'tfoot' :
  120. case 'tbody' :
  121. case 'tr' :
  122. $is_collecting = true;
  123. $collection[] = $token;
  124. continue;
  125. default :
  126. if (! empty($token->is_whitespace))
  127. {
  128. $collection[] = $token;
  129. $tag_index ++;
  130. }
  131. continue;
  132. }
  133. }
  134. }
  135. if (empty($content))
  136. return false;
  137. $ret = array();
  138. if ($caption !== false)
  139. $ret = array_merge($ret, $caption);
  140. if ($cols !== false)
  141. foreach ($cols as $token_array)
  142. $ret = array_merge($ret, $token_array);
  143. if ($thead !== false)
  144. $ret = array_merge($ret, $thead);
  145. if ($tfoot !== false)
  146. $ret = array_merge($ret, $tfoot);
  147. foreach ($content as $token_array)
  148. $ret = array_merge($ret, $token_array);
  149. if (! empty($collection) && $is_collecting == false)
  150. {
  151. // grab the trailing space
  152. $ret = array_merge($ret, $collection);
  153. }
  154. array_pop($tokens_of_children); // remove phantom token
  155. return ($ret === $tokens_of_children) ? true : $ret;
  156. }
  157. }
  158. // vim: et sw=4 sts=4