diff --git a/src/Html2Text.php b/src/Html2Text.php index ffeb377..a40ddd2 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -8,6 +8,7 @@ public static function defaultOptions() { return array( 'ignore_errors' => false, 'drop_links' => false, + 'one_newline_tags' => [] ); } @@ -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": @@ -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": @@ -334,7 +338,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, $output = ""; break; } - + // debug //$output .= "[$name,$nextName]"; @@ -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 @@ -499,7 +503,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, default: // do nothing } - + return $output; } } diff --git a/tests/Html2TextTest.php b/tests/Html2TextTest.php index bf32b1b..230bb99 100644 --- a/tests/Html2TextTest.php +++ b/tests/Html2TextTest.php @@ -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'])); + } } \ No newline at end of file diff --git a/tests/lists-one-newline-ul.html b/tests/lists-one-newline-ul.html new file mode 100644 index 0000000..7189a30 --- /dev/null +++ b/tests/lists-one-newline-ul.html @@ -0,0 +1,24 @@ +
+Add some lists. +
+ +