ContentSets.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @todo Unit test
  4. */
  5. class HTMLPurifier_ContentSets
  6. {
  7. /**
  8. * List of content set strings (pipe seperators) indexed by name.
  9. */
  10. public $info = array();
  11. /**
  12. * List of content set lookups (element => true) indexed by name.
  13. * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
  14. */
  15. public $lookup = array();
  16. /**
  17. * Synchronized list of defined content sets (keys of info)
  18. */
  19. protected $keys = array();
  20. /**
  21. * Synchronized list of defined content values (values of info)
  22. */
  23. protected $values = array();
  24. /**
  25. * Merges in module's content sets, expands identifiers in the content
  26. * sets and populates the keys, values and lookup member variables.
  27. * @param $modules List of HTMLPurifier_HTMLModule
  28. */
  29. public function __construct($modules) {
  30. if (!is_array($modules)) $modules = array($modules);
  31. // populate content_sets based on module hints
  32. // sorry, no way of overloading
  33. foreach ($modules as $module_i => $module) {
  34. foreach ($module->content_sets as $key => $value) {
  35. $temp = $this->convertToLookup($value);
  36. if (isset($this->lookup[$key])) {
  37. // add it into the existing content set
  38. $this->lookup[$key] = array_merge($this->lookup[$key], $temp);
  39. } else {
  40. $this->lookup[$key] = $temp;
  41. }
  42. }
  43. }
  44. $old_lookup = false;
  45. while ($old_lookup !== $this->lookup) {
  46. $old_lookup = $this->lookup;
  47. foreach ($this->lookup as $i => $set) {
  48. $add = array();
  49. foreach ($set as $element => $x) {
  50. if (isset($this->lookup[$element])) {
  51. $add += $this->lookup[$element];
  52. unset($this->lookup[$i][$element]);
  53. }
  54. }
  55. $this->lookup[$i] += $add;
  56. }
  57. }
  58. foreach ($this->lookup as $key => $lookup) {
  59. $this->info[$key] = implode(' | ', array_keys($lookup));
  60. }
  61. $this->keys = array_keys($this->info);
  62. $this->values = array_values($this->info);
  63. }
  64. /**
  65. * Accepts a definition; generates and assigns a ChildDef for it
  66. * @param $def HTMLPurifier_ElementDef reference
  67. * @param $module Module that defined the ElementDef
  68. */
  69. public function generateChildDef(&$def, $module) {
  70. if (!empty($def->child)) return; // already done!
  71. $content_model = $def->content_model;
  72. if (is_string($content_model)) {
  73. // Assume that $this->keys is alphanumeric
  74. $def->content_model = preg_replace_callback(
  75. '/\b(' . implode('|', $this->keys) . ')\b/',
  76. array($this, 'generateChildDefCallback'),
  77. $content_model
  78. );
  79. //$def->content_model = str_replace(
  80. // $this->keys, $this->values, $content_model);
  81. }
  82. $def->child = $this->getChildDef($def, $module);
  83. }
  84. public function generateChildDefCallback($matches) {
  85. return $this->info[$matches[0]];
  86. }
  87. /**
  88. * Instantiates a ChildDef based on content_model and content_model_type
  89. * member variables in HTMLPurifier_ElementDef
  90. * @note This will also defer to modules for custom HTMLPurifier_ChildDef
  91. * subclasses that need content set expansion
  92. * @param $def HTMLPurifier_ElementDef to have ChildDef extracted
  93. * @return HTMLPurifier_ChildDef corresponding to ElementDef
  94. */
  95. public function getChildDef($def, $module) {
  96. $value = $def->content_model;
  97. if (is_object($value)) {
  98. trigger_error(
  99. 'Literal object child definitions should be stored in '.
  100. 'ElementDef->child not ElementDef->content_model',
  101. E_USER_NOTICE
  102. );
  103. return $value;
  104. }
  105. switch ($def->content_model_type) {
  106. case 'required':
  107. return new HTMLPurifier_ChildDef_Required($value);
  108. case 'optional':
  109. return new HTMLPurifier_ChildDef_Optional($value);
  110. case 'empty':
  111. return new HTMLPurifier_ChildDef_Empty();
  112. case 'custom':
  113. return new HTMLPurifier_ChildDef_Custom($value);
  114. }
  115. // defer to its module
  116. $return = false;
  117. if ($module->defines_child_def) { // save a func call
  118. $return = $module->getChildDef($def);
  119. }
  120. if ($return !== false) return $return;
  121. // error-out
  122. trigger_error(
  123. 'Could not determine which ChildDef class to instantiate',
  124. E_USER_ERROR
  125. );
  126. return false;
  127. }
  128. /**
  129. * Converts a string list of elements separated by pipes into
  130. * a lookup array.
  131. * @param $string List of elements
  132. * @return Lookup array of elements
  133. */
  134. protected function convertToLookup($string) {
  135. $array = explode('|', str_replace(' ', '', $string));
  136. $ret = array();
  137. foreach ($array as $i => $k) {
  138. $ret[$k] = true;
  139. }
  140. return $ret;
  141. }
  142. }
  143. // vim: et sw=4 sts=4