You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.1KB

  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. * @type HTMLPurifier_AttrDef[]
  11. * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
  12. */
  13. protected $info;
  14. /**
  15. * @param HTMLPurifier_Config $config
  16. */
  17. public function __construct($config)
  18. {
  19. $def = $config->getCSSDefinition();
  20. $this->info['background-color'] = $def->info['background-color'];
  21. $this->info['background-image'] = $def->info['background-image'];
  22. $this->info['background-repeat'] = $def->info['background-repeat'];
  23. $this->info['background-attachment'] = $def->info['background-attachment'];
  24. $this->info['background-position'] = $def->info['background-position'];
  25. }
  26. /**
  27. * @param string $string
  28. * @param HTMLPurifier_Config $config
  29. * @param HTMLPurifier_Context $context
  30. * @return bool|string
  31. */
  32. public function validate($string, $config, $context)
  33. {
  34. // regular pre-processing
  35. $string = $this->parseCDATA($string);
  36. if ($string === '') {
  37. return false;
  38. }
  39. // munge rgb() decl if necessary
  40. $string = $this->mungeRgb($string);
  41. // assumes URI doesn't have spaces in it
  42. $bits = explode(' ', $string); // bits to process
  43. $caught = array();
  44. $caught['color'] = false;
  45. $caught['image'] = false;
  46. $caught['repeat'] = false;
  47. $caught['attachment'] = false;
  48. $caught['position'] = false;
  49. $i = 0; // number of catches
  50. foreach ($bits as $bit) {
  51. if ($bit === '') {
  52. continue;
  53. }
  54. foreach ($caught as $key => $status) {
  55. if ($key != 'position') {
  56. if ($status !== false) {
  57. continue;
  58. }
  59. $r = $this->info['background-' . $key]->validate($bit, $config, $context);
  60. } else {
  61. $r = $bit;
  62. }
  63. if ($r === false) {
  64. continue;
  65. }
  66. if ($key == 'position') {
  67. if ($caught[$key] === false) {
  68. $caught[$key] = '';
  69. }
  70. $caught[$key] .= $r . ' ';
  71. } else {
  72. $caught[$key] = $r;
  73. }
  74. $i++;
  75. break;
  76. }
  77. }
  78. if (!$i) {
  79. return false;
  80. }
  81. if ($caught['position'] !== false) {
  82. $caught['position'] = $this->info['background-position']->
  83. validate($caught['position'], $config, $context);
  84. }
  85. $ret = array();
  86. foreach ($caught as $value) {
  87. if ($value === false) {
  88. continue;
  89. }
  90. $ret[] = $value;
  91. }
  92. if (empty($ret)) {
  93. return false;
  94. }
  95. return implode(' ', $ret);
  96. }
  97. }
  98. // vim: et sw=4 sts=4