Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ protected function doConvert()
$text = trim($this->html);
}

// Inline data URIs (e.g. base64 images) never end up in the text, and very
// long ones exhaust the PCRE backtrack limit of the patterns below (#130)
$text = preg_replace('/(<img\b[^>]*?\ssrc\s*=\s*)(["\'])data:[^"\']*+\2/i', '$1$2$2', $text);

$this->converter($text);

if ($this->linkList) {
Expand Down
40 changes: 40 additions & 0 deletions test/DataUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Html2Text;

use PHPUnit\Framework\TestCase;

/**
* Large inline data URIs must not exhaust the PCRE backtrack limit (#130).
*/
class DataUriTest extends TestCase
{
public function dataUriDataProvider()
{
$image = 'data:image/png;base64,' . str_repeat('QUJDREVGR0g', 120000);

return array(
'Image in list item' => array(
'html' => '<ul><li>Item <img src="' . $image . '"></li></ul>',
'expected' => '* Item',
),
'Image in link' => array(
'html' => '<a href="https://example.com"><img src="' . $image . '"></a> text',
'expected' => '[https://example.com] text',
),
'Image with alt tag in paragraph' => array(
'html' => '<p>Hello <img src="' . $image . '" alt="logo"> world</p>',
'expected' => 'Hello [logo] world',
),
);
}

/**
* @dataProvider dataUriDataProvider
*/
public function testDataUri($html, $expected)
{
$html2text = new Html2Text($html);
$this->assertEquals($expected, trim($html2text->getText()));
}
}