From 81999a8869af275f2533d16bec47e817758f344d 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 | 16 ++++++++++++++-- test/ImageTest.php | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/Html2Text.php b/src/Html2Text.php index 6e0f9e5..c7329d4 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. + + '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()) @@ -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); @@ -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. * diff --git a/test/ImageTest.php b/test/ImageTest.php index afe439a..9cd2125 100644 --- a/test/ImageTest.php +++ b/test/ImageTest.php @@ -35,14 +35,54 @@ public function imageDataProvider() { ); } + public function ImageDataWithoutAltProvider() { + 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 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); + } }