From f0b207d0c2d47dbb30aac5dd7335b74f40a5a404 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 8 Oct 2015 10:36:28 +0800 Subject: [PATCH] Supply the image alt text when converting images In order to provide an accessible version of HTML content, where an alt tag has been provided, this should be used. Where text refers to an image inline (for example, an emoticon), complete removal of that image can confuse the meaning of the text. As per WCAG 2.0 guidelines (http://www.w3.org/TR/WCAG20-TECHS/H37.html), images which convey words should be provided with alt text which inclues those words. This also relates to images which convey meaning in some other fashion. We have been using this modification to Html2Text since August 2010. --- src/Html2Text.php | 4 +++- test/ImageTest.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/ImageTest.php diff --git a/src/Html2Text.php b/src/Html2Text.php index 0dfb8d4..5ead1ff 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -67,6 +67,7 @@ class Html2Text '/(]*>|<\/tr>)/i', // and '/]*>(.*?)<\/td>/i', // and '/.+?<\/span>/i', // ... + '/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt tag ); /** @@ -97,7 +98,8 @@ class Html2Text "\n\n", // and
"\n", // and "\t\t\\1\n", // and - "" // ... + "", // ... + '[\\2]', // with alt tag ); /** diff --git a/test/ImageTest.php b/test/ImageTest.php new file mode 100644 index 0000000..04a3aa6 --- /dev/null +++ b/test/ImageTest.php @@ -0,0 +1,42 @@ + array( + 'html' => '', + 'expected' => '', + ), + 'Without alt tag, wrapped in text' => array( + 'html' => 'xxxx', + 'expected' => 'xxxx', + ), + 'With alt tag' => array( + 'html' => 'An example image', + 'expected' => '[An example image]', + ), + 'With alt, and title tags' => array( + 'html' => 'An example image', + 'expected' => '[An example image]', + ), + 'With alt tag, wrapped in text' => array( + 'html' => 'xxAn example imagexx', + 'expected' => 'xx[An example image]xx', + ), + ); + } + + /** + * @dataProvider testImageDataProvider + */ + public function testImages($html, $expected) + { + $html2text = new Html2Text($html); + $output = $html2text->getText(); + + $this->assertEquals($expected, $output); + } +}