Skip to content

Commit

Permalink
Html: Concatenate more arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed Apr 21, 2022
1 parent 863f13b commit 5372571
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
19 changes: 13 additions & 6 deletions src/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
class Escape
{
/**
* Escapes string for use everywhere inside HTML (except for comments).
* @param string|HtmlStringable|IHtmlString|mixed $data
* Escapes strings for use everywhere inside HTML (except for comments) and concatenate it to string.
* @param string|HtmlStringable|IHtmlString|mixed ...$data
* @return string
*
* @link https://api.nette.org/2.4/source-Latte.Runtime.Filters.php.html#27-35
*/
public static function html($data): string
public static function html(...$data): string
{
if ($data instanceof HtmlStringable || $data instanceof IHtmlString) {
return (string)$data;
$output = '';

foreach ($data as $item) {
if ($item instanceof HtmlStringable || $item instanceof IHtmlString) {
$output .= $item;
} else {
$output .= htmlspecialchars((string)$item, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE);
}
}
return htmlspecialchars((string)$data, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE);

return $output;
}

/**
Expand Down
45 changes: 27 additions & 18 deletions tests/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@ class EscapeTest extends TestCase
public function getHtmlArgs(): array
{
return [
['', null],
['', ''],
['1', 1],
['string', 'string'],
['&lt;br&gt;', '<br>'],
['&lt; &amp; &apos; &quot; &gt;', '< & \' " >'],
['&amp;quot;', '&quot;'],
['`hello', '`hello'],
["foo \u{FFFD} bar", "foo \u{D800} bar"], // invalid codepoint high surrogates
["foo \u{FFFD}&quot; bar", "foo \xE3\x80\x22 bar"], // stripped UTF
['Hello World', 'Hello World'],
['Hello &lt;World&gt;', 'Hello <World>'],
['&quot; &apos; &lt; &gt; &amp; �', "\" ' < > & \x8F"],
['`hello`', '`hello`'],
['` &lt;br&gt; `', '` <br> `'],
['Foo<br>bar', Html::fromHtml('Foo<br>bar')]
['', []],
['', [null]],
['', ['']],
['1', [1]],
['string', ['string']],
['&lt;br&gt;', ['<br>']],
['&lt; &amp; &apos; &quot; &gt;', ['< & \' " >']],
['&amp;quot;', ['&quot;']],
['`hello', ['`hello']],
["foo \u{FFFD} bar", ["foo \u{D800} bar"]], // invalid codepoint high surrogates
["foo \u{FFFD}&quot; bar", ["foo \xE3\x80\x22 bar"]], // stripped UTF
['Hello World', ['Hello World']],
['Hello &lt;World&gt;', ['Hello <World>']],
['Hello World', [Html::fromText('Hello World')]],
['Hello &lt;World&gt;', [Html::fromText('Hello <World>')]],
['&quot; &apos; &lt; &gt; &amp; �', ["\" ' < > & \x8F"]],
['`hello`', ['`hello`']],
['` &lt;br&gt; `', ['` <br> `']],
['Foo<br>bar', [Html::fromHtml('Foo<br>bar')]],
['Foo&lt;br&gt;bar', [Html::fromText('Foo<br>bar')]],
['Hello &lt;World&gt;Hello &lt;World&gt;', ['Hello <World>', 'Hello <World>']],
['Hello &lt;World&gt;Hello <World>', ['Hello <World>', Html::fromHtml('Hello <World>')]],
['Hello <World>Hello &lt;World&gt;', [Html::fromHtml('Hello <World>'), 'Hello <World>']],
['Hello <World>Hello <World>', [Html::fromHtml('Hello <World>'), Html::fromHtml('Hello <World>')]],
];
}

/**
* @param array<string> $data
* @dataProvider getHtmlArgs
*/
public function testHtml(string $expected, $data): void
public function testHtml(string $expected, array $data): void
{
Assert::same($expected, Escape::html($data));
Assert::same($expected, Escape::html(...$data));
}

public function getHtmlAttrArgs(): array
Expand Down

0 comments on commit 5372571

Please sign in to comment.