StrictBlockquote.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Takes the contents of blockquote when in strict and reformats for validation.
  4. */
  5. class HTMLPurifier_ChildDef_StrictBlockquote extends HTMLPurifier_ChildDef_Required
  6. {
  7. /**
  8. * @type array
  9. */
  10. protected $real_elements;
  11. /**
  12. * @type array
  13. */
  14. protected $fake_elements;
  15. /**
  16. * @type bool
  17. */
  18. public $allow_empty = true;
  19. /**
  20. * @type string
  21. */
  22. public $type = 'strictblockquote';
  23. /**
  24. * @type bool
  25. */
  26. protected $init = false;
  27. /**
  28. * @param HTMLPurifier_Config $config
  29. * @return array
  30. * @note We don't want MakeWellFormed to auto-close inline elements since
  31. * they might be allowed.
  32. */
  33. public function getAllowedElements($config)
  34. {
  35. $this->init($config);
  36. return $this->fake_elements;
  37. }
  38. /**
  39. * @param array $children
  40. * @param HTMLPurifier_Config $config
  41. * @param HTMLPurifier_Context $context
  42. * @return array
  43. */
  44. public function validateChildren($children, $config, $context)
  45. {
  46. $this->init($config);
  47. // trick the parent class into thinking it allows more
  48. $this->elements = $this->fake_elements;
  49. $result = parent::validateChildren($children, $config, $context);
  50. $this->elements = $this->real_elements;
  51. if ($result === false) {
  52. return array();
  53. }
  54. if ($result === true) {
  55. $result = $children;
  56. }
  57. $def = $config->getHTMLDefinition();
  58. $block_wrap_name = $def->info_block_wrapper;
  59. $block_wrap = false;
  60. $ret = array();
  61. foreach ($result as $node) {
  62. if ($block_wrap === false) {
  63. if (($node instanceof HTMLPurifier_Node_Text && !$node->is_whitespace) ||
  64. ($node instanceof HTMLPurifier_Node_Element && !isset($this->elements[$node->name]))) {
  65. $block_wrap = new HTMLPurifier_Node_Element($def->info_block_wrapper);
  66. $ret[] = $block_wrap;
  67. }
  68. } else {
  69. if ($node instanceof HTMLPurifier_Node_Element && isset($this->elements[$node->name])) {
  70. $block_wrap = false;
  71. }
  72. }
  73. if ($block_wrap) {
  74. $block_wrap->children[] = $node;
  75. } else {
  76. $ret[] = $node;
  77. }
  78. }
  79. return $ret;
  80. }
  81. /**
  82. * @param HTMLPurifier_Config $config
  83. */
  84. private function init($config)
  85. {
  86. if (!$this->init) {
  87. $def = $config->getHTMLDefinition();
  88. // allow all inline elements
  89. $this->real_elements = $this->elements;
  90. $this->fake_elements = $def->info_content_sets['Flow'];
  91. $this->fake_elements['#PCDATA'] = true;
  92. $this->init = true;
  93. }
  94. }
  95. }
  96. // vim: et sw=4 sts=4