diff --git a/src/View/Parsers/HtmlDiff.php b/src/View/Parsers/HtmlDiff.php index d2789ba5c..2ee11e6b1 100644 --- a/src/View/Parsers/HtmlDiff.php +++ b/src/View/Parsers/HtmlDiff.php @@ -2,6 +2,7 @@ namespace SilverStripe\View\Parsers; +use LogicException; use Masterminds\HTML5\Elements; use SebastianBergmann\Diff\Differ; use SilverStripe\Core\Convert; @@ -69,6 +70,9 @@ class HtmlDiff case Differ::REMOVED: $content .= ' ' . $value . ' '; break; + + default: + throw new LogicException('Unexpected type encountered: "' . (string)$type . '"'); } } diff --git a/tests/php/View/Parsers/HtmlDiffTest.php b/tests/php/View/Parsers/HtmlDiffTest.php index 3508c9f3b..b66b1685c 100644 --- a/tests/php/View/Parsers/HtmlDiffTest.php +++ b/tests/php/View/Parsers/HtmlDiffTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\View\Tests\Parsers; +use SebastianBergmann\Diff\Differ; use SilverStripe\Dev\SapphireTest; use SilverStripe\View\Parsers\HtmlDiff; @@ -30,6 +31,42 @@ class HtmlDiffTest extends SapphireTest $this->assertEquals('<span> Some Other text </span> <span> more text </span>', $diff, true); } + /** + * The underlying SebastianBergmann\Diff\Differ class has a special constant for end of line differences + * but we shouldn't ever encounter that because of the way we're modifying the values before passing them + * in to that class. + */ + public function testEndOfLineDoesntNeedHandling() + { + $from = 'some change' . "\r"; + $to = 'some change' . "\n"; + + // If we encounter an end-of-line difference, these should throw exceptions + $this->assertSame('some change', HtmlDiff::compareHtml($from, $to, true)); + $this->assertSame('some change', HtmlDiff::compareHtml($from, $to)); + $this->assertSame('some change', HtmlDiff::compareHtml($to, $from, true)); + $this->assertSame('some change', HtmlDiff::compareHtml($to, $from)); + + // Ensure that this test is valid and that those changes would include an end-of-line warning + // in a direct call to the underlying differ + $differ = new Differ(); + $expected = [ + [ + '#Warning: Strings contain different line endings!' . "\n", + Differ::DIFF_LINE_END_WARNING, + ], + [ + $from, + Differ::REMOVED, + ], + [ + $to, + Differ::ADDED, + ], + ]; + $this->assertSame($expected, $differ->diffToArray($from, $to)); + } + public function provideCompareHtml(): array { return [