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
16 changes: 10 additions & 6 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static function defaultOptions() {
return array(
'ignore_errors' => false,
'drop_links' => false,
'one_newline_tags' => []
);
}

Expand Down Expand Up @@ -258,7 +259,6 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document,

$name = strtolower($node->nodeName);
$nextName = static::nextChildName($node);

// start whitespace
switch ($name) {
case "hr":
Expand All @@ -285,8 +285,12 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document,
case "ol":
case "ul":
case "pre":
// add two newlines
$output = "\n\n";
if(in_array($name, $options['one_newline_tags'])){
$output = "\n";
}else{
// add two newlines
$output = "\n\n";
}
break;

case "td":
Expand Down Expand Up @@ -334,7 +338,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document,
$output = "";
break;
}

// debug
//$output .= "[$name,$nextName]";

Expand All @@ -350,7 +354,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document,
while ($n != null) {

$text = static::iterateOverNode($n, $previousSiblingName, $in_pre || $name == 'pre', $is_office_document, $options);

// Pass current node name to next child, as previousSibling does not appear to get populated
if ($n instanceof \DOMDocumentType
|| $n instanceof \DOMProcessingInstruction
Expand Down Expand Up @@ -499,7 +503,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document,
default:
// do nothing
}

return $output;
}
}
4 changes: 4 additions & 0 deletions tests/Html2TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,9 @@ function testBasicDropLinks() {
function testAnchorsDropLinks() {
$this->doTestWithResults("anchors", "anchors.no-links", array('drop_links' => true));
}

function testOneNewlineTagsOption() {
$this->doTestWithResults("lists-one-newline-ul", "lists-one-newline-ul", array('one_newline_tags' => ['ul']));
}

}
24 changes: 24 additions & 0 deletions tests/lists-one-newline-ul.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>List tests</h1>

<p>
Add some lists.
</p>

<ol>
<li>one</li>
<li>two
<li>three
</ol>

<div>An unordered list</div>

<ul>
<li>one
<li>two</li>
<li>three</li>
</ul>
<ul>
<li>one
<li>two</li>
<li>three</li>
</ul>
16 changes: 16 additions & 0 deletions tests/lists-one-newline-ul.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
List tests

Add some lists.

- one
- two
- three

An unordered list
- one
- two
- three

- one
- two
- three