CSSDefinition.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 = [];
  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. ['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. [
  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. ['none', 'left', 'right', 'both'],
  46. false
  47. );
  48. $this->info['float'] = new HTMLPurifier_AttrDef_Enum(
  49. ['none', 'left', 'right'],
  50. false
  51. );
  52. $this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
  53. ['normal', 'italic', 'oblique'],
  54. false
  55. );
  56. $this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
  57. ['normal', 'small-caps'],
  58. false
  59. );
  60. $uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
  61. [
  62. new HTMLPurifier_AttrDef_Enum(['none']),
  63. new HTMLPurifier_AttrDef_CSS_URI()
  64. ]
  65. );
  66. $this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
  67. ['inside', 'outside'],
  68. false
  69. );
  70. $this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
  71. [
  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. ['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. ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
  94. );
  95. $this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
  96. ['scroll', 'fixed']
  97. );
  98. $this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
  99. $this->info['background-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
  100. [
  101. new HTMLPurifier_AttrDef_Enum(
  102. [
  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. [
  121. new HTMLPurifier_AttrDef_Enum(['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. [
  133. new HTMLPurifier_AttrDef_Enum(['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. [
  140. new HTMLPurifier_AttrDef_Enum(['normal']),
  141. new HTMLPurifier_AttrDef_CSS_Length()
  142. ]
  143. );
  144. $this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(
  145. [
  146. new HTMLPurifier_AttrDef_Enum(['normal']),
  147. new HTMLPurifier_AttrDef_CSS_Length()
  148. ]
  149. );
  150. $this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(
  151. [
  152. new HTMLPurifier_AttrDef_Enum(
  153. [
  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. [
  171. new HTMLPurifier_AttrDef_Enum(['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. [
  183. new HTMLPurifier_AttrDef_CSS_Length(),
  184. new HTMLPurifier_AttrDef_CSS_Percentage(),
  185. new HTMLPurifier_AttrDef_Enum(['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. [
  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. [
  203. new HTMLPurifier_AttrDef_CSS_Length(),
  204. new HTMLPurifier_AttrDef_CSS_Percentage()
  205. ]
  206. );
  207. $trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  208. [
  209. new HTMLPurifier_AttrDef_CSS_Length('0'),
  210. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  211. new HTMLPurifier_AttrDef_Enum(['auto', 'initial', 'inherit'])
  212. ]
  213. );
  214. $trusted_min_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  215. [
  216. new HTMLPurifier_AttrDef_CSS_Length('0'),
  217. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  218. new HTMLPurifier_AttrDef_Enum(['initial', 'inherit'])
  219. ]
  220. );
  221. $trusted_max_wh = new HTMLPurifier_AttrDef_CSS_Composite(
  222. [
  223. new HTMLPurifier_AttrDef_CSS_Length('0'),
  224. new HTMLPurifier_AttrDef_CSS_Percentage(true),
  225. new HTMLPurifier_AttrDef_Enum(['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. [
  238. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  239. new HTMLPurifier_AttrDef_Enum(['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. [
  254. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  255. new HTMLPurifier_AttrDef_Enum(['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. [
  270. new HTMLPurifier_AttrDef_CSS_Length('0', $max),
  271. new HTMLPurifier_AttrDef_Enum(['none', 'initial', 'inherit'])
  272. ]
  273. ),
  274. // For everyone else:
  275. $trusted_max_wh
  276. );
  277. // text-decoration and related shorthands
  278. $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
  279. $this->info['text-decoration-line'] = new HTMLPurifier_AttrDef_Enum(
  280. ['none', 'underline', 'overline', 'line-through', 'initial', 'inherit']
  281. );
  282. $this->info['text-decoration-style'] = new HTMLPurifier_AttrDef_Enum(
  283. ['solid', 'double', 'dotted', 'dashed', 'wavy', 'initial', 'inherit']
  284. );
  285. $this->info['text-decoration-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  286. $this->info['text-decoration-thickness'] = new HTMLPurifier_AttrDef_CSS_Composite([
  287. new HTMLPurifier_AttrDef_CSS_Length(),
  288. new HTMLPurifier_AttrDef_CSS_Percentage(),
  289. new HTMLPurifier_AttrDef_Enum(['auto', 'from-font', 'initial', 'inherit'])
  290. ]);
  291. $this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
  292. // this could use specialized code
  293. $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
  294. [
  295. 'normal',
  296. 'bold',
  297. 'bolder',
  298. 'lighter',
  299. '100',
  300. '200',
  301. '300',
  302. '400',
  303. '500',
  304. '600',
  305. '700',
  306. '800',
  307. '900'
  308. ],
  309. false
  310. );
  311. // MUST be called after other font properties, as it references
  312. // a CSSDefinition object
  313. $this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
  314. // same here
  315. $this->info['border'] =
  316. $this->info['border-bottom'] =
  317. $this->info['border-top'] =
  318. $this->info['border-left'] =
  319. $this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
  320. $this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(
  321. ['collapse', 'separate']
  322. );
  323. $this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(
  324. ['top', 'bottom']
  325. );
  326. $this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(
  327. ['auto', 'fixed']
  328. );
  329. $this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(
  330. [
  331. new HTMLPurifier_AttrDef_Enum(
  332. [
  333. 'baseline',
  334. 'sub',
  335. 'super',
  336. 'top',
  337. 'text-top',
  338. 'middle',
  339. 'bottom',
  340. 'text-bottom'
  341. ]
  342. ),
  343. new HTMLPurifier_AttrDef_CSS_Length(),
  344. new HTMLPurifier_AttrDef_CSS_Percentage()
  345. ]
  346. );
  347. $this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
  348. // These CSS properties don't work on many browsers, but we live
  349. // in THE FUTURE!
  350. $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(
  351. ['nowrap', 'normal', 'pre', 'pre-wrap', 'pre-line']
  352. );
  353. if ($config->get('CSS.Proprietary')) {
  354. $this->doSetupProprietary($config);
  355. }
  356. if ($config->get('CSS.AllowTricky')) {
  357. $this->doSetupTricky($config);
  358. }
  359. if ($config->get('CSS.Trusted')) {
  360. $this->doSetupTrusted($config);
  361. }
  362. $allow_important = $config->get('CSS.AllowImportant');
  363. // wrap all attr-defs with decorator that handles !important
  364. foreach ($this->info as $k => $v) {
  365. $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
  366. }
  367. $this->setupConfigStuff($config);
  368. }
  369. /**
  370. * @param HTMLPurifier_Config $config
  371. */
  372. protected function doSetupProprietary($config)
  373. {
  374. // Internet Explorer only scrollbar colors
  375. $this->info['scrollbar-arrow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  376. $this->info['scrollbar-base-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  377. $this->info['scrollbar-darkshadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  378. $this->info['scrollbar-face-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  379. $this->info['scrollbar-highlight-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  380. $this->info['scrollbar-shadow-color'] = new HTMLPurifier_AttrDef_CSS_Color();
  381. // vendor specific prefixes of opacity
  382. $this->info['-moz-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  383. $this->info['-khtml-opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  384. // only opacity, for now
  385. $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
  386. // more CSS3
  387. $this->info['page-break-after'] =
  388. $this->info['page-break-before'] = new HTMLPurifier_AttrDef_Enum(
  389. [
  390. 'auto',
  391. 'always',
  392. 'avoid',
  393. 'left',
  394. 'right'
  395. ]
  396. );
  397. $this->info['page-break-inside'] = new HTMLPurifier_AttrDef_Enum(['auto', 'avoid']);
  398. $border_radius = new HTMLPurifier_AttrDef_CSS_Composite(
  399. [
  400. new HTMLPurifier_AttrDef_CSS_Percentage(true), // disallow negative
  401. new HTMLPurifier_AttrDef_CSS_Length('0') // disallow negative
  402. ]);
  403. $this->info['border-top-left-radius'] =
  404. $this->info['border-top-right-radius'] =
  405. $this->info['border-bottom-right-radius'] =
  406. $this->info['border-bottom-left-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 2);
  407. // TODO: support SLASH syntax
  408. $this->info['border-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius, 4);
  409. }
  410. /**
  411. * @param HTMLPurifier_Config $config
  412. */
  413. protected function doSetupTricky($config)
  414. {
  415. $this->info['display'] = new HTMLPurifier_AttrDef_Enum(
  416. [
  417. 'inline',
  418. 'block',
  419. 'list-item',
  420. 'run-in',
  421. 'compact',
  422. 'marker',
  423. 'table',
  424. 'inline-block',
  425. 'inline-table',
  426. 'table-row-group',
  427. 'table-header-group',
  428. 'table-footer-group',
  429. 'table-row',
  430. 'table-column-group',
  431. 'table-column',
  432. 'table-cell',
  433. 'table-caption',
  434. 'none'
  435. ]
  436. );
  437. $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(
  438. ['visible', 'hidden', 'collapse']
  439. );
  440. $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(['visible', 'hidden', 'auto', 'scroll']);
  441. $this->info['opacity'] = new HTMLPurifier_AttrDef_CSS_AlphaValue();
  442. }
  443. /**
  444. * @param HTMLPurifier_Config $config
  445. */
  446. protected function doSetupTrusted($config)
  447. {
  448. $this->info['position'] = new HTMLPurifier_AttrDef_Enum(
  449. ['static', 'relative', 'absolute', 'fixed']
  450. );
  451. $this->info['top'] =
  452. $this->info['left'] =
  453. $this->info['right'] =
  454. $this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(
  455. [
  456. new HTMLPurifier_AttrDef_CSS_Length(),
  457. new HTMLPurifier_AttrDef_CSS_Percentage(),
  458. new HTMLPurifier_AttrDef_Enum(['auto']),
  459. ]
  460. );
  461. $this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(
  462. [
  463. new HTMLPurifier_AttrDef_Integer(),
  464. new HTMLPurifier_AttrDef_Enum(['auto']),
  465. ]
  466. );
  467. }
  468. /**
  469. * Performs extra config-based processing. Based off of
  470. * HTMLPurifier_HTMLDefinition.
  471. * @param HTMLPurifier_Config $config
  472. * @todo Refactor duplicate elements into common class (probably using
  473. * composition, not inheritance).
  474. */
  475. protected function setupConfigStuff($config)
  476. {
  477. // setup allowed elements
  478. $support = "(for information on implementing this, see the " .
  479. "support forums) ";
  480. $allowed_properties = $config->get('CSS.AllowedProperties');
  481. if ($allowed_properties !== null) {
  482. foreach ($this->info as $name => $d) {
  483. if (!isset($allowed_properties[$name])) {
  484. unset($this->info[$name]);
  485. }
  486. unset($allowed_properties[$name]);
  487. }
  488. // emit errors
  489. foreach ($allowed_properties as $name => $d) {
  490. // :TODO: Is this htmlspecialchars() call really necessary?
  491. $name = htmlspecialchars($name);
  492. trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
  493. }
  494. }
  495. $forbidden_properties = $config->get('CSS.ForbiddenProperties');
  496. if ($forbidden_properties !== null) {
  497. foreach ($this->info as $name => $d) {
  498. if (isset($forbidden_properties[$name])) {
  499. unset($this->info[$name]);
  500. }
  501. }
  502. }
  503. }
  504. }
  505. // vim: et sw=4 sts=4