CSSDefinition.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Defines allowed CSS attributes and what their values are.
  4. * @see HTMLPurifier_HTMLDefinition
  5. */
  6. class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
  7. {
  8. public $type = 'CSS';
  9. /**
  10. * Assoc array of attribute name to definition object.
  11. */
  12. public $info = array();
  13. /**
  14. * Constructs the info array. The meat of this class.
  15. */
  16. protected function doSetup($config) {
  17. $this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
  18. array('left', 'right', 'center', 'justify'), false);
  19. $border_style =
  20. $this->info['border-bottom-style'] =
  21. $this->info['border-right-style'] =
  22. $this->info['border-left-style'] =
  23. $this->info['border-top-style'] = new HTMLPurifier_AttrDef_Enum(
  24. array('none', 'hidden', 'dotted', 'dashed', 'solid', 'double',
  25. 'groove', 'ridge', 'inset', 'outset'), false);
  26. $this->info['border-style'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
  27. $this->info['clear'] = new HTMLPurifier_AttrDef_Enum(
  28. array('none', 'left', 'right', 'both'), false);
  29. $this->info['float'] = new HTMLPurifier_AttrDef_Enum(
  30. array('none', 'left', 'right'), false);
  31. $this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
  32. array('normal', 'italic', 'oblique'), false);
  33. $this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
  34. array('normal', 'small-caps'), false);
  35. $uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
  36. array(
  37. new HTMLPurifier_AttrDef_Enum(array('none')),
  38. new HTMLPurifier_AttrDef_CSS_URI()
  39. )
  40. );
  41. $this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
  42. array('inside', 'outside'), false);
  43. $this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
  44. array('disc', 'circle', 'square', 'decimal', 'lower-roman',
  45. 'upper-roman', 'lower-alpha', 'upper-alpha', 'none'), false);
  46. $this->info['list-style-image'] = $uri_or_none;
  47. $this->info['list-style'] = new HTMLPurifier_AttrDef_CSS_ListStyle($config);
  48. $this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
  49. array('capitalize', 'uppercase', 'lowercase', 'none'), false);
  50. $this->info['color'] = new HTMLPurifier_AttrDef_CSS_Color();
  51. $this->info['background-image'] = $uri_or_none;
  52. $this->info['background-repeat'] = new HTMLPurifier_AttrDef_Enum(
  53. array('repeat', 'repeat-x', 'repeat-y', 'no-repeat')
  54. );
  55. $this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
  56. array('scroll', 'fixed')
  57. );
  58. $this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
  59. $border_color =
  60. $this->info['border-top-color'] =
  61. $this->info['border-bottom-color'] =
  62. $this->info['border-left-color'] =
  63. $this->info['border-right-color'] =
  64. $this->info['background-color'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  65. new HTMLPurifier_AttrDef_Enum(array('transparent')),
  66. new HTMLPurifier_AttrDef_CSS_Color()
  67. ));
  68. $this->info['background'] = new HTMLPurifier_AttrDef_CSS_Background($config);
  69. $this->info['border-color'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
  70. $border_width =
  71. $this->info['border-top-width'] =
  72. $this->info['border-bottom-width'] =
  73. $this->info['border-left-width'] =
  74. $this->info['border-right-width'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  75. new HTMLPurifier_AttrDef_Enum(array('thin', 'medium', 'thick')),
  76. new HTMLPurifier_AttrDef_CSS_Length('0') //disallow negative
  77. ));
  78. $this->info['border-width'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
  79. $this->info['letter-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  80. new HTMLPurifier_AttrDef_Enum(array('normal')),
  81. new HTMLPurifier_AttrDef_CSS_Length()
  82. ));
  83. $this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  84. new HTMLPurifier_AttrDef_Enum(array('normal')),
  85. new HTMLPurifier_AttrDef_CSS_Length()
  86. ));
  87. $this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  88. new HTMLPurifier_AttrDef_Enum(array('xx-small', 'x-small',
  89. 'small', 'medium', 'large', 'x-large', 'xx-large',
  90. 'larger', 'smaller')),
  91. new HTMLPurifier_AttrDef_CSS_Percentage(),
  92. new HTMLPurifier_AttrDef_CSS_Length()
  93. ));
  94. $this->info['line-height'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  95. new HTMLPurifier_AttrDef_Enum(array('normal')),
  96. new HTMLPurifier_AttrDef_CSS_Number(true), // no negatives
  97. new HTMLPurifier_AttrDef_CSS_Length('0'),
  98. new HTMLPurifier_AttrDef_CSS_Percentage(true)
  99. ));
  100. $margin =
  101. $this->info['margin-top'] =
  102. $this->info['margin-bottom'] =
  103. $this->info['margin-left'] =
  104. $this->info['margin-right'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  105. new HTMLPurifier_AttrDef_CSS_Length(),
  106. new HTMLPurifier_AttrDef_CSS_Percentage(),
  107. new HTMLPurifier_AttrDef_Enum(array('auto'))
  108. ));
  109. $this->info['margin'] = new HTMLPurifier_AttrDef_CSS_Multiple($margin);
  110. // non-negative
  111. $padding =
  112. $this->info['padding-top'] =
  113. $this->info['padding-bottom'] =
  114. $this->info['padding-left'] =
  115. $this->info['padding-right'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  116. new HTMLPurifier_AttrDef_CSS_Length('0'),
  117. new HTMLPurifier_AttrDef_CSS_Percentage(true)
  118. ));
  119. $this->info['padding'] = new HTMLPurifier_AttrDef_CSS_Multiple($padding);
  120. $this->info['text-indent'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  121. new HTMLPurifier_AttrDef_CSS_Length(),
  122. new HTMLPurifier_AttrDef_CSS_Percentage()
  123. ));
  124. $trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(array(
  125. new HTMLPurifier_AttrDef_CSS_Length('0'),
  126. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  127. new HTMLPurifier_AttrDef_Enum(array('auto'))
  128. ));
  129. $max = $config->get('CSS.MaxImgLength');
  130. $this->info['width'] =
  131. $this->info['height'] =
  132. $max === null ?
  133. $trusted_wh :
  134. new HTMLPurifier_AttrDef_Switch('img',
  135. // For img tags:
  136. new HTMLPurifier_AttrDef_CSS_Composite(array(
  137. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  138. new HTMLPurifier_AttrDef_Enum(array('auto'))
  139. )),
  140. // For everyone else:
  141. $trusted_wh
  142. );
  143. $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
  144. $this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
  145. // this could use specialized code
  146. $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
  147. array('normal', 'bold', 'bolder', 'lighter', '100', '200', '300',
  148. '400', '500', '600', '700', '800', '900'), false);
  149. // MUST be called after other font properties, as it references
  150. // a CSSDefinition object
  151. $this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
  152. // same here
  153. $this->info['border'] =
  154. $this->info['border-bottom'] =
  155. $this->info['border-top'] =
  156. $this->info['border-left'] =
  157. $this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
  158. $this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(array(
  159. 'collapse', 'separate'));
  160. $this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(array(
  161. 'top', 'bottom'));
  162. $this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(array(
  163. 'auto', 'fixed'));
  164. $this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  165. new HTMLPurifier_AttrDef_Enum(array('baseline', 'sub', 'super',
  166. 'top', 'text-top', 'middle', 'bottom', 'text-bottom')),
  167. new HTMLPurifier_AttrDef_CSS_Length(),
  168. new HTMLPurifier_AttrDef_CSS_Percentage()
  169. ));
  170. $this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
  171. // partial support
  172. $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap'));
  173. if ($config->get('CSS.Proprietary')) {
  174. $this->doSetupProprietary($config);
  175. }
  176. if ($config->get('CSS.AllowTricky')) {
  177. $this->doSetupTricky($config);
  178. }
  179. $allow_important = $config->get('CSS.AllowImportant');
  180. // wrap all attr-defs with decorator that handles !important
  181. foreach ($this->info as $k => $v) {
  182. $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
  183. }
  184. $this->setupConfigStuff($config);
  185. }
  186. protected function doSetupProprietary($config) {
  187. // Internet Explorer only scrollbar colors
  188. $this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  189. $this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  190. $this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  191. $this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  192. $this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  193. $this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  194. // technically not proprietary, but CSS3, and no one supports it
  195. $this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  196. $this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  197. $this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  198. // only opacity, for now
  199. $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
  200. }
  201. protected function doSetupTricky($config) {
  202. $this->info['display'] = new HTMLPurifier_AttrDef_Enum(array(
  203. 'inline', 'block', 'list-item', 'run-in', 'compact',
  204. 'marker', 'table', 'inline-table', 'table-row-group',
  205. 'table-header-group', 'table-footer-group', 'table-row',
  206. 'table-column-group', 'table-column', 'table-cell', 'table-caption', 'none'
  207. ));
  208. $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(array(
  209. 'visible', 'hidden', 'collapse'
  210. ));
  211. $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(array('visible', 'hidden', 'auto', 'scroll'));
  212. }
  213. /**
  214. * Performs extra config-based processing. Based off of
  215. * HTMLPurifier_HTMLDefinition.
  216. * @todo Refactor duplicate elements into common class (probably using
  217. * composition, not inheritance).
  218. */
  219. protected function setupConfigStuff($config) {
  220. // setup allowed elements
  221. $support = "(for information on implementing this, see the ".
  222. "support forums) ";
  223. $allowed_properties = $config->get('CSS.AllowedProperties');
  224. if ($allowed_properties !== null) {
  225. foreach ($this->info as $name => $d) {
  226. if(!isset($allowed_properties[$name])) unset($this->info[$name]);
  227. unset($allowed_properties[$name]);
  228. }
  229. // emit errors
  230. foreach ($allowed_properties as $name => $d) {
  231. // :TODO: Is this htmlspecialchars() call really necessary?
  232. $name = htmlspecialchars($name);
  233. trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
  234. }
  235. }
  236. $forbidden_properties = $config->get('CSS.ForbiddenProperties');
  237. if ($forbidden_properties !== null) {
  238. foreach ($this->info as $name => $d) {
  239. if (isset($forbidden_properties[$name])) {
  240. unset($this->info[$name]);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. // vim: et sw=4 sts=4