Skip to content

Commit a8cb26a

Browse files
committed
add option to hide alt text
1 parent b0fb318 commit a8cb26a

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

src/Html2Text.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class Html2Text
6868
'/(<tr\b[^>]*>|<\/tr>)/i', // <tr> and </tr>
6969
'/<td\b[^>]*>(.*?)<\/td>/i', // <td> and </td>
7070
'/<span class="_html2text_ignore">.+?<\/span>/i', // <span class="_html2text_ignore">...</span>
71-
'/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i', // <img> with alt tag
7271
);
7372

7473
/**
@@ -99,7 +98,6 @@ class Html2Text
9998
"\n", // <tr> and </tr>
10099
"\t\t\\1\n", // <td> and </td>
101100
"", // <span class="_html2text_ignore">...</span>
102-
'[\\2]', // <img> with alt tag
103101
);
104102

105103
/**
@@ -222,6 +220,9 @@ class Html2Text
222220
'width' => 70, // Maximum width of the formatted text, in columns.
223221
// Set this value to 0 (or less) to ignore word wrapping
224222
// and not constrain text to a fixed-width column.
223+
224+
'alt_image'=>'show', // 'show' (show the alt text in brackets)
225+
// 'hide' (don't show the alt text)
225226
);
226227

227228
private function legacyConstruct($html = '', $fromFile = false, array $options = array())
@@ -368,7 +369,7 @@ protected function doConvert()
368369
}
369370

370371
protected function converter(&$text)
371-
{
372+
{ $this->convertImages();
372373
$this->convertBlockquotes($text);
373374
$this->convertPre($text);
374375
$text = preg_replace($this->search, $this->replace, $text);
@@ -396,6 +397,16 @@ protected function converter(&$text)
396397
}
397398
}
398399

400+
/**
401+
* show/hide alt text for images
402+
*/
403+
404+
protected function convertImages(){
405+
406+
$this->search[] = '/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i';
407+
$this->replace[] = $this->options['alt_image'] == 'show'?'[\\2]':'';
408+
}
409+
399410
/**
400411
* Helper function called by preg_replace() on link replacement.
401412
*

test/ImageTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
class ImageTest extends TestCase
88
{
9-
public function testImageDataProvider() {
9+
public function testImageDataWithAltProvider() {
10+
$this->expectNotToPerformAssertions();
1011
return array(
1112
'Without alt tag' => array(
1213
'html' => '<img src="http://example.com/example.jpg">',
@@ -35,14 +36,55 @@ public function testImageDataProvider() {
3536
);
3637
}
3738

39+
public function testImageDataWithoutAltProvider() {
40+
$this->expectNotToPerformAssertions();
41+
return array(
42+
'Without alt tag' => array(
43+
'html' => '<img src="http://example.com/example.jpg">',
44+
'expected' => '',
45+
),
46+
'Without alt tag, wrapped in text' => array(
47+
'html' => 'xx<img src="http://example.com/example.jpg">xx',
48+
'expected' => 'xxxx',
49+
),
50+
'With alt tag' => array(
51+
'html' => '<img src="http://example.com/example.jpg" alt="An example image">',
52+
'expected' => '',
53+
),
54+
'With alt, and title tags' => array(
55+
'html' => '<img src="http://example.com/example.jpg" alt="An example image" title="Should be ignored">',
56+
'expected' => '',
57+
),
58+
'With alt tag, wrapped in text' => array(
59+
'html' => 'xx<img src="http://example.com/example.jpg" alt="An example image">xx',
60+
'expected' => 'xxxx',
61+
),
62+
'With italics' => array(
63+
'html' => '<img src="shrek.jpg" alt="the ogrelord" /> Blah <i>blah</i> blah',
64+
'expected' => ' Blah _blah_ blah'
65+
)
66+
);
67+
}
68+
3869
/**
39-
* @dataProvider testImageDataProvider
70+
* @dataProvider testImageDataWithAltProvider
4071
*/
41-
public function testImages($html, $expected)
72+
public function testImagesAlt($html, $expected)
4273
{
4374
$html2text = new Html2Text($html);
4475
$output = $html2text->getText();
4576

4677
$this->assertEquals($expected, $output);
4778
}
79+
80+
/**
81+
* @dataProvider testImageDataWithoutAltProvider
82+
*/
83+
public function testImagesNoAlt($html, $expected)
84+
{
85+
$html2text = new Html2Text($html,array('alt_image' => 'hide'));
86+
$output = $html2text->getText();
87+
88+
$this->assertEquals($expected, $output);
89+
}
4890
}

0 commit comments

Comments
 (0)