Skip to content

Commit f55104b

Browse files
committed
Merge pull request #62 from cameron1729/br_in_pre
Fix whitespace problem with pre tags containing brs
2 parents 2712226 + 1bc583d commit f55104b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Html2Text.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ protected function convertPre(&$text)
436436
{
437437
// get the content of PRE element
438438
while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
439-
$this->preContent = $matches[1];
439+
// Replace br tags with newlines to prevent the search-and-replace callback from killing whitespace
440+
$this->preContent = preg_replace('/(<br\b[^>]*>)/i', "\n", $matches[1]);
440441

441442
// Run our defined tags search-and-replace with callback
442443
$this->preContent = preg_replace_callback(

test/PreTest.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
class PreTest extends \PHPUnit_Framework_TestCase
66
{
7-
public function testPre()
7+
public function preDataProvider()
88
{
9-
$html =<<<'EOT'
9+
return array(
10+
'Basic pre' => array(
11+
'html' => <<<EOT
1012
<p>Before</p>
1113
<pre>
1214
@@ -17,9 +19,9 @@ public function testPre()
1719
1820
</pre>
1921
<p>After</p>
20-
EOT;
21-
22-
$expected =<<<'EOT'
22+
EOT
23+
,
24+
'expected' => <<<EOT
2325
Before
2426
2527
Foo bar baz
@@ -28,8 +30,36 @@ public function testPre()
2830
2931
After
3032
31-
EOT;
33+
EOT
34+
,
35+
),
36+
'br in pre' => array(
37+
'html' => <<<EOT
38+
<pre>
39+
some<br /> indented<br /> text<br /> on<br /> several<br /> lines<br />
40+
</pre>
41+
EOT
42+
,
43+
'expected' => <<<EOT
44+
some
45+
  indented
46+
  text
47+
    on
48+
    several
49+
  lines
50+
3251
52+
EOT
53+
,
54+
),
55+
);
56+
}
57+
58+
/**
59+
* @dataProvider preDataProvider
60+
*/
61+
public function testPre($html, $expected)
62+
{
3363
$html2text = new Html2Text($html);
3464
$this->assertEquals($expected, $html2text->getText());
3565
}

0 commit comments

Comments
 (0)