Skip to content

Commit

Permalink
feat(html): use Str\Encoding instead of string|null for $encoding arg…
Browse files Browse the repository at this point in the history
…ument

Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 3, 2021
1 parent 6ce6f6f commit 8e06239
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,3 +28,4 @@
* introduced a new `IO\write_line()` function.
* introduced a new `IO\write_error()` function.
* introduced a new `IO\write_error_line()` functions.
* **BC** - `$encoding` argument for `Psl\Html` functions now accepts `Psl\Str\Encoding` instead of `?string`.
10 changes: 4 additions & 6 deletions src/Psl/Html/decode.php
Expand Up @@ -5,7 +5,7 @@
namespace Psl\Html;

use Psl\Exception;
use Psl\Internal;
use Psl\Str;

use function html_entity_decode;

Expand All @@ -14,15 +14,13 @@
/**
* Convert HTML entities to their corresponding characters.
*
* @param string $encoding defines character set used in conversion.
* @param Str\Encoding $encoding defines character set used in conversion.
*
* @throws Exception\InvariantViolationException If $encoding is invalid.
*
* @pure
*/
function decode(string $html, ?string $encoding = null): string
function decode(string $html, Str\Encoding $encoding = Str\Encoding::UTF_8): string
{
$encoding = Internal\internal_encoding($encoding);

return html_entity_decode($html, ENT_QUOTES, $encoding);
return html_entity_decode($html, ENT_QUOTES, $encoding->value);
}
10 changes: 4 additions & 6 deletions src/Psl/Html/encode.php
Expand Up @@ -5,7 +5,7 @@
namespace Psl\Html;

use Psl\Exception;
use Psl\Internal;
use Psl\Str;

use function htmlentities;

Expand All @@ -16,17 +16,15 @@
*
* @param bool $double_encoding If set to false, this function will not
* encode existing html entities.
* @param string $encoding defines character set used in conversion.
* @param Str\Encoding $encoding defines character set used in conversion.
*
* @throws Exception\InvariantViolationException If $encoding is invalid.
*
* @psalm-taint-escape html
*
* @pure
*/
function encode(string $html, bool $double_encoding = true, ?string $encoding = null): string
function encode(string $html, bool $double_encoding = true, Str\Encoding $encoding = Str\Encoding::UTF_8): string
{
$encoding = Internal\internal_encoding($encoding);

return htmlentities($html, ENT_QUOTES, $encoding, $double_encoding);
return htmlentities($html, ENT_QUOTES, $encoding->value, $double_encoding);
}
10 changes: 4 additions & 6 deletions src/Psl/Html/encode_special_characters.php
Expand Up @@ -5,7 +5,7 @@
namespace Psl\Html;

use Psl\Exception;
use Psl\Internal;
use Psl\Str;

use function htmlspecialchars;

Expand All @@ -18,17 +18,15 @@
*
* @param bool $double_encoding If set to false, this function will not
* encode existing html entities.
* @param string $encoding defines character set used in conversion.
* @param Str\Encoding $encoding defines character set used in conversion.
*
* @throws Exception\InvariantViolationException If $encoding is invalid.
*
* @psalm-taint-escape html
*
* @pure
*/
function encode_special_characters(string $html, bool $double_encoding = true, ?string $encoding = null): string
function encode_special_characters(string $html, bool $double_encoding = true, Str\Encoding $encoding = Str\Encoding::UTF_8): string
{
$encoding = Internal\internal_encoding($encoding);

return htmlspecialchars($html, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, $encoding, $double_encoding);
return htmlspecialchars($html, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, $encoding->value, $double_encoding);
}
23 changes: 12 additions & 11 deletions tests/unit/Html/DecodeTest.php
Expand Up @@ -6,29 +6,30 @@

use PHPUnit\Framework\TestCase;
use Psl\Html;
use Psl\Str;

final class DecodeTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testEncode(string $expected, string $html, ?string $encoding): void
public function testEncode(string $expected, string $html, Str\Encoding $encoding): void
{
static::assertSame($expected, Html\decode($html, $encoding));
}

public function provideData(): iterable
{
yield ['hello', 'hello', 'UTF-8'];
yield ['héllo', 'h&eacute;llo', 'UTF-8'];
yield ['<hello />', '&lt;hello /&gt;', 'UTF-8'];
yield ['<p>Hello</p>', '&lt;p&gt;Hello&lt;/p&gt;', 'UTF-8'];
yield ['<p>&lt; </p>', '&lt;p&gt;&amp;lt; &lt;/p&gt;', 'UTF-8'];
yield ['hello', 'hello', Str\Encoding::UTF_8];
yield ['héllo', 'h&eacute;llo', Str\Encoding::UTF_8];
yield ['<hello />', '&lt;hello /&gt;', Str\Encoding::UTF_8];
yield ['<p>Hello</p>', '&lt;p&gt;Hello&lt;/p&gt;', Str\Encoding::UTF_8];
yield ['<p>&lt; </p>', '&lt;p&gt;&amp;lt; &lt;/p&gt;', Str\Encoding::UTF_8];

yield ['hello', 'hello', 'UTF-8'];
yield ['héllo', 'h&eacute;llo', 'UTF-8'];
yield ['<hello />', '&lt;hello /&gt;', 'UTF-8'];
yield ['<p>Hello</p>', '&lt;p&gt;Hello&lt;/p&gt;', 'UTF-8'];
yield ['<p>< </p>', '&lt;p&gt;&lt; &lt;/p&gt;', 'UTF-8'];
yield ['hello', 'hello', Str\Encoding::UTF_8];
yield ['héllo', 'h&eacute;llo', Str\Encoding::UTF_8];
yield ['<hello />', '&lt;hello /&gt;', Str\Encoding::UTF_8];
yield ['<p>Hello</p>', '&lt;p&gt;Hello&lt;/p&gt;', Str\Encoding::UTF_8];
yield ['<p>< </p>', '&lt;p&gt;&lt; &lt;/p&gt;', Str\Encoding::UTF_8];
}
}
23 changes: 12 additions & 11 deletions tests/unit/Html/EncodeSpecialCharactersTest.php
Expand Up @@ -6,29 +6,30 @@

use PHPUnit\Framework\TestCase;
use Psl\Html;
use Psl\Str;

final class EncodeSpecialCharactersTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testEncode(string $expected, string $html, bool $double_encoding, ?string $encoding): void
public function testEncode(string $expected, string $html, bool $double_encoding, Str\Encoding $encoding): void
{
static::assertSame($expected, Html\encode_special_characters($html, $double_encoding, $encoding));
}

public function provideData(): iterable
{
yield ['hello', 'hello', true, 'UTF-8'];
yield ['héllo', 'héllo', true, 'UTF-8'];
yield ['&lt;hello /&gt;', '<hello />', true, 'UTF-8'];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', true, 'UTF-8'];
yield ['&lt;p&gt;&amp;lt; &lt;/p&gt;', '<p>&lt; </p>', true, 'UTF-8'];
yield ['hello', 'hello', true, Str\Encoding::UTF_8];
yield ['héllo', 'héllo', true, Str\Encoding::UTF_8];
yield ['&lt;hello /&gt;', '<hello />', true, Str\Encoding::UTF_8];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', true, Str\Encoding::UTF_8];
yield ['&lt;p&gt;&amp;lt; &lt;/p&gt;', '<p>&lt; </p>', true, Str\Encoding::UTF_8];

yield ['hello', 'hello', false, 'UTF-8'];
yield ['héllo', 'héllo', false, 'UTF-8'];
yield ['&lt;hello /&gt;', '<hello />', false, 'UTF-8'];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', false, 'UTF-8'];
yield ['&lt;p&gt;&lt; &lt;/p&gt;', '<p>&lt; </p>', false, 'UTF-8'];
yield ['hello', 'hello', false, Str\Encoding::UTF_8];
yield ['héllo', 'héllo', false, Str\Encoding::UTF_8];
yield ['&lt;hello /&gt;', '<hello />', false, Str\Encoding::UTF_8];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', false, Str\Encoding::UTF_8];
yield ['&lt;p&gt;&lt; &lt;/p&gt;', '<p>&lt; </p>', false, Str\Encoding::UTF_8];
}
}
23 changes: 12 additions & 11 deletions tests/unit/Html/EncodeTest.php
Expand Up @@ -6,29 +6,30 @@

use PHPUnit\Framework\TestCase;
use Psl\Html;
use Psl\Str;

final class EncodeTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testEncode(string $expected, string $html, bool $double_encoding, ?string $encoding): void
public function testEncode(string $expected, string $html, bool $double_encoding, Str\Encoding $encoding): void
{
static::assertSame($expected, Html\encode($html, $double_encoding, $encoding));
}

public function provideData(): iterable
{
yield ['hello', 'hello', true, 'UTF-8'];
yield ['h&eacute;llo', 'héllo', true, 'UTF-8'];
yield ['&lt;hello /&gt;', '<hello />', true, 'UTF-8'];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', true, 'UTF-8'];
yield ['&lt;p&gt;&amp;lt; &lt;/p&gt;', '<p>&lt; </p>', true, 'UTF-8'];
yield ['hello', 'hello', true, Str\Encoding::UTF_8];
yield ['h&eacute;llo', 'héllo', true, Str\Encoding::UTF_8];
yield ['&lt;hello /&gt;', '<hello />', true, Str\Encoding::UTF_8];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', true, Str\Encoding::UTF_8];
yield ['&lt;p&gt;&amp;lt; &lt;/p&gt;', '<p>&lt; </p>', true, Str\Encoding::UTF_8];

yield ['hello', 'hello', false, 'UTF-8'];
yield ['h&eacute;llo', 'héllo', false, 'UTF-8'];
yield ['&lt;hello /&gt;', '<hello />', false, 'UTF-8'];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', false, 'UTF-8'];
yield ['&lt;p&gt;&lt; &lt;/p&gt;', '<p>&lt; </p>', false, 'UTF-8'];
yield ['hello', 'hello', false, Str\Encoding::UTF_8];
yield ['h&eacute;llo', 'héllo', false, Str\Encoding::UTF_8];
yield ['&lt;hello /&gt;', '<hello />', false, Str\Encoding::UTF_8];
yield ['&lt;p&gt;Hello&lt;/p&gt;', '<p>Hello</p>', false, Str\Encoding::UTF_8];
yield ['&lt;p&gt;&lt; &lt;/p&gt;', '<p>&lt; </p>', false, Str\Encoding::UTF_8];
}
}

0 comments on commit 8e06239

Please sign in to comment.