Truncate alt text from src if it's too long.
[htmlpurifier.git] / library / HTMLPurifier / AttrTransform / ImgRequired.php
blob7f0e4b7a59fbf5babd4203be3b195477d3790635
1 <?php
3 // must be called POST validation
5 /**
6 * Transform that supplies default values for the src and alt attributes
7 * in img tags, as well as prevents the img tag from being removed
8 * because of a missing alt tag. This needs to be registered as both
9 * a pre and post attribute transform.
11 class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
14 public function transform($attr, $config, $context) {
16 $src = true;
17 if (!isset($attr['src'])) {
18 if ($config->get('Core.RemoveInvalidImg')) return $attr;
19 $attr['src'] = $config->get('Attr.DefaultInvalidImage');
20 $src = false;
23 if (!isset($attr['alt'])) {
24 if ($src) {
25 $alt = $config->get('Attr.DefaultImageAlt');
26 if ($alt === null) {
27 // truncate if the alt is too long
28 $attr['alt'] = substr(basename($attr['src']),0,40);
29 } else {
30 $attr['alt'] = $alt;
32 } else {
33 $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
37 return $attr;
43 // vim: et sw=4 sts=4