0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-18 18:25:18 +00:00

Implement %Attr.DefaultImageAlt, allowing overriding default behavior for alt attributes.

Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
Edward Z. Yang 2008-10-06 14:51:03 -04:00
parent 70515dd48f
commit f7bc0b0875
6 changed files with 34 additions and 4 deletions

2
NEWS
View File

@ -44,6 +44,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
! HTMLPurifier_Injector->handleEnd() permits modification to end tokens. The
time of operation varies slightly from notifyEnd() as *all* end tokens are
processed by the injector before they are subject to the well-formedness rules.
! %Attr.DefaultImageAlt allows overriding default behavior of setting alt to
basename of image when not present.
- Fix two bugs in %URI.MakeAbsolute; one involving empty paths in base URLs,
the other involving an undefined $is_folder error.
- Throw error when %Core.Encoding is set to a spurious value. Previously,

View File

@ -290,9 +290,14 @@
<line>19</line>
</file>
</directive>
<directive id="Attr.DefaultImageAlt">
<file name="HTMLPurifier/AttrTransform/ImgRequired.php">
<line>25</line>
</file>
</directive>
<directive id="Attr.DefaultInvalidImageAlt">
<file name="HTMLPurifier/AttrTransform/ImgRequired.php">
<line>27</line>
<line>32</line>
</file>
</directive>
<directive id="Core.EscapeInvalidChildren">
@ -363,7 +368,7 @@
</directive>
<directive id="Core.EscapeInvalidTags">
<file name="HTMLPurifier/Strategy/MakeWellFormed.php">
<line>21</line>
<line>45</line>
</file>
<file name="HTMLPurifier/Strategy/RemoveForeignElements.php">
<line>19</line>

View File

@ -22,7 +22,12 @@ class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
if (!isset($attr['alt'])) {
if ($src) {
$attr['alt'] = basename($attr['src']);
$alt = $config->get('Attr', 'DefaultImageAlt');
if ($alt === null) {
$attr['alt'] = basename($attr['src']);
} else {
$attr['alt'] = $alt;
}
} else {
$attr['alt'] = $config->get('Attr', 'DefaultInvalidImageAlt');
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,9 @@
Attr.DefaultImageAlt
TYPE: string/null
DEFAULT: null
--DESCRIPTION--
This is the content of the alt tag of an image if the user had not
previously specified an alt attribute. This applies to all images without
a valid alt attribute, as opposed to %Attr.DefaultInvalidImageAlt, which
only applies to invalid images, and overrides in the case of an invalid image.
Default behavior with null is to use the basename of the src tag for the alt.

View File

@ -19,6 +19,7 @@ class HTMLPurifier_AttrTransform_ImgRequiredTest extends HTMLPurifier_AttrTransf
function testAlternateDefaults() {
$this->config->set('Attr', 'DefaultInvalidImage', 'blank.png');
$this->config->set('Attr', 'DefaultInvalidImageAlt', 'Pawned!');
$this->config->set('Attr', 'DefaultImageAlt', 'not pawned');
$this->config->set('Core', 'RemoveInvalidImg', false);
$this->assertResult(
array(),
@ -41,5 +42,13 @@ class HTMLPurifier_AttrTransform_ImgRequiredTest extends HTMLPurifier_AttrTransf
);
}
function testAddDefaultAlt() {
$this->config->set('Attr', 'DefaultImageAlt', 'default');
$this->assertResult(
array('src' => ''),
array('src' => '', 'alt' => 'default')
);
}
}