From 72feea20decab3895637c79d2d5929832393532a Mon Sep 17 00:00:00 2001 From: Constantin Ross Date: Mon, 21 Nov 2022 22:47:25 +0100 Subject: [PATCH 1/3] fix (#152): remove last new line from rich text blocks --- src/Prismic/Dom/RichText.php | 2 +- tests/Prismic/Dom/RichTextTest.php | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Prismic/Dom/RichText.php b/src/Prismic/Dom/RichText.php index 9df10c27..2663957d 100644 --- a/src/Prismic/Dom/RichText.php +++ b/src/Prismic/Dom/RichText.php @@ -32,7 +32,7 @@ public static function asText($richText) } } - return $result; + return substr($result, 0, -1); } /** diff --git a/tests/Prismic/Dom/RichTextTest.php b/tests/Prismic/Dom/RichTextTest.php index 22cdd50f..3b7035c0 100644 --- a/tests/Prismic/Dom/RichTextTest.php +++ b/tests/Prismic/Dom/RichTextTest.php @@ -20,9 +20,27 @@ protected function setUp(): void public function testAsText() { - $expected = "The title\n"; - $actual = RichText::asText($this->richText->title); - $this->assertEquals($expected, $actual); + $expected = ( + "Heading 1\n". + "Heading 2\n". + "Heading 3\n". + "Heading 4\n". + "Heading 5\n". + "Heading 6\n". + "Paragraph em and strong\n". + "Paragraph Web link and media link\n". + "Preformatted block\n". + "Help\n". + "Revolver\n". + "Abbey Road\n". + "John\n". + "Paul\n". + "George\n". + "Ringo" + ); + + $this->assertEquals($expected, RichText::asText($this->richText->description)); + $this->assertEquals($expected, RichText::asText($this->richText->description, $this->linkResolver)); } public function testAsHtml() From 6a6914cce680dbdb24b6fd4be00c256602e999c7 Mon Sep 17 00:00:00 2001 From: Constantin Ross Date: Tue, 22 Nov 2022 11:05:49 +0100 Subject: [PATCH 2/3] fix (#152): use trim instead of substr to remove trailing new line --- src/Prismic/Dom/RichText.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Prismic/Dom/RichText.php b/src/Prismic/Dom/RichText.php index 2663957d..da903973 100644 --- a/src/Prismic/Dom/RichText.php +++ b/src/Prismic/Dom/RichText.php @@ -32,7 +32,7 @@ public static function asText($richText) } } - return substr($result, 0, -1); + return trim($result); } /** From ecbffbf79f75c62867c2943dd3004ac2cf4adbfe Mon Sep 17 00:00:00 2001 From: Constantin Ross Date: Tue, 22 Nov 2022 11:08:20 +0100 Subject: [PATCH 3/3] test: add new richtext astext assertion --- tests/Prismic/Dom/RichTextTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Prismic/Dom/RichTextTest.php b/tests/Prismic/Dom/RichTextTest.php index 3b7035c0..a18a9df7 100644 --- a/tests/Prismic/Dom/RichTextTest.php +++ b/tests/Prismic/Dom/RichTextTest.php @@ -41,6 +41,8 @@ public function testAsText() $this->assertEquals($expected, RichText::asText($this->richText->description)); $this->assertEquals($expected, RichText::asText($this->richText->description, $this->linkResolver)); + + $this->assertStringEndsNotWith("\n", RichText::asText($this->richText->description)); } public function testAsHtml()