-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Html; | ||
|
||
use Psl\Exception; | ||
use Psl\Internal; | ||
|
||
use function html_entity_decode; | ||
|
||
use const ENT_QUOTES; | ||
|
||
/** | ||
* Convert HTML entities to their corresponding characters. | ||
* | ||
* @param string $encoding defines character set used in conversion. | ||
* | ||
* @throws Exception\InvariantViolationException If $encoding is invalid. | ||
* | ||
* @psalm-pure | ||
*/ | ||
function decode(string $html, ?string $encoding = null): string | ||
{ | ||
$encoding = Internal\internal_encoding($encoding); | ||
|
||
return html_entity_decode($html, ENT_QUOTES, $encoding); | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Html; | ||
|
||
use function htmlspecialchars_decode; | ||
|
||
use const ENT_HTML5; | ||
use const ENT_QUOTES; | ||
use const ENT_SUBSTITUTE; | ||
|
||
/** | ||
* Convert special HTML entities back to characters. | ||
* | ||
* @psalm-pure | ||
*/ | ||
function decode_special_characters(string $html): string | ||
{ | ||
return htmlspecialchars_decode($html, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE); | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Html; | ||
|
||
use Psl\Exception; | ||
use Psl\Internal; | ||
|
||
use function htmlentities; | ||
|
||
use const ENT_QUOTES; | ||
|
||
/** | ||
* Convert all applicable characters to HTML entities. | ||
* | ||
* @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. | ||
* | ||
* @throws Exception\InvariantViolationException If $encoding is invalid. | ||
* | ||
* @psalm-pure | ||
*/ | ||
function encode(string $html, bool $double_encoding = true, ?string $encoding = null): string | ||
{ | ||
$encoding = Internal\internal_encoding($encoding); | ||
|
||
return htmlentities($html, ENT_QUOTES, $encoding, $double_encoding); | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Html; | ||
|
||
use Psl\Exception; | ||
use Psl\Internal; | ||
|
||
use function htmlspecialchars; | ||
|
||
use const ENT_HTML5; | ||
use const ENT_QUOTES; | ||
use const ENT_SUBSTITUTE; | ||
|
||
/** | ||
* Convert special characters to HTML entities. | ||
* | ||
* @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. | ||
* | ||
* @throws Exception\InvariantViolationException If $encoding is invalid. | ||
* | ||
* @psalm-pure | ||
*/ | ||
function encode_special_characters(string $html, bool $double_encoding = true, ?string $encoding = null): string | ||
{ | ||
$encoding = Internal\internal_encoding($encoding); | ||
|
||
return htmlspecialchars($html, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, $encoding, $double_encoding); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Html; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Html; | ||
|
||
final class DecodeSpecialCharactersTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testEncode(string $expected, string $html): void | ||
{ | ||
static::assertSame($expected, Html\decode_special_characters($html)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield ['hello', 'hello']; | ||
yield ['héllo', 'héllo']; | ||
yield ['<hello />', '<hello />']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>']; | ||
yield ['<p>< </p>', '<p>&lt; </p>']; | ||
|
||
yield ['hello', 'hello']; | ||
yield ['héllo', 'héllo']; | ||
yield ['<hello />', '<hello />']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>']; | ||
yield ['<p>< </p>', '<p>< </p>']; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Html; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Html; | ||
|
||
final class DecodeTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testEncode(string $expected, string $html, ?string $encoding): void | ||
{ | ||
static::assertSame($expected, Html\decode($html, $encoding)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield ['hello', 'hello', 'UTF-8']; | ||
yield ['héllo', 'héllo', 'UTF-8']; | ||
yield ['<hello />', '<hello />', 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', 'UTF-8']; | ||
yield ['<p>< </p>', '<p>&lt; </p>', 'UTF-8']; | ||
|
||
yield ['hello', 'hello', 'UTF-8']; | ||
yield ['héllo', 'héllo', 'UTF-8']; | ||
yield ['<hello />', '<hello />', 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', 'UTF-8']; | ||
yield ['<p>< </p>', '<p>< </p>', 'UTF-8']; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Html; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Html; | ||
|
||
final class EncodeSpecialCharactersTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testEncode(string $expected, string $html, bool $double_encoding, ?string $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 ['<hello />', '<hello />', true, 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', true, 'UTF-8']; | ||
yield ['<p>&lt; </p>', '<p>< </p>', true, 'UTF-8']; | ||
|
||
yield ['hello', 'hello', false, 'UTF-8']; | ||
yield ['héllo', 'héllo', false, 'UTF-8']; | ||
yield ['<hello />', '<hello />', false, 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', false, 'UTF-8']; | ||
yield ['<p>< </p>', '<p>< </p>', false, 'UTF-8']; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Html; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Html; | ||
|
||
final class EncodeTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testEncode(string $expected, string $html, bool $double_encoding, ?string $encoding): void | ||
{ | ||
static::assertSame($expected, Html\encode($html, $double_encoding, $encoding)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield ['hello', 'hello', true, 'UTF-8']; | ||
yield ['héllo', 'héllo', true, 'UTF-8']; | ||
yield ['<hello />', '<hello />', true, 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', true, 'UTF-8']; | ||
yield ['<p>&lt; </p>', '<p>< </p>', true, 'UTF-8']; | ||
|
||
yield ['hello', 'hello', false, 'UTF-8']; | ||
yield ['héllo', 'héllo', false, 'UTF-8']; | ||
yield ['<hello />', '<hello />', false, 'UTF-8']; | ||
yield ['<p>Hello</p>', '<p>Hello</p>', false, 'UTF-8']; | ||
yield ['<p>< </p>', '<p>< </p>', false, 'UTF-8']; | ||
} | ||
} |