EnumToCSS.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Generic pre-transform that converts an attribute with a fixed number of
  4. * values (enumerated) to CSS.
  5. */
  6. class HTMLPurifier_AttrTransform_EnumToCSS extends HTMLPurifier_AttrTransform
  7. {
  8. /**
  9. * Name of attribute to transform from.
  10. * @type string
  11. */
  12. protected $attr;
  13. /**
  14. * Lookup array of attribute values to CSS.
  15. * @type array
  16. */
  17. protected $enumToCSS = array();
  18. /**
  19. * Case sensitivity of the matching.
  20. * @type bool
  21. * @warning Currently can only be guaranteed to work with ASCII
  22. * values.
  23. */
  24. protected $caseSensitive = false;
  25. /**
  26. * @param string $attr Attribute name to transform from
  27. * @param array $enum_to_css Lookup array of attribute values to CSS
  28. * @param bool $case_sensitive Case sensitivity indicator, default false
  29. */
  30. public function __construct($attr, $enum_to_css, $case_sensitive = false)
  31. {
  32. $this->attr = $attr;
  33. $this->enumToCSS = $enum_to_css;
  34. $this->caseSensitive = (bool)$case_sensitive;
  35. }
  36. /**
  37. * @param array $attr
  38. * @param HTMLPurifier_Config $config
  39. * @param HTMLPurifier_Context $context
  40. * @return array
  41. */
  42. public function transform($attr, $config, $context)
  43. {
  44. if (!isset($attr[$this->attr])) {
  45. return $attr;
  46. }
  47. $value = trim($attr[$this->attr]);
  48. unset($attr[$this->attr]);
  49. if (!$this->caseSensitive) {
  50. $value = strtolower($value);
  51. }
  52. if (!isset($this->enumToCSS[$value])) {
  53. return $attr;
  54. }
  55. $this->prependCSS($attr, $this->enumToCSS[$value]);
  56. return $attr;
  57. }
  58. }
  59. // vim: et sw=4 sts=4