Skip to content

Commit

Permalink
Supply the image alt text when converting images
Browse files Browse the repository at this point in the history
In order to provide an accessible version of HTML content, where an alt tag
has been provided, this should be used.

Where text refers to an image inline (for example, an emoticon), complete
removal of that image can confuse the meaning of the text. As per WCAG 2.0
guidelines (http://www.w3.org/TR/WCAG20-TECHS/H37.html), images which
convey words should be provided with alt text which inclues those words.

This also relates to images which convey meaning in some other fashion.

We have been using this modification to Html2Text since August 2010.
  • Loading branch information
andrewnicols committed Oct 8, 2015
1 parent 35e78a7 commit f0b207d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Html2Text
'/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr>
'/<td[^>]*>(.*?)<\/td>/i', // <td> and </td>
'/<span class="_html2text_ignore">.+?<\/span>/i', // <span class="_html2text_ignore">...</span>
'/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // <img> with alt tag
);

/**
Expand Down Expand Up @@ -97,7 +98,8 @@ class Html2Text
"\n\n", // <table> and </table>
"\n", // <tr> and </tr>
"\t\t\\1\n", // <td> and </td>
"" // <span class="_html2text_ignore">...</span>
"", // <span class="_html2text_ignore">...</span>
'[\\2]', // <img> with alt tag
);

/**
Expand Down
42 changes: 42 additions & 0 deletions test/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Html2Text;

class ImageTest extends \PHPUnit_Framework_TestCase
{
public function testImageDataProvider() {
return array(
'Without alt tag' => array(
'html' => '<img src="http://example.com/example.jpg">',
'expected' => '',
),
'Without alt tag, wrapped in text' => array(
'html' => 'xx<img src="http://example.com/example.jpg">xx',
'expected' => 'xxxx',
),
'With alt tag' => array(
'html' => '<img src="http://example.com/example.jpg" alt="An example image">',
'expected' => '[An example image]',
),
'With alt, and title tags' => array(
'html' => '<img src="http://example.com/example.jpg" alt="An example image" title="Should be ignored">',
'expected' => '[An example image]',
),
'With alt tag, wrapped in text' => array(
'html' => 'xx<img src="http://example.com/example.jpg" alt="An example image">xx',
'expected' => 'xx[An example image]xx',
),
);
}

/**
* @dataProvider testImageDataProvider
*/
public function testImages($html, $expected)
{
$html2text = new Html2Text($html);
$output = $html2text->getText();

$this->assertEquals($expected, $output);
}
}

0 comments on commit f0b207d

Please sign in to comment.