Skip to content

Commit

Permalink
add option to hide alt text
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza221 committed Mar 16, 2023
1 parent b0fb318 commit a8cb26a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class Html2Text
'/(<tr\b[^>]*>|<\/tr>)/i', // <tr> and </tr>
'/<td\b[^>]*>(.*?)<\/td>/i', // <td> and </td>
'/<span class="_html2text_ignore">.+?<\/span>/i', // <span class="_html2text_ignore">...</span>
'/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i', // <img> with alt tag
);

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

/**
Expand Down Expand Up @@ -222,6 +220,9 @@ class Html2Text
'width' => 70, // Maximum width of the formatted text, in columns.
// Set this value to 0 (or less) to ignore word wrapping
// and not constrain text to a fixed-width column.

'alt_image'=>'show', // 'show' (show the alt text in brackets)
// 'hide' (don't show the alt text)
);

private function legacyConstruct($html = '', $fromFile = false, array $options = array())
Expand Down Expand Up @@ -368,7 +369,7 @@ protected function doConvert()
}

protected function converter(&$text)
{
{ $this->convertImages();
$this->convertBlockquotes($text);
$this->convertPre($text);
$text = preg_replace($this->search, $this->replace, $text);
Expand Down Expand Up @@ -396,6 +397,16 @@ protected function converter(&$text)
}
}

/**
* show/hide alt text for images
*/

protected function convertImages(){

$this->search[] = '/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i';
$this->replace[] = $this->options['alt_image'] == 'show'?'[\\2]':'';
}

/**
* Helper function called by preg_replace() on link replacement.
*
Expand Down
48 changes: 45 additions & 3 deletions test/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class ImageTest extends TestCase
{
public function testImageDataProvider() {
public function testImageDataWithAltProvider() {
$this->expectNotToPerformAssertions();
return array(
'Without alt tag' => array(
'html' => '<img src="http://example.com/example.jpg">',
Expand Down Expand Up @@ -35,14 +36,55 @@ public function testImageDataProvider() {
);
}

public function testImageDataWithoutAltProvider() {
$this->expectNotToPerformAssertions();
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' => '',
),
'With alt, and title tags' => array(
'html' => '<img src="http://example.com/example.jpg" alt="An example image" title="Should be ignored">',
'expected' => '',
),
'With alt tag, wrapped in text' => array(
'html' => 'xx<img src="http://example.com/example.jpg" alt="An example image">xx',
'expected' => 'xxxx',
),
'With italics' => array(
'html' => '<img src="shrek.jpg" alt="the ogrelord" /> Blah <i>blah</i> blah',
'expected' => ' Blah _blah_ blah'
)
);
}

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

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

/**
* @dataProvider testImageDataWithoutAltProvider
*/
public function testImagesNoAlt($html, $expected)
{
$html2text = new Html2Text($html,array('alt_image' => 'hide'));
$output = $html2text->getText();

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

0 comments on commit a8cb26a

Please sign in to comment.