PageRenderTime 72ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/htmlpurifier/HTMLPurifier/ElementDef.php

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