]>
Commit | Line | Data |
---|---|---|
f45a286b AD |
1 | <?php |
2 | ||
3 | // must be called POST validation | |
4 | ||
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. | |
10 | */ | |
11 | class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform | |
12 | { | |
13 | ||
14 | public function transform($attr, $config, $context) { | |
15 | ||
16 | $src = true; | |
17 | if (!isset($attr['src'])) { | |
f4f0f80d AD |
18 | if ($config->get('Core.RemoveInvalidImg')) return $attr; |
19 | $attr['src'] = $config->get('Attr.DefaultInvalidImage'); | |
f45a286b AD |
20 | $src = false; |
21 | } | |
22 | ||
23 | if (!isset($attr['alt'])) { | |
24 | if ($src) { | |
f4f0f80d | 25 | $alt = $config->get('Attr.DefaultImageAlt'); |
f45a286b | 26 | if ($alt === null) { |
f4f0f80d AD |
27 | // truncate if the alt is too long |
28 | $attr['alt'] = substr(basename($attr['src']),0,40); | |
f45a286b AD |
29 | } else { |
30 | $attr['alt'] = $alt; | |
31 | } | |
32 | } else { | |
f4f0f80d | 33 | $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt'); |
f45a286b AD |
34 | } |
35 | } | |
36 | ||
37 | return $attr; | |
38 | ||
39 | } | |
40 | ||
41 | } | |
42 | ||
43 | // vim: et sw=4 sts=4 |