diff --git a/src/AlternativeText.php b/src/AlternativeText.php index 772f33e..b56e314 100644 --- a/src/AlternativeText.php +++ b/src/AlternativeText.php @@ -12,6 +12,11 @@ final class AlternativeText */ private $text; + /** + * @var bool + */ + private $shouldNormalizeSpace = true; + /** * @param string $text */ @@ -41,7 +46,11 @@ public function getRaw(): string */ public function __toString(): string { - return $this->normalizeSpace($this->text); + if ($this->shouldNormalizeSpace) { + return $this->normalizeSpace($this->text); + } + + return $this->text; } /** @@ -85,6 +94,17 @@ public static function fromEncodedText(string $text, string $charset): Alternati return new self($converted); } + /** + * @param string $text + * @return AlternativeText + */ + public static function fromRawString(string $text): AlternativeText + { + $self = new self($text); + $self->shouldNormalizeSpace = false; + return $self; + } + /** * @param string $html * @return AlternativeText diff --git a/test/Unit/AlternativeTextTest.php b/test/Unit/AlternativeTextTest.php index c4616e1..2a9883b 100644 --- a/test/Unit/AlternativeTextTest.php +++ b/test/Unit/AlternativeTextTest.php @@ -33,6 +33,16 @@ public function it_strips_tags_on_dom_exception(): void $this->assertEquals($text, (string) $alternativeText); } + /** + * @test + */ + public function it_does_not_normalize_space_for_raw_strings(): void + { + $text = ' test '; + $alternativeText = AlternativeText::fromRawString($text); + $this->assertEquals($text, (string) $alternativeText); + } + /** * @return array */