CSSDefinition.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // These CSS properties don't work on many browsers, but we live
  172. // in THE FUTURE!
  173. $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap', 'normal', 'pre', 'pre-wrap', 'pre-line'));
  174. if ($config->get('CSS.Proprietary')) {
  175. $this->doSetupProprietary($config);
  176. }
  177. if ($config->get('CSS.AllowTricky')) {
  178. $this->doSetupTricky($config);
  179. }
  180. if ($config->get('CSS.Trusted')) {
  181. $this->doSetupTrusted($config);
  182. }
  183. $allow_important = $config->get('CSS.AllowImportant');
  184. // wrap all attr-defs with decorator that handles !important
  185. foreach ($this->info as $k => $v) {
  186. $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
  187. }
  188. $this->setupConfigStuff($config);
  189. }
  190. protected function doSetupProprietary($config) {
  191. // Internet Explorer only scrollbar colors
  192. $this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  193. $this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  194. $this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  195. $this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  196. $this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  197. $this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  198. // technically not proprietary, but CSS3, and no one supports it
  199. $this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  200. $this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  201. $this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  202. // only opacity, for now
  203. $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
  204. // more CSS3
  205. $this->info['page-break-after'] =
  206. $this->info['page-break-before'] = new HTMLPurifier_AttrDef_Enum(array('auto','always','avoid','left','right'));
  207. $this->info['page-break-inside'] = new HTMLPurifier_AttrDef_Enum(array('auto','avoid'));
  208. }
  209. protected function doSetupTricky($config) {
  210. $this->info['display'] = new HTMLPurifier_AttrDef_Enum(array(
  211. 'inline', 'block', 'list-item', 'run-in', 'compact',
  212. 'marker', 'table', 'inline-block', 'inline-table', 'table-row-group',
  213. 'table-header-group', 'table-footer-group', 'table-row',
  214. 'table-column-group', 'table-column', 'table-cell', 'table-caption', 'none'
  215. ));
  216. $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(array(
  217. 'visible', 'hidden', 'collapse'
  218. ));
  219. $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(array('visible', 'hidden', 'auto', 'scroll'));
  220. }
  221. protected function doSetupTrusted($config) {
  222. $this->info['position'] = new HTMLPurifier_AttrDef_Enum(array(
  223. 'static', 'relative', 'absolute', 'fixed'
  224. ));
  225. $this->info['top'] =
  226. $this->info['left'] =
  227. $this->info['right'] =
  228. $this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  229. new HTMLPurifier_AttrDef_CSS_Length(),
  230. new HTMLPurifier_AttrDef_CSS_Percentage(),
  231. new HTMLPurifier_AttrDef_Enum(array('auto')),
  232. ));
  233. $this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
  234. new HTMLPurifier_AttrDef_Integer(),
  235. new HTMLPurifier_AttrDef_Enum(array('auto')),
  236. ));
  237. }
  238. /**
  239. * Performs extra config-based processing. Based off of
  240. * HTMLPurifier_HTMLDefinition.
  241. * @todo Refactor duplicate elements into common class (probably using
  242. * composition, not inheritance).
  243. */
  244. protected function setupConfigStuff($config) {
  245. // setup allowed elements
  246. $support = "(for information on implementing this, see the ".
  247. "support forums) ";
  248. $allowed_properties = $config->get('CSS.AllowedProperties');
  249. if ($allowed_properties !== null) {
  250. foreach ($this->info as $name => $d) {
  251. if(!isset($allowed_properties[$name])) unset($this->info[$name]);
  252. unset($allowed_properties[$name]);
  253. }
  254. // emit errors
  255. foreach ($allowed_properties as $name => $d) {
  256. // :TODO: Is this htmlspecialchars() call really necessary?
  257. $name = htmlspecialchars($name);
  258. trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
  259. }
  260. }
  261. $forbidden_properties = $config->get('CSS.ForbiddenProperties');
  262. if ($forbidden_properties !== null) {
  263. foreach ($this->info as $name => $d) {
  264. if (isset($forbidden_properties[$name])) {
  265. unset($this->info[$name]);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. // vim: et sw=4 sts=4