mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2024-11-26 12:25:03 +01:00
[Html] add strip_tags function
This commit is contained in:
parent
347747f720
commit
f32efe4597
@ -20,6 +20,8 @@ use const ENT_QUOTES;
|
||||
*
|
||||
* @throws Exception\InvariantViolationException If $encoding is invalid.
|
||||
*
|
||||
* @psalm-taint-escape html
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
function encode(string $html, bool $double_encoding = true, ?string $encoding = null): string
|
||||
|
@ -22,6 +22,8 @@ use const ENT_SUBSTITUTE;
|
||||
*
|
||||
* @throws Exception\InvariantViolationException If $encoding is invalid.
|
||||
*
|
||||
* @psalm-taint-escape html
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
function encode_special_characters(string $html, bool $double_encoding = true, ?string $encoding = null): string
|
||||
|
24
src/Psl/Html/strip_tags.php
Normal file
24
src/Psl/Html/strip_tags.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Html;
|
||||
|
||||
use function strip_tags as php_strip_tags;
|
||||
|
||||
/**
|
||||
* Strip HTML and PHP tags from a string.
|
||||
*
|
||||
* @param list<string> $allowed_tags tags which should not be stripped.
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
function strip_tags(string $html, array $allowed_tags = []): string
|
||||
{
|
||||
/**
|
||||
* @psalm-suppress InvalidArgument
|
||||
*
|
||||
* @link https://github.com/vimeo/psalm/issues/5330
|
||||
*/
|
||||
return php_strip_tags($html, $allowed_tags);
|
||||
}
|
@ -464,6 +464,7 @@ final class Loader
|
||||
'Psl\Html\encode_special_characters',
|
||||
'Psl\Html\decode',
|
||||
'Psl\Html\decode_special_characters',
|
||||
'Psl\Html\strip_tags',
|
||||
];
|
||||
|
||||
public const INTERFACES = [
|
||||
|
30
tests/Psl/Html/StripTagsTest.php
Normal file
30
tests/Psl/Html/StripTagsTest.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Html;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Html;
|
||||
|
||||
final class StripTagsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testEncode(string $expected, string $html, array $allowed_tags): void
|
||||
{
|
||||
static::assertSame($expected, Html\strip_tags($html, $allowed_tags));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield ['hello', 'hello', []];
|
||||
yield ['hello', '<p>hello</p>', []];
|
||||
yield ['<p>hello</p>', '<p>hello</p>', ['p']];
|
||||
yield ['<p>hello</p>', '<p>hello</p>', ['p', 'span']];
|
||||
yield ['hello, <span>world!</span>', '<p>hello, <span>world!</span></p>', ['span']];
|
||||
yield ['<p>hello, world!</p>', '<p>hello, <span>world!</span></p>', ['p']];
|
||||
yield ['hello, world!', '<p>hello, <span>world!</span></p>', []];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user