ElementDef.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Structure that stores an HTML element definition. Used by
  4. * HTMLPurifier_HTMLDefinition and HTMLPurifier_HTMLModule.
  5. * @note This class is inspected by HTMLPurifier_Printer_HTMLDefinition.
  6. * Please update that class too.
  7. * @warning If you add new properties to this class, you MUST update
  8. * the mergeIn() method.
  9. */
  10. class HTMLPurifier_ElementDef
  11. {
  12. /**
  13. * Does the definition work by itself, or is it created solely
  14. * for the purpose of merging into another definition?
  15. * @type bool
  16. */
  17. public $standalone = true;
  18. /**
  19. * Associative array of attribute name to HTMLPurifier_AttrDef.
  20. * @type array
  21. * @note Before being processed by HTMLPurifier_AttrCollections
  22. * when modules are finalized during
  23. * HTMLPurifier_HTMLDefinition->setup(), this array may also
  24. * contain an array at index 0 that indicates which attribute
  25. * collections to load into the full array. It may also
  26. * contain string indentifiers in lieu of HTMLPurifier_AttrDef,
  27. * see HTMLPurifier_AttrTypes on how they are expanded during
  28. * HTMLPurifier_HTMLDefinition->setup() processing.
  29. */
  30. public $attr = array();
  31. // XXX: Design note: currently, it's not possible to override
  32. // previously defined AttrTransforms without messing around with
  33. // the final generated config. This is by design; a previous version
  34. // used an associated list of attr_transform, but it was extremely
  35. // easy to accidentally override other attribute transforms by
  36. // forgetting to specify an index (and just using 0.) While we
  37. // could check this by checking the index number and complaining,
  38. // there is a second problem which is that it is not at all easy to
  39. // tell when something is getting overridden. Combine this with a
  40. // codebase where this isn't really being used, and it's perfect for
  41. // nuking.
  42. /**
  43. * List of tags HTMLPurifier_AttrTransform to be done before validation.
  44. * @type array
  45. */
  46. public $attr_transform_pre = array();
  47. /**
  48. * List of tags HTMLPurifier_AttrTransform to be done after validation.
  49. * @type array
  50. */
  51. public $attr_transform_post = array();
  52. /**
  53. * HTMLPurifier_ChildDef of this tag.
  54. * @type HTMLPurifier_ChildDef
  55. */
  56. public $child;
  57. /**
  58. * Abstract string representation of internal ChildDef rules.
  59. * @see HTMLPurifier_ContentSets for how this is parsed and then transformed
  60. * into an HTMLPurifier_ChildDef.
  61. * @warning This is a temporary variable that is not available after
  62. * being processed by HTMLDefinition
  63. * @type string
  64. */
  65. public $content_model;
  66. /**
  67. * Value of $child->type, used to determine which ChildDef to use,
  68. * used in combination with $content_model.
  69. * @warning This must be lowercase
  70. * @warning This is a temporary variable that is not available after
  71. * being processed by HTMLDefinition
  72. * @type string
  73. */
  74. public $content_model_type;
  75. /**
  76. * Does the element have a content model (#PCDATA | Inline)*? This
  77. * is important for chameleon ins and del processing in
  78. * HTMLPurifier_ChildDef_Chameleon. Dynamically set: modules don't
  79. * have to worry about this one.
  80. * @type bool
  81. */
  82. public $descendants_are_inline = false;
  83. /**
  84. * List of the names of required attributes this element has.
  85. * Dynamically populated by HTMLPurifier_HTMLDefinition::getElement()
  86. * @type array
  87. */
  88. public $required_attr = array();
  89. /**
  90. * Lookup table of tags excluded from all descendants of this tag.
  91. * @type array
  92. * @note SGML permits exclusions for all descendants, but this is
  93. * not possible with DTDs or XML Schemas. W3C has elected to
  94. * use complicated compositions of content_models to simulate
  95. * exclusion for children, but we go the simpler, SGML-style
  96. * route of flat-out exclusions, which correctly apply to
  97. * all descendants and not just children. Note that the XHTML
  98. * Modularization Abstract Modules are blithely unaware of such
  99. * distinctions.
  100. */
  101. public $excludes = array();
  102. /**
  103. * This tag is explicitly auto-closed by the following tags.
  104. * @type array
  105. */
  106. public $autoclose = array();
  107. /**
  108. * If a foreign element is found in this element, test if it is
  109. * allowed by this sub-element; if it is, instead of closing the
  110. * current element, place it inside this element.
  111. * @type string
  112. */
  113. public $wrap;
  114. /**
  115. * Whether or not this is a formatting element affected by the
  116. * "Active Formatting Elements" algorithm.
  117. * @type bool
  118. */
  119. public $formatting;
  120. /**
  121. * Low-level factory constructor for creating new standalone element defs
  122. */
  123. public static function create($content_model, $content_model_type, $attr)
  124. {
  125. $def = new HTMLPurifier_ElementDef();
  126. $def->content_model = $content_model;
  127. $def->content_model_type = $content_model_type;
  128. $def->attr = $attr;
  129. return $def;
  130. }
  131. /**
  132. * Merges the values of another element definition into this one.
  133. * Values from the new element def take precedence if a value is
  134. * not mergeable.
  135. * @param HTMLPurifier_ElementDef $def
  136. */
  137. public function mergeIn($def)
  138. {
  139. // later keys takes precedence
  140. foreach ($def->attr as $k => $v) {
  141. if ($k === 0) {
  142. // merge in the includes
  143. // sorry, no way to override an include
  144. foreach ($v as $v2) {
  145. $this->attr[0][] = $v2;
  146. }
  147. continue;
  148. }
  149. if ($v === false) {
  150. if (isset($this->attr[$k])) {
  151. unset($this->attr[$k]);
  152. }
  153. continue;
  154. }
  155. $this->attr[$k] = $v;
  156. }
  157. $this->_mergeAssocArray($this->excludes, $def->excludes);
  158. $this->attr_transform_pre = array_merge($this->attr_transform_pre, $def->attr_transform_pre);
  159. $this->attr_transform_post = array_merge($this->attr_transform_post, $def->attr_transform_post);
  160. if (!empty($def->content_model)) {
  161. $this->content_model =
  162. str_replace("#SUPER", $this->content_model, $def->content_model);
  163. $this->child = false;
  164. }
  165. if (!empty($def->content_model_type)) {
  166. $this->content_model_type = $def->content_model_type;
  167. $this->child = false;
  168. }
  169. if (!is_null($def->child)) {
  170. $this->child = $def->child;
  171. }
  172. if (!is_null($def->formatting)) {
  173. $this->formatting = $def->formatting;
  174. }
  175. if ($def->descendants_are_inline) {
  176. $this->descendants_are_inline = $def->descendants_are_inline;
  177. }
  178. }
  179. /**
  180. * Merges one array into another, removes values which equal false
  181. * @param $a1 Array by reference that is merged into
  182. * @param $a2 Array that merges into $a1
  183. */
  184. private function _mergeAssocArray(&$a1, $a2)
  185. {
  186. foreach ($a2 as $k => $v) {
  187. if ($v === false) {
  188. if (isset($a1[$k])) {
  189. unset($a1[$k]);
  190. }
  191. continue;
  192. }
  193. $a1[$k] = $v;
  194. }
  195. }
  196. }
  197. // vim: et sw=4 sts=4