PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/lib/htmlpurifier/HTMLPurifier/HTMLModule.php

https://bitbucket.org/geek745/moodle-db2
PHP | 232 lines | 72 code | 23 blank | 137 comment | 14 complexity | c85e9cfbe88761e534635741b1feb7b3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. /**
  3. * Represents an XHTML 1.1 module, with information on elements, tags
  4. * and attributes.
  5. * @note Even though this is technically XHTML 1.1, it is also used for
  6. * regular HTML parsing. We are using modulization as a convenient
  7. * way to represent the internals of HTMLDefinition, and our
  8. * implementation is by no means conforming and does not directly
  9. * use the normative DTDs or XML schemas.
  10. * @note The public variables in a module should almost directly
  11. * correspond to the variables in HTMLPurifier_HTMLDefinition.
  12. * However, the prefix info carries no special meaning in these
  13. * objects (include it anyway if that's the correspondence though).
  14. */
  15. class HTMLPurifier_HTMLModule
  16. {
  17. // -- Overloadable ----------------------------------------------------
  18. /**
  19. * Short unique string identifier of the module
  20. */
  21. var $name;
  22. /**
  23. * Informally, a list of elements this module changes. Not used in
  24. * any significant way.
  25. * @protected
  26. */
  27. var $elements = array();
  28. /**
  29. * Associative array of element names to element definitions.
  30. * Some definitions may be incomplete, to be merged in later
  31. * with the full definition.
  32. * @public
  33. */
  34. var $info = array();
  35. /**
  36. * Associative array of content set names to content set additions.
  37. * This is commonly used to, say, add an A element to the Inline
  38. * content set. This corresponds to an internal variable $content_sets
  39. * and NOT info_content_sets member variable of HTMLDefinition.
  40. * @public
  41. */
  42. var $content_sets = array();
  43. /**
  44. * Associative array of attribute collection names to attribute
  45. * collection additions. More rarely used for adding attributes to
  46. * the global collections. Example is the StyleAttribute module adding
  47. * the style attribute to the Core. Corresponds to HTMLDefinition's
  48. * attr_collections->info, since the object's data is only info,
  49. * with extra behavior associated with it.
  50. * @public
  51. */
  52. var $attr_collections = array();
  53. /**
  54. * Associative array of deprecated tag name to HTMLPurifier_TagTransform
  55. * @public
  56. */
  57. var $info_tag_transform = array();
  58. /**
  59. * List of HTMLPurifier_AttrTransform to be performed before validation.
  60. * @public
  61. */
  62. var $info_attr_transform_pre = array();
  63. /**
  64. * List of HTMLPurifier_AttrTransform to be performed after validation.
  65. * @public
  66. */
  67. var $info_attr_transform_post = array();
  68. /**
  69. * Boolean flag that indicates whether or not getChildDef is implemented.
  70. * For optimization reasons: may save a call to a function. Be sure
  71. * to set it if you do implement getChildDef(), otherwise it will have
  72. * no effect!
  73. * @public
  74. */
  75. var $defines_child_def = false;
  76. /**
  77. * Retrieves a proper HTMLPurifier_ChildDef subclass based on
  78. * content_model and content_model_type member variables of
  79. * the HTMLPurifier_ElementDef class. There is a similar function
  80. * in HTMLPurifier_HTMLDefinition.
  81. * @param $def HTMLPurifier_ElementDef instance
  82. * @return HTMLPurifier_ChildDef subclass
  83. * @public
  84. */
  85. function getChildDef($def) {return false;}
  86. // -- Convenience -----------------------------------------------------
  87. /**
  88. * Convenience function that sets up a new element
  89. * @param $element Name of element to add
  90. * @param $safe Is element safe for untrusted users to use?
  91. * @param $type What content set should element be registered to?
  92. * Set as false to skip this step.
  93. * @param $contents Allowed children in form of:
  94. * "$content_model_type: $content_model"
  95. * @param $attr_includes What attribute collections to register to
  96. * element?
  97. * @param $attr What unique attributes does the element define?
  98. * @note See ElementDef for in-depth descriptions of these parameters.
  99. * @return Reference to created element definition object, so you
  100. * can set advanced parameters
  101. * @protected
  102. */
  103. function &addElement($element, $safe, $type, $contents, $attr_includes = array(), $attr = array()) {
  104. $this->elements[] = $element;
  105. // parse content_model
  106. list($content_model_type, $content_model) = $this->parseContents($contents);
  107. // merge in attribute inclusions
  108. $this->mergeInAttrIncludes($attr, $attr_includes);
  109. // add element to content sets
  110. if ($type) $this->addElementToContentSet($element, $type);
  111. // create element
  112. $this->info[$element] = HTMLPurifier_ElementDef::create(
  113. $safe, $content_model, $content_model_type, $attr
  114. );
  115. // literal object $contents means direct child manipulation
  116. if (!is_string($contents)) $this->info[$element]->child = $contents;
  117. return $this->info[$element];
  118. }
  119. /**
  120. * Convenience function that creates a totally blank, non-standalone
  121. * element.
  122. * @param $element Name of element to create
  123. * @return Reference to created element
  124. */
  125. function &addBlankElement($element) {
  126. if (!isset($this->info[$element])) {
  127. $this->elements[] = $element;
  128. $this->info[$element] = new HTMLPurifier_ElementDef();
  129. $this->info[$element]->standalone = false;
  130. } else {
  131. trigger_error("Definition for $element already exists in module, cannot redefine");
  132. }
  133. return $this->info[$element];
  134. }
  135. /**
  136. * Convenience function that registers an element to a content set
  137. * @param Element to register
  138. * @param Name content set (warning: case sensitive, usually upper-case
  139. * first letter)
  140. * @protected
  141. */
  142. function addElementToContentSet($element, $type) {
  143. if (!isset($this->content_sets[$type])) $this->content_sets[$type] = '';
  144. else $this->content_sets[$type] .= ' | ';
  145. $this->content_sets[$type] .= $element;
  146. }
  147. /**
  148. * Convenience function that transforms single-string contents
  149. * into separate content model and content model type
  150. * @param $contents Allowed children in form of:
  151. * "$content_model_type: $content_model"
  152. * @note If contents is an object, an array of two nulls will be
  153. * returned, and the callee needs to take the original $contents
  154. * and use it directly.
  155. */
  156. function parseContents($contents) {
  157. if (!is_string($contents)) return array(null, null); // defer
  158. switch ($contents) {
  159. // check for shorthand content model forms
  160. case 'Empty':
  161. return array('empty', '');
  162. case 'Inline':
  163. return array('optional', 'Inline | #PCDATA');
  164. case 'Flow':
  165. return array('optional', 'Flow | #PCDATA');
  166. }
  167. list($content_model_type, $content_model) = explode(':', $contents);
  168. $content_model_type = strtolower(trim($content_model_type));
  169. $content_model = trim($content_model);
  170. return array($content_model_type, $content_model);
  171. }
  172. /**
  173. * Convenience function that merges a list of attribute includes into
  174. * an attribute array.
  175. * @param $attr Reference to attr array to modify
  176. * @param $attr_includes Array of includes / string include to merge in
  177. */
  178. function mergeInAttrIncludes(&$attr, $attr_includes) {
  179. if (!is_array($attr_includes)) {
  180. if (empty($attr_includes)) $attr_includes = array();
  181. else $attr_includes = array($attr_includes);
  182. }
  183. $attr[0] = $attr_includes;
  184. }
  185. /**
  186. * Convenience function that generates a lookup table with boolean
  187. * true as value.
  188. * @param $list List of values to turn into a lookup
  189. * @note You can also pass an arbitrary number of arguments in
  190. * place of the regular argument
  191. * @return Lookup array equivalent of list
  192. */
  193. function makeLookup($list) {
  194. if (is_string($list)) $list = func_get_args();
  195. $ret = array();
  196. foreach ($list as $value) {
  197. if (is_null($value)) continue;
  198. $ret[$value] = true;
  199. }
  200. return $ret;
  201. }
  202. /**
  203. * Lazy load construction of the module after determining whether
  204. * or not it's needed, and also when a finalized configuration object
  205. * is available.
  206. * @param $config Instance of HTMLPurifier_Config
  207. */
  208. function setup($config) {}
  209. }