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.

ImgRequired.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. // must be called POST validation
  3. /**
  4. * Transform that supplies default values for the src and alt attributes
  5. * in img tags, as well as prevents the img tag from being removed
  6. * because of a missing alt tag. This needs to be registered as both
  7. * a pre and post attribute transform.
  8. */
  9. class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
  10. {
  11. /**
  12. * @param array $attr
  13. * @param HTMLPurifier_Config $config
  14. * @param HTMLPurifier_Context $context
  15. * @return array
  16. */
  17. public function transform($attr, $config, $context)
  18. {
  19. $src = true;
  20. if (!isset($attr['src'])) {
  21. if ($config->get('Core.RemoveInvalidImg')) {
  22. return $attr;
  23. }
  24. $attr['src'] = $config->get('Attr.DefaultInvalidImage');
  25. $src = false;
  26. }
  27. if (!isset($attr['alt'])) {
  28. if ($src) {
  29. $alt = $config->get('Attr.DefaultImageAlt');
  30. if ($alt === null) {
  31. // truncate if the alt is too long
  32. $attr['alt'] = substr(basename($attr['src']), 0, 40);
  33. } else {
  34. $attr['alt'] = $alt;
  35. }
  36. } else {
  37. $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
  38. }
  39. }
  40. return $attr;
  41. }
  42. }
  43. // vim: et sw=4 sts=4