From a8cb26ad3f2e19d0501bbbbbb3ad5165fe18d4d6 Mon Sep 17 00:00:00 2001 From: hamza221 Date: Thu, 16 Mar 2023 12:43:27 +0100 Subject: [PATCH] add option to hide alt text --- src/Html2Text.php | 17 +++++++++++++--- test/ImageTest.php | 48 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Html2Text.php b/src/Html2Text.php index 16a6572..120c6f0 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -68,7 +68,6 @@ class Html2Text '/(]*>|<\/tr>)/i', // and '/]*>(.*?)<\/td>/i', // and '/.+?<\/span>/i', // ... - '/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt tag ); /** @@ -99,7 +98,6 @@ class Html2Text "\n", // and "\t\t\\1\n", // and "", // ... - '[\\2]', // with alt tag ); /** @@ -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()) @@ -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); @@ -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. * diff --git a/test/ImageTest.php b/test/ImageTest.php index a981d03..a7e015f 100644 --- a/test/ImageTest.php +++ b/test/ImageTest.php @@ -6,7 +6,8 @@ class ImageTest extends TestCase { - public function testImageDataProvider() { + public function testImageDataWithAltProvider() { + $this->expectNotToPerformAssertions(); return array( 'Without alt tag' => array( 'html' => '', @@ -35,14 +36,55 @@ public function testImageDataProvider() { ); } + public function testImageDataWithoutAltProvider() { + $this->expectNotToPerformAssertions(); + return array( + 'Without alt tag' => array( + 'html' => '', + 'expected' => '', + ), + 'Without alt tag, wrapped in text' => array( + 'html' => 'xxxx', + 'expected' => 'xxxx', + ), + 'With alt tag' => array( + 'html' => 'An example image', + 'expected' => '', + ), + 'With alt, and title tags' => array( + 'html' => 'An example image', + 'expected' => '', + ), + 'With alt tag, wrapped in text' => array( + 'html' => 'xxAn example imagexx', + 'expected' => 'xxxx', + ), + 'With italics' => array( + 'html' => 'the ogrelord Blah blah 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); + } }