国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

113 lines
3.0KB

  1. <?php
  2. /**
  3. * Validates shorthand CSS property list-style.
  4. * @warning Does not support url tokens that have internal spaces.
  5. */
  6. class HTMLPurifier_AttrDef_CSS_ListStyle extends HTMLPurifier_AttrDef
  7. {
  8. /**
  9. * Local copy of validators.
  10. * @type HTMLPurifier_AttrDef[]
  11. * @note See HTMLPurifier_AttrDef_CSS_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['list-style-type'] = $def->info['list-style-type'];
  21. $this->info['list-style-position'] = $def->info['list-style-position'];
  22. $this->info['list-style-image'] = $def->info['list-style-image'];
  23. }
  24. /**
  25. * @param string $string
  26. * @param HTMLPurifier_Config $config
  27. * @param HTMLPurifier_Context $context
  28. * @return bool|string
  29. */
  30. public function validate($string, $config, $context)
  31. {
  32. // regular pre-processing
  33. $string = $this->parseCDATA($string);
  34. if ($string === '') {
  35. return false;
  36. }
  37. // assumes URI doesn't have spaces in it
  38. $bits = explode(' ', strtolower($string)); // bits to process
  39. $caught = array();
  40. $caught['type'] = false;
  41. $caught['position'] = false;
  42. $caught['image'] = false;
  43. $i = 0; // number of catches
  44. $none = false;
  45. foreach ($bits as $bit) {
  46. if ($i >= 3) {
  47. return;
  48. } // optimization bit
  49. if ($bit === '') {
  50. continue;
  51. }
  52. foreach ($caught as $key => $status) {
  53. if ($status !== false) {
  54. continue;
  55. }
  56. $r = $this->info['list-style-' . $key]->validate($bit, $config, $context);
  57. if ($r === false) {
  58. continue;
  59. }
  60. if ($r === 'none') {
  61. if ($none) {
  62. continue;
  63. } else {
  64. $none = true;
  65. }
  66. if ($key == 'image') {
  67. continue;
  68. }
  69. }
  70. $caught[$key] = $r;
  71. $i++;
  72. break;
  73. }
  74. }
  75. if (!$i) {
  76. return false;
  77. }
  78. $ret = array();
  79. // construct type
  80. if ($caught['type']) {
  81. $ret[] = $caught['type'];
  82. }
  83. // construct image
  84. if ($caught['image']) {
  85. $ret[] = $caught['image'];
  86. }
  87. // construct position
  88. if ($caught['position']) {
  89. $ret[] = $caught['position'];
  90. }
  91. if (empty($ret)) {
  92. return false;
  93. }
  94. return implode(' ', $ret);
  95. }
  96. }
  97. // vim: et sw=4 sts=4