By default, HTML Purifier adds an alt
attribute to each img
tag (really annoying behavior). So
<img src="123.jpg" />
becomes
<img src="123.jpg" alt="123.jpg" />
Documentation mentiones an Attr.DefaultImageAlt option. It defaults to NULL meaning to use the basename of the src
attribute for the alt
. When I set Attr.DefaultImageAlt
to an empty string the result becomes
<img src="123.jpg" alt="" />
Anyone can suggest how to get rid of the alt
attribute completely?
What you're observing stems from that the alt
attribute is mandatory for img
tags according to the standards, and HTML Purifier takes the standards into account.
That means HTML Purifier, unless you tweak its fundamental HTML handling behaviour (be it by patching HTML Purifier, or by overriding its understanding of certain tags or attributes), cannot be made to leave away the alt=
attribute.
(Browsers actually have a similar behaviour, though it may not be as apparent - if you remove alt=
, they will still have an internal alt=
value that they use instead.)
If this information doesn't change your opinion on how to handle the attribute, read on:
(i.e. changing the behaviour by changing the HTML Purifier source code.)
If you want to patch HTML Purifier to allow alt
to be absent, you should patch library/HTMLPurifier/AttrTransform/ImgRequired.php. You can also see how the Attr.DefaultImageAlt
directive is used there - if you supply a value of null
(rather than an empty string), part of the filename will be used as the alt
value.
(i.e. changing the behaviour without changing the HTML Purifier source code.)
If you want to override the HTML Purifier behaviour, check out the Customize! documentation on the HTML Purifier site.
Without having tested it, I believe you need to make two changes to override the behaviour you see:
1) Make alt
non-mandatory:
$htmlDef = $this->configuration->getHTMLDefinition(true);
$htmlDef->addAttribute('img', 'alt', new HTMLPurifier_AttrDef_Text());
The lack of *
should help you there.
2) Remove or replace the ImgRequired
attribute-transformation.
You can see that the HTMLPurifier_AttrTransform_ImgRequired
class ends up getting registered to both $htmlDef->info_attr_transform_post['img']
and $htmlDef->info_attr_transform_pre['img']
in library/HTMLPurifier/HTMLModule/Image.php. You should be able to do something like this:
$htmlDef->info_attr_transform_pre['img'] = array();
$htmlDef->info_attr_transform_post['img'] = array();
// You can *replace* the old behaviour with your own by writing
// your own class and loading it here:
// $htmlDef->info_attr_transform_pre['img'][] = new YourOwnClass();
// $htmlDef->info_attr_transform_post['img'][] = new YourOwnClass();
There may be some roadblocks on the way to getting this to work (e.g. the class may be registered somewhere subtly different that I just said it would be - it's been a few years since I tinkered with HTML Purifier on this level!), but this should set you on a good path to getting your hands dirty on HTML Purifier code. :)