Background.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Validates shorthand CSS property background.
  4. * @warning Does not support url tokens that have internal spaces.
  5. */
  6. class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
  7. {
  8. /**
  9. * Local copy of component validators.
  10. * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
  11. */
  12. protected $info;
  13. public function __construct($config) {
  14. $def = $config->getCSSDefinition();
  15. $this->info['background-color'] = $def->info['background-color'];
  16. $this->info['background-image'] = $def->info['background-image'];
  17. $this->info['background-repeat'] = $def->info['background-repeat'];
  18. $this->info['background-attachment'] = $def->info['background-attachment'];
  19. $this->info['background-position'] = $def->info['background-position'];
  20. }
  21. public function validate($string, $config, $context) {
  22. // regular pre-processing
  23. $string = $this->parseCDATA($string);
  24. if ($string === '') return false;
  25. // munge rgb() decl if necessary
  26. $string = $this->mungeRgb($string);
  27. // assumes URI doesn't have spaces in it
  28. $bits = explode(' ', $string); // bits to process
  29. $caught = array();
  30. $caught['color'] = false;
  31. $caught['image'] = false;
  32. $caught['repeat'] = false;
  33. $caught['attachment'] = false;
  34. $caught['position'] = false;
  35. $i = 0; // number of catches
  36. $none = false;
  37. foreach ($bits as $bit) {
  38. if ($bit === '') continue;
  39. foreach ($caught as $key => $status) {
  40. if ($key != 'position') {
  41. if ($status !== false) continue;
  42. $r = $this->info['background-' . $key]->validate($bit, $config, $context);
  43. } else {
  44. $r = $bit;
  45. }
  46. if ($r === false) continue;
  47. if ($key == 'position') {
  48. if ($caught[$key] === false) $caught[$key] = '';
  49. $caught[$key] .= $r . ' ';
  50. } else {
  51. $caught[$key] = $r;
  52. }
  53. $i++;
  54. break;
  55. }
  56. }
  57. if (!$i) return false;
  58. if ($caught['position'] !== false) {
  59. $caught['position'] = $this->info['background-position']->
  60. validate($caught['position'], $config, $context);
  61. }
  62. $ret = array();
  63. foreach ($caught as $value) {
  64. if ($value === false) continue;
  65. $ret[] = $value;
  66. }
  67. if (empty($ret)) return false;
  68. return implode(' ', $ret);
  69. }
  70. }
  71. // vim: et sw=4 sts=4