ENH Protect against possible unexpected values

This commit is contained in:
Guy Sartorelli 2023-02-08 16:15:32 +13:00
parent 557421a245
commit 9a5ccdba51
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
2 changed files with 41 additions and 0 deletions

View File

@ -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 .= ' <del>' . $value . '</del> ';
break;
default:
throw new LogicException('Unexpected type encountered: "' . (string)$type . '"');
}
}

View File

@ -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('&lt;span&gt; <del>Some</del> <ins>Other</ins> text &lt;/span&gt; &lt;span&gt; more text &lt;/span&gt;', $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 [