AttrTransform.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Processes an entire attribute array for corrections needing multiple values.
  4. *
  5. * Occasionally, a certain attribute will need to be removed and popped onto
  6. * another value. Instead of creating a complex return syntax for
  7. * HTMLPurifier_AttrDef, we just pass the whole attribute array to a
  8. * specialized object and have that do the special work. That is the
  9. * family of HTMLPurifier_AttrTransform.
  10. *
  11. * An attribute transformation can be assigned to run before or after
  12. * HTMLPurifier_AttrDef validation. See HTMLPurifier_HTMLDefinition for
  13. * more details.
  14. */
  15. abstract class HTMLPurifier_AttrTransform
  16. {
  17. /**
  18. * Abstract: makes changes to the attributes dependent on multiple values.
  19. *
  20. * @param $attr Assoc array of attributes, usually from
  21. * HTMLPurifier_Token_Tag::$attr
  22. * @param $config Mandatory HTMLPurifier_Config object.
  23. * @param $context Mandatory HTMLPurifier_Context object
  24. * @returns Processed attribute array.
  25. */
  26. abstract public function transform($attr, $config, $context);
  27. /**
  28. * Prepends CSS properties to the style attribute, creating the
  29. * attribute if it doesn't exist.
  30. * @param $attr Attribute array to process (passed by reference)
  31. * @param $css CSS to prepend
  32. */
  33. public function prependCSS(&$attr, $css) {
  34. $attr['style'] = isset($attr['style']) ? $attr['style'] : '';
  35. $attr['style'] = $css . $attr['style'];
  36. }
  37. /**
  38. * Retrieves and removes an attribute
  39. * @param $attr Attribute array to process (passed by reference)
  40. * @param $key Key of attribute to confiscate
  41. */
  42. public function confiscateAttr(&$attr, $key) {
  43. if (!isset($attr[$key])) return null;
  44. $value = $attr[$key];
  45. unset($attr[$key]);
  46. return $value;
  47. }
  48. }
  49. // vim: et sw=4 sts=4