EnumToCSS.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. * Name of attribute to transform from
  9. */
  10. protected $attr;
  11. /**
  12. * Lookup array of attribute values to CSS
  13. */
  14. protected $enumToCSS = array();
  15. /**
  16. * Case sensitivity of the matching
  17. * @warning Currently can only be guaranteed to work with ASCII
  18. * values.
  19. */
  20. protected $caseSensitive = false;
  21. /**
  22. * @param $attr String attribute name to transform from
  23. * @param $enumToCSS Lookup array of attribute values to CSS
  24. * @param $case_sensitive Boolean case sensitivity indicator, default false
  25. */
  26. public function __construct($attr, $enum_to_css, $case_sensitive = false) {
  27. $this->attr = $attr;
  28. $this->enumToCSS = $enum_to_css;
  29. $this->caseSensitive = (bool) $case_sensitive;
  30. }
  31. public function transform($attr, $config, $context) {
  32. if (!isset($attr[$this->attr])) return $attr;
  33. $value = trim($attr[$this->attr]);
  34. unset($attr[$this->attr]);
  35. if (!$this->caseSensitive) $value = strtolower($value);
  36. if (!isset($this->enumToCSS[$value])) {
  37. return $attr;
  38. }
  39. $this->prependCSS($attr, $this->enumToCSS[$value]);
  40. return $attr;
  41. }
  42. }
  43. // vim: et sw=4 sts=4