mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH Protect against possible unexpected values
This commit is contained in:
parent
557421a245
commit
9a5ccdba51
@ -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 . '"');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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> <del>Some</del> <ins>Other</ins> 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 [
|
||||
|
Loading…
Reference in New Issue
Block a user