-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow disallowed raw HTML tags to be configurable (#507)
- Loading branch information
1 parent
6fb7958
commit 212a668
Showing
5 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/unit/Extension/DisallowedRawHtml/DisallowedRawHtmlRendererTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the league/commonmark package. | ||
* | ||
* (c) Colin O'Dell <colinodell@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\CommonMark\Tests\Unit\Extension\DisallowedRawHtml; | ||
|
||
use League\CommonMark\Configuration\Configuration; | ||
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlRenderer; | ||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DisallowedRawHtmlRendererTest extends TestCase | ||
{ | ||
public function testWithEmptyHtml(): void | ||
{ | ||
$mockRenderer = $this->createMock(NodeRendererInterface::class); | ||
$mockRenderer->method('render')->willReturn(''); | ||
|
||
$renderer = new DisallowedRawHtmlRenderer($mockRenderer); | ||
|
||
$this->assertSame('', $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer())); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTestWithDefaultSettings | ||
*/ | ||
public function testWithDefaultSettings(string $input, string $expectedOutput): void | ||
{ | ||
$mockRenderer = $this->createMock(NodeRendererInterface::class); | ||
$mockRenderer->method('render')->willReturn($input); | ||
|
||
$renderer = new DisallowedRawHtmlRenderer($mockRenderer); | ||
$renderer->setConfiguration(new Configuration()); | ||
|
||
$this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer())); | ||
} | ||
|
||
/** | ||
* @return iterable<mixed> | ||
*/ | ||
public function dataProviderForTestWithDefaultSettings(): iterable | ||
{ | ||
// Different tag variants | ||
yield ['<title>', '<title>']; | ||
yield ['</title>', '</title>']; | ||
yield ['<title x="sdf">', '<title x="sdf">']; | ||
yield ['<title/>', '<title/>']; | ||
yield ['<title />', '<title />']; | ||
|
||
// Other tags escaped by default | ||
yield ['<textarea>', '<textarea>']; | ||
yield ['<style>', '<style>']; | ||
yield ['<xmp>', '<xmp>']; | ||
yield ['<iframe>', '<iframe>']; | ||
yield ['<noembed>', '<noembed>']; | ||
yield ['<noframes>', '<noframes>']; | ||
yield ['<script>', '<script>']; | ||
yield ['<plaintext>', '<plaintext>']; | ||
|
||
// Tags not escaped by default | ||
yield ['<strong>', '<strong>']; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForTestWithCustomSettings | ||
*/ | ||
public function testWithCustomSettings(string $input, string $expectedOutput): void | ||
{ | ||
$mockRenderer = $this->createMock(NodeRendererInterface::class); | ||
$mockRenderer->method('render')->willReturn($input); | ||
|
||
$renderer = new DisallowedRawHtmlRenderer($mockRenderer); | ||
$renderer->setConfiguration(new Configuration([ | ||
'disallowed_raw_html' => [ | ||
'disallowed_tags' => [ | ||
'strong', | ||
], | ||
], | ||
])); | ||
|
||
$this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer())); | ||
} | ||
|
||
/** | ||
* @return iterable<mixed> | ||
*/ | ||
public function dataProviderForTestWithCustomSettings(): iterable | ||
{ | ||
// Tags that I've configured to escape | ||
yield ['<strong>', '<strong>']; | ||
yield ['</strong>', '</strong>']; | ||
yield ['<strong x="sdf">', '<strong x="sdf">']; | ||
yield ['<strong/>', '<strong/>']; | ||
yield ['<strong />', '<strong />']; | ||
|
||
// Defaults that I didn't include in my custom config | ||
yield ['<title>', '<title>']; | ||
yield ['<textarea>', '<textarea>']; | ||
yield ['<style>', '<style>']; | ||
yield ['<xmp>', '<xmp>']; | ||
yield ['<iframe>', '<iframe>']; | ||
yield ['<noembed>', '<noembed>']; | ||
yield ['<noframes>', '<noframes>']; | ||
yield ['<script>', '<script>']; | ||
yield ['<plaintext>', '<plaintext>']; | ||
} | ||
} |