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 Sep 24, 2024
1 parent 3b443cb commit 81999a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
16 changes: 14 additions & 2 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.

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

private function legacyConstruct($html = '', $fromFile = false, array $options = array())
Expand Down Expand Up @@ -376,6 +377,7 @@ protected function doConvert()

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

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

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

/**
* Helper function called by preg_replace() on link replacement.
*
Expand Down
42 changes: 41 additions & 1 deletion test/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,54 @@ public function imageDataProvider() {
);
}

public function ImageDataWithoutAltProvider() {
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 imageDataProvider
*/
public function testImages($html, $expected)
public function testImagesAlt($html, $expected)
{
$html2text = new Html2Text($html);
$output = $html2text->getText();

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

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

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

0 comments on commit 81999a8

Please sign in to comment.