|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpMarkdown\Tests\Unit\Renderer; |
| 6 | + |
| 7 | +use PhpMarkdown\MarkdownParser; |
| 8 | +use PhpMarkdown\Node\Block\DocumentNode; |
| 9 | +use PhpMarkdown\Node\Block\ParagraphNode; |
| 10 | +use PhpMarkdown\Node\Inline\SoftBreakNode; |
| 11 | +use PhpMarkdown\Renderer\HtmlRenderer; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +final class SoftLineBreakTest extends TestCase |
| 15 | +{ |
| 16 | + private MarkdownParser $parser; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->parser = new MarkdownParser(); |
| 21 | + } |
| 22 | + |
| 23 | + private function parseAndRender(string $markdown): string |
| 24 | + { |
| 25 | + return $this->parser->parse($markdown); |
| 26 | + } |
| 27 | + |
| 28 | + public function testSingleNewlineInParagraphPreservedAsSoftBreak(): void |
| 29 | + { |
| 30 | + $this->assertSame("<p>foo\nbaz</p>\n", $this->parseAndRender("foo\nbaz")); |
| 31 | + } |
| 32 | + |
| 33 | + public function testTrailingSpacesBeforeNewlineStrippedNotHardBreak(): void |
| 34 | + { |
| 35 | + $this->assertSame("<p>foo<br />\nbaz</p>\n", $this->parseAndRender("foo \nbaz")); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSingleTrailingSpaceBeforeNewlineIsSoftBreak(): void |
| 39 | + { |
| 40 | + $this->assertSame("<p>foo\nbaz</p>\n", $this->parseAndRender("foo \nbaz")); |
| 41 | + } |
| 42 | + |
| 43 | + public function testTwoLineParagraphPreservesInternalNewline(): void |
| 44 | + { |
| 45 | + $this->assertSame("<p>aaa\nbbb</p>\n<p>ccc\nddd</p>\n", $this->parseAndRender("aaa\nbbb\n\nccc\nddd")); |
| 46 | + } |
| 47 | + |
| 48 | + public function testLeadingSpacesOnFirstLineStripped(): void |
| 49 | + { |
| 50 | + $this->assertSame("<p>aaa\nbbb</p>\n", $this->parseAndRender(" aaa\nbbb")); |
| 51 | + } |
| 52 | + |
| 53 | + public function testLeadingSpacesOnContinuationLineStripped(): void |
| 54 | + { |
| 55 | + $this->assertSame("<p>aaa\nbbb</p>\n", $this->parseAndRender(" aaa\n bbb")); |
| 56 | + } |
| 57 | + |
| 58 | + public function testEmphasisSpanningSoftLineBreak(): void |
| 59 | + { |
| 60 | + $this->assertSame("<p><em>foo\nbar</em></p>\n", $this->parseAndRender("*foo\nbar*")); |
| 61 | + } |
| 62 | + |
| 63 | + public function testStrongEmphasisSpanningSoftLineBreak(): void |
| 64 | + { |
| 65 | + $this->assertSame("<p><strong>foo\nbar</strong></p>\n", $this->parseAndRender("**foo\nbar**")); |
| 66 | + } |
| 67 | + |
| 68 | + public function testTabBeforeContinuationTextStripped(): void |
| 69 | + { |
| 70 | + $this->assertSame("<p>foo\nbaz</p>\n", $this->parseAndRender("foo\n\tbaz")); |
| 71 | + } |
| 72 | + |
| 73 | + public function testSoftBreakNodeRendersAsNewlineNotSpace(): void |
| 74 | + { |
| 75 | + $renderer = new HtmlRenderer(); |
| 76 | + $document = new DocumentNode([ |
| 77 | + new ParagraphNode([new SoftBreakNode()]), |
| 78 | + ]); |
| 79 | + $this->assertSame("<p>\n</p>\n", $renderer->render($document)); |
| 80 | + } |
| 81 | + |
| 82 | + public function testHardBreakWithTwoTrailingSpacesUnaffectedBySoftBreak(): void |
| 83 | + { |
| 84 | + $this->assertSame("<p>foo<br />\nbar</p>\n", $this->parseAndRender("foo \nbar")); |
| 85 | + } |
| 86 | + |
| 87 | + public function testNoBreakNodeInsertedInSingleLineParagraph(): void |
| 88 | + { |
| 89 | + $this->assertSame("<p>foo bar</p>\n", $this->parseAndRender("foo bar")); |
| 90 | + } |
| 91 | +} |
0 commit comments