Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrTransform / ImgRequired.php
blobd0428055381f1b59eb8cae70a145888615d869ca
1 <?php
3 require_once 'HTMLPurifier/AttrTransform.php';
5 // must be called POST validation
7 HTMLPurifier_ConfigSchema::define(
8 'Attr', 'DefaultInvalidImage', '', 'string',
9 'This is the default image an img tag will be pointed to if it does '.
10 'not have a valid src attribute. In future versions, we may allow the '.
11 'image tag to be removed completely, but due to design issues, this is '.
12 'not possible right now.'
15 HTMLPurifier_ConfigSchema::define(
16 'Attr', 'DefaultInvalidImageAlt', 'Invalid image', 'string',
17 'This is the content of the alt tag of an invalid image if the user '.
18 'had not previously specified an alt attribute. It has no effect when the '.
19 'image is valid but there was no alt attribute present.'
22 /**
23 * Transform that supplies default values for the src and alt attributes
24 * in img tags, as well as prevents the img tag from being removed
25 * because of a missing alt tag. This needs to be registered as both
26 * a pre and post attribute transform.
28 class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
31 function transform($attr, $config, &$context) {
33 $src = true;
34 if (!isset($attr['src'])) {
35 if ($config->get('Core', 'RemoveInvalidImg')) return $attr;
36 $attr['src'] = $config->get('Attr', 'DefaultInvalidImage');
37 $src = false;
40 if (!isset($attr['alt'])) {
41 if ($src) {
42 $attr['alt'] = basename($attr['src']);
43 } else {
44 $attr['alt'] = $config->get('Attr', 'DefaultInvalidImageAlt');
48 return $attr;