CSSDefinition.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. * @type HTMLPurifier_AttrDef[]
  12. */
  13. public $info = array();
  14. /**
  15. * Constructs the info array. The meat of this class.
  16. * @param HTMLPurifier_Config $config
  17. */
  18. protected function doSetup($config)
  19. {
  20. $this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
  21. array('left', 'right', 'center', 'justify'),
  22. false
  23. );
  24. $border_style =
  25. $this->info['border-bottom-style'] =
  26. $this->info['border-right-style'] =
  27. $this->info['border-left-style'] =
  28. $this->info['border-top-style'] = new HTMLPurifier_AttrDef_Enum(
  29. array(
  30. 'none',
  31. 'hidden',
  32. 'dotted',
  33. 'dashed',
  34. 'solid',
  35. 'double',
  36. 'groove',
  37. 'ridge',
  38. 'inset',
  39. 'outset'
  40. ),
  41. false
  42. );
  43. $this->info['border-style'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
  44. $this->info['clear'] = new HTMLPurifier_AttrDef_Enum(
  45. array('none', 'left', 'right', 'both'),
  46. false
  47. );
  48. $this->info['float'] = new HTMLPurifier_AttrDef_Enum(
  49. array('none', 'left', 'right'),
  50. false
  51. );
  52. $this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
  53. array('normal', 'italic', 'oblique'),
  54. false
  55. );
  56. $this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
  57. array('normal', 'small-caps'),
  58. false
  59. );
  60. $uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
  61. array(
  62. new HTMLPurifier_AttrDef_Enum(array('none')),
  63. new HTMLPurifier_AttrDef_CSS_URI()
  64. )
  65. );
  66. $this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
  67. array('inside', 'outside'),
  68. false
  69. );
  70. $this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
  71. array(
  72. 'disc',
  73. 'circle',
  74. 'square',
  75. 'decimal',
  76. 'lower-roman',
  77. 'upper-roman',
  78. 'lower-alpha',
  79. 'upper-alpha',
  80. 'none'
  81. ),
  82. false
  83. );
  84. $this->info['list-style-image'] = $uri_or_none;
  85. $this->info['list-style'] = new HTMLPurifier_AttrDef_CSS_ListStyle($config);
  86. $this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
  87. array('capitalize', 'uppercase', 'lowercase', 'none'),
  88. false
  89. );
  90. $this->info['color'] = new HTMLPurifier_AttrDef_CSS_Color();
  91. $this->info['background-image'] = $uri_or_none;
  92. $this->info['background-repeat'] = new HTMLPurifier_AttrDef_Enum(
  93. array('repeat', 'repeat-x', 'repeat-y', 'no-repeat')
  94. );
  95. $this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
  96. array('scroll', 'fixed')
  97. );
  98. $this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
  99. $this->info['background-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
  100. array(
  101. new HTMLPurifier_AttrDef_Enum(
  102. array(
  103. 'auto',
  104. 'cover',
  105. 'contain',
  106. 'initial',
  107. 'inherit',
  108. )
  109. ),
  110. new HTMLPurifier_AttrDef_CSS_Percentage(),
  111. new HTMLPurifier_AttrDef_CSS_Length()
  112. )
  113. );
  114. $border_color =
  115. $this->info['border-top-color'] =
  116. $this->info['border-bottom-color'] =
  117. $this->info['border-left-color'] =
  118. $this->info['border-right-color'] =
  119. $this->info['background-color'] = new HTMLPurifier_AttrDef_CSS_Composite(
  120. array(
  121. new HTMLPurifier_AttrDef_Enum(array('transparent')),
  122. new HTMLPurifier_AttrDef_CSS_Color()
  123. )
  124. );
  125. $this->info['background'] = new HTMLPurifier_AttrDef_CSS_Background($config);
  126. $this->info['border-color'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
  127. $border_width =
  128. $this->info['border-top-width'] =
  129. $this->info['border-bottom-width'] =
  130. $this->info['border-left-width'] =
  131. $this->info['border-right-width'] = new HTMLPurifier_AttrDef_CSS_Composite(
  132. array(
  133. new HTMLPurifier_AttrDef_Enum(array('thin', 'medium', 'thick')),
  134. new HTMLPurifier_AttrDef_CSS_Length('0') //disallow negative
  135. )
  136. );
  137. $this->info['border-width'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
  138. $this->info['letter-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
  139. array(
  140. new HTMLPurifier_AttrDef_Enum(array('normal')),
  141. new HTMLPurifier_AttrDef_CSS_Length()
  142. )
  143. );
  144. $this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
  145. array(
  146. new HTMLPurifier_AttrDef_Enum(array('normal')),
  147. new HTMLPurifier_AttrDef_CSS_Length()
  148. )
  149. );
  150. $this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
  151. array(
  152. new HTMLPurifier_AttrDef_Enum(
  153. array(
  154. 'xx-small',
  155. 'x-small',
  156. 'small',
  157. 'medium',
  158. 'large',
  159. 'x-large',
  160. 'xx-large',
  161. 'larger',
  162. 'smaller'
  163. )
  164. ),
  165. new HTMLPurifier_AttrDef_CSS_Percentage(),
  166. new HTMLPurifier_AttrDef_CSS_Length()
  167. )
  168. );
  169. $this->info['line-height'] = new HTMLPurifier_AttrDef_CSS_Composite(
  170. array(
  171. new HTMLPurifier_AttrDef_Enum(array('normal')),
  172. new HTMLPurifier_AttrDef_CSS_Number(true), // no negatives
  173. new HTMLPurifier_AttrDef_CSS_Length('0'),
  174. new HTMLPurifier_AttrDef_CSS_Percentage(true)
  175. )
  176. );
  177. $margin =
  178. $this->info['margin-top'] =
  179. $this->info['margin-bottom'] =
  180. $this->info['margin-left'] =
  181. $this->info['margin-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
  182. array(
  183. new HTMLPurifier_AttrDef_CSS_Length(),
  184. new HTMLPurifier_AttrDef_CSS_Percentage(),
  185. new HTMLPurifier_AttrDef_Enum(array('auto'))
  186. )
  187. );
  188. $this->info['margin'] = new HTMLPurifier_AttrDef_CSS_Multiple($margin);
  189. // non-negative
  190. $padding =
  191. $this->info['padding-top'] =
  192. $this->info['padding-bottom'] =
  193. $this->info['padding-left'] =
  194. $this->info['padding-right'] = new HTMLPurifier_AttrDef_CSS_Composite(
  195. array(
  196. new HTMLPurifier_AttrDef_CSS_Length('0'),
  197. new HTMLPurifier_AttrDef_CSS_Percentage(true)
  198. )
  199. );
  200. $this->info['padding'] = new HTMLPurifier_AttrDef_CSS_Multiple($padding);
  201. $this->info['text-indent'] = new HTMLPurifier_AttrDef_CSS_Composite(
  202. array(
  203. new HTMLPurifier_AttrDef_CSS_Length(),
  204. new HTMLPurifier_AttrDef_CSS_Percentage()
  205. )
  206. );
  207. $trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  208. array(
  209. new HTMLPurifier_AttrDef_CSS_Length('0'),
  210. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  211. new HTMLPurifier_AttrDef_Enum(array('auto', 'initial', 'inherit'))
  212. )
  213. );
  214. $trusted_min_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  215. array(
  216. new HTMLPurifier_AttrDef_CSS_Length('0'),
  217. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  218. new HTMLPurifier_AttrDef_Enum(array('initial', 'inherit'))
  219. )
  220. );
  221. $trusted_max_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  222. array(
  223. new HTMLPurifier_AttrDef_CSS_Length('0'),
  224. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  225. new HTMLPurifier_AttrDef_Enum(array('none', 'initial', 'inherit'))
  226. )
  227. );
  228. $max = $config->get('CSS.MaxImgLength');
  229. $this->info['width'] =
  230. $this->info['height'] =
  231. $max === null ?
  232. $trusted_wh :
  233. new HTMLPurifier_AttrDef_Switch(
  234. 'img',
  235. // For img tags:
  236. new HTMLPurifier_AttrDef_CSS_Composite(
  237. array(
  238. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  239. new HTMLPurifier_AttrDef_Enum(array('auto'))
  240. )
  241. ),
  242. // For everyone else:
  243. $trusted_wh
  244. );
  245. $this->info['min-width'] =
  246. $this->info['min-height'] =
  247. $max === null ?
  248. $trusted_min_wh :
  249. new HTMLPurifier_AttrDef_Switch(
  250. 'img',
  251. // For img tags:
  252. new HTMLPurifier_AttrDef_CSS_Composite(
  253. array(
  254. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  255. new HTMLPurifier_AttrDef_Enum(array('initial', 'inherit'))
  256. )
  257. ),
  258. // For everyone else:
  259. $trusted_min_wh
  260. );
  261. $this->info['max-width'] =
  262. $this->info['max-height'] =
  263. $max === null ?
  264. $trusted_max_wh :
  265. new HTMLPurifier_AttrDef_Switch(
  266. 'img',
  267. // For img tags:
  268. new HTMLPurifier_AttrDef_CSS_Composite(
  269. array(
  270. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  271. new HTMLPurifier_AttrDef_Enum(array('none', 'initial', 'inherit'))
  272. )
  273. ),
  274. // For everyone else:
  275. $trusted_max_wh
  276. );
  277. $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
  278. $this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
  279. // this could use specialized code
  280. $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
  281. array(
  282. 'normal',
  283. 'bold',
  284. 'bolder',
  285. 'lighter',
  286. '100',
  287. '200',
  288. '300',
  289. '400',
  290. '500',
  291. '600',
  292. '700',
  293. '800',
  294. '900'
  295. ),
  296. false
  297. );
  298. // MUST be called after other font properties, as it references
  299. // a CSSDefinition object
  300. $this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
  301. // same here
  302. $this->info['border'] =
  303. $this->info['border-bottom'] =
  304. $this->info['border-top'] =
  305. $this->info['border-left'] =
  306. $this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
  307. $this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(
  308. array('collapse', 'separate')
  309. );
  310. $this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(
  311. array('top', 'bottom')
  312. );
  313. $this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(
  314. array('auto', 'fixed')
  315. );
  316. $this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(
  317. array(
  318. new HTMLPurifier_AttrDef_Enum(
  319. array(
  320. 'baseline',
  321. 'sub',
  322. 'super',
  323. 'top',
  324. 'text-top',
  325. 'middle',
  326. 'bottom',
  327. 'text-bottom'
  328. )
  329. ),
  330. new HTMLPurifier_AttrDef_CSS_Length(),
  331. new HTMLPurifier_AttrDef_CSS_Percentage()
  332. )
  333. );
  334. $this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
  335. // These CSS properties don't work on many browsers, but we live
  336. // in THE FUTURE!
  337. $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(
  338. array('nowrap', 'normal', 'pre', 'pre-wrap', 'pre-line')
  339. );
  340. if ($config->get('CSS.Proprietary')) {
  341. $this->doSetupProprietary($config);
  342. }
  343. if ($config->get('CSS.AllowTricky')) {
  344. $this->doSetupTricky($config);
  345. }
  346. if ($config->get('CSS.Trusted')) {
  347. $this->doSetupTrusted($config);
  348. }
  349. $allow_important = $config->get('CSS.AllowImportant');
  350. // wrap all attr-defs with decorator that handles !important
  351. foreach ($this->info as $k => $v) {
  352. $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
  353. }
  354. $this->setupConfigStuff($config);
  355. }
  356. /**
  357. * @param HTMLPurifier_Config $config
  358. */
  359. protected function doSetupProprietary($config)
  360. {
  361. // Internet Explorer only scrollbar colors
  362. $this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  363. $this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  364. $this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  365. $this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  366. $this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  367. $this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  368. // vendor specific prefixes of opacity
  369. $this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  370. $this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  371. // only opacity, for now
  372. $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
  373. // more CSS3
  374. $this->info['page-break-after'] =
  375. $this->info['page-break-before'] = new HTMLPurifier_AttrDef_Enum(
  376. array(
  377. 'auto',
  378. 'always',
  379. 'avoid',
  380. 'left',
  381. 'right'
  382. )
  383. );
  384. $this->info['page-break-inside'] = new HTMLPurifier_AttrDef_Enum(array('auto', 'avoid'));
  385. $border_radius = new HTMLPurifier_AttrDef_CSS_Composite(
  386. array(
  387. new HTMLPurifier_AttrDef_CSS_Percentage(true), // disallow negative
  388. new HTMLPurifier_AttrDef_CSS_Length('0') // disallow negative
  389. ));
  390. $this->info['border-top-left-radius'] =
  391. $this->info['border-top-right-radius'] =
  392. $this->info['border-bottom-right-radius'] =
  393. $this->info['border-bottom-left-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 2);
  394. // TODO: support SLASH syntax
  395. $this->info['border-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 4);
  396. }
  397. /**
  398. * @param HTMLPurifier_Config $config
  399. */
  400. protected function doSetupTricky($config)
  401. {
  402. $this->info['display'] = new HTMLPurifier_AttrDef_Enum(
  403. array(
  404. 'inline',
  405. 'block',
  406. 'list-item',
  407. 'run-in',
  408. 'compact',
  409. 'marker',
  410. 'table',
  411. 'inline-block',
  412. 'inline-table',
  413. 'table-row-group',
  414. 'table-header-group',
  415. 'table-footer-group',
  416. 'table-row',
  417. 'table-column-group',
  418. 'table-column',
  419. 'table-cell',
  420. 'table-caption',
  421. 'none'
  422. )
  423. );
  424. $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(
  425. array('visible', 'hidden', 'collapse')
  426. );
  427. $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(array('visible', 'hidden', 'auto', 'scroll'));
  428. $this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  429. }
  430. /**
  431. * @param HTMLPurifier_Config $config
  432. */
  433. protected function doSetupTrusted($config)
  434. {
  435. $this->info['position'] = new HTMLPurifier_AttrDef_Enum(
  436. array('static', 'relative', 'absolute', 'fixed')
  437. );
  438. $this->info['top'] =
  439. $this->info['left'] =
  440. $this->info['right'] =
  441. $this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(
  442. array(
  443. new HTMLPurifier_AttrDef_CSS_Length(),
  444. new HTMLPurifier_AttrDef_CSS_Percentage(),
  445. new HTMLPurifier_AttrDef_Enum(array('auto')),
  446. )
  447. );
  448. $this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(
  449. array(
  450. new HTMLPurifier_AttrDef_Integer(),
  451. new HTMLPurifier_AttrDef_Enum(array('auto')),
  452. )
  453. );
  454. }
  455. /**
  456. * Performs extra config-based processing. Based off of
  457. * HTMLPurifier_HTMLDefinition.
  458. * @param HTMLPurifier_Config $config
  459. * @todo Refactor duplicate elements into common class (probably using
  460. * composition, not inheritance).
  461. */
  462. protected function setupConfigStuff($config)
  463. {
  464. // setup allowed elements
  465. $support = "(for information on implementing this, see the " .
  466. "support forums) ";
  467. $allowed_properties = $config->get('CSS.AllowedProperties');
  468. if ($allowed_properties !== null) {
  469. foreach ($this->info as $name => $d) {
  470. if (!isset($allowed_properties[$name])) {
  471. unset($this->info[$name]);
  472. }
  473. unset($allowed_properties[$name]);
  474. }
  475. // emit errors
  476. foreach ($allowed_properties as $name => $d) {
  477. // :TODO: Is this htmlspecialchars() call really necessary?
  478. $name = htmlspecialchars($name);
  479. trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
  480. }
  481. }
  482. $forbidden_properties = $config->get('CSS.ForbiddenProperties');
  483. if ($forbidden_properties !== null) {
  484. foreach ($this->info as $name => $d) {
  485. if (isset($forbidden_properties[$name])) {
  486. unset($this->info[$name]);
  487. }
  488. }
  489. }
  490. }
  491. }
  492. // vim: et sw=4 sts=4