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

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/ChildDef/Required.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 117 lines | 77 code | 14 blank | 26 comment | 22 complexity | 682f8da336f2a1dad1cd7880d4848076 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Definition that allows a set of elements, but disallows empty children.
  4. */
  5. class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
  6. {
  7. /**
  8. * Lookup table of allowed elements.
  9. * @public
  10. */
  11. public $elements = array();
  12. /**
  13. * Whether or not the last passed node was all whitespace.
  14. */
  15. protected $whitespace = false;
  16. /**
  17. * @param $elements List of allowed element names (lowercase).
  18. */
  19. public function __construct($elements) {
  20. if (is_string($elements)) {
  21. $elements = str_replace(' ', '', $elements);
  22. $elements = explode('|', $elements);
  23. }
  24. $keys = array_keys($elements);
  25. if ($keys == array_keys($keys)) {
  26. $elements = array_flip($elements);
  27. foreach ($elements as $i => $x) {
  28. $elements[$i] = true;
  29. if (empty($i)) unset($elements[$i]); // remove blank
  30. }
  31. }
  32. $this->elements = $elements;
  33. }
  34. public $allow_empty = false;
  35. public $type = 'required';
  36. public function validateChildren($tokens_of_children, $config, $context) {
  37. // Flag for subclasses
  38. $this->whitespace = false;
  39. // if there are no tokens, delete parent node
  40. if (empty($tokens_of_children)) return false;
  41. // the new set of children
  42. $result = array();
  43. // current depth into the nest
  44. $nesting = 0;
  45. // whether or not we're deleting a node
  46. $is_deleting = false;
  47. // whether or not parsed character data is allowed
  48. // this controls whether or not we silently drop a tag
  49. // or generate escaped HTML from it
  50. $pcdata_allowed = isset($this->elements['#PCDATA']);
  51. // a little sanity check to make sure it's not ALL whitespace
  52. $all_whitespace = true;
  53. // some configuration
  54. $escape_invalid_children = $config->get('Core.EscapeInvalidChildren');
  55. // generator
  56. $gen = new HTMLPurifier_Generator($config, $context);
  57. foreach ($tokens_of_children as $token) {
  58. if (!empty($token->is_whitespace)) {
  59. $result[] = $token;
  60. continue;
  61. }
  62. $all_whitespace = false; // phew, we're not talking about whitespace
  63. $is_child = ($nesting == 0);
  64. if ($token instanceof HTMLPurifier_Token_Start) {
  65. $nesting++;
  66. } elseif ($token instanceof HTMLPurifier_Token_End) {
  67. $nesting--;
  68. }
  69. if ($is_child) {
  70. $is_deleting = false;
  71. if (!isset($this->elements[$token->name])) {
  72. $is_deleting = true;
  73. if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
  74. $result[] = $token;
  75. } elseif ($pcdata_allowed && $escape_invalid_children) {
  76. $result[] = new HTMLPurifier_Token_Text(
  77. $gen->generateFromToken($token)
  78. );
  79. }
  80. continue;
  81. }
  82. }
  83. if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
  84. $result[] = $token;
  85. } elseif ($pcdata_allowed && $escape_invalid_children) {
  86. $result[] =
  87. new HTMLPurifier_Token_Text(
  88. $gen->generateFromToken($token)
  89. );
  90. } else {
  91. // drop silently
  92. }
  93. }
  94. if (empty($result)) return false;
  95. if ($all_whitespace) {
  96. $this->whitespace = true;
  97. return false;
  98. }
  99. if ($tokens_of_children == $result) return true;
  100. return $result;
  101. }
  102. }
  103. // vim: et sw=4 sts=4