diff --git a/src/Psl/Html/encode.php b/src/Psl/Html/encode.php index 0a97b22..3616063 100644 --- a/src/Psl/Html/encode.php +++ b/src/Psl/Html/encode.php @@ -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 diff --git a/src/Psl/Html/encode_special_characters.php b/src/Psl/Html/encode_special_characters.php index 0858b1e..cc10fad 100644 --- a/src/Psl/Html/encode_special_characters.php +++ b/src/Psl/Html/encode_special_characters.php @@ -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 diff --git a/src/Psl/Html/strip_tags.php b/src/Psl/Html/strip_tags.php new file mode 100644 index 0000000..3313875 --- /dev/null +++ b/src/Psl/Html/strip_tags.php @@ -0,0 +1,24 @@ + $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); +} diff --git a/src/Psl/Internal/Loader.php b/src/Psl/Internal/Loader.php index 9ef8c36..155ac93 100644 --- a/src/Psl/Internal/Loader.php +++ b/src/Psl/Internal/Loader.php @@ -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 = [ diff --git a/tests/Psl/Html/StripTagsTest.php b/tests/Psl/Html/StripTagsTest.php new file mode 100644 index 0000000..d62a173 --- /dev/null +++ b/tests/Psl/Html/StripTagsTest.php @@ -0,0 +1,30 @@ +hello

', []]; + yield ['

hello

', '

hello

', ['p']]; + yield ['

hello

', '

hello

', ['p', 'span']]; + yield ['hello, world!', '

hello, world!

', ['span']]; + yield ['

hello, world!

', '

hello, world!

', ['p']]; + yield ['hello, world!', '

hello, world!

', []]; + } +}