Skip to content

Commit

Permalink
Merge pull request #62 from cameron1729/br_in_pre
Browse files Browse the repository at this point in the history
Fix whitespace problem with pre tags containing brs
  • Loading branch information
mtibben committed Mar 16, 2016
2 parents 2712226 + 1bc583d commit f55104b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Html2Text.php
Expand Up @@ -436,7 +436,8 @@ protected function convertPre(&$text)
{
// get the content of PRE element
while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
$this->preContent = $matches[1];
// Replace br tags with newlines to prevent the search-and-replace callback from killing whitespace
$this->preContent = preg_replace('/(<br\b[^>]*>)/i', "\n", $matches[1]);

// Run our defined tags search-and-replace with callback
$this->preContent = preg_replace_callback(
Expand Down
42 changes: 36 additions & 6 deletions test/PreTest.php
Expand Up @@ -4,9 +4,11 @@

class PreTest extends \PHPUnit_Framework_TestCase
{
public function testPre()
public function preDataProvider()
{
$html =<<<'EOT'
return array(
'Basic pre' => array(
'html' => <<<EOT
<p>Before</p>
<pre>
Expand All @@ -17,9 +19,9 @@ public function testPre()
</pre>
<p>After</p>
EOT;

$expected =<<<'EOT'
EOT
,
'expected' => <<<EOT
Before
Foo bar baz
Expand All @@ -28,8 +30,36 @@ public function testPre()
After
EOT;
EOT
,
),
'br in pre' => array(
'html' => <<<EOT
<pre>
some<br /> indented<br /> text<br /> on<br /> several<br /> lines<br />
</pre>
EOT
,
'expected' => <<<EOT
some
  indented
  text
    on
    several
  lines
EOT
,
),
);
}

/**
* @dataProvider preDataProvider
*/
public function testPre($html, $expected)
{
$html2text = new Html2Text($html);
$this->assertEquals($expected, $html2text->getText());
}
Expand Down

0 comments on commit f55104b

Please sign in to comment.