Add output helpers

[MAILPOET-1891]
This commit is contained in:
Rostislav Wolny
2019-04-04 16:19:09 +02:00
committed by M. Shull
parent 756ebe673b
commit d07f64038b
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace MailPoet\Newsletter\Renderer;
class EscapeHelper {
static function escapeHtmlText($string) {
return htmlspecialchars((string)$string, ENT_NOQUOTES, 'UTF-8');
}
static function escapeHtmlAttr($string) {
return htmlspecialchars((string)$string, ENT_QUOTES, 'UTF-8', true);
}
static function escapeHtmlStyleAttr($string) {
return htmlspecialchars((string)$string, ENT_COMPAT, 'UTF-8', true);
}
static function unescapeHtmlStyleAttr($string) {
return htmlspecialchars_decode((string)$string, ENT_COMPAT);
}
static function escapeHtmlLinkAttr($string) {
$string = self::escapeHtmlAttr($string);
if (preg_match('~^javascript:|^data:text|^data:application~i', $string) === 1) {
return '';
}
return $string;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace MailPoet\Test\Newsletter;
use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper;
class EscapeHelperTest extends \MailPoetUnitTest {
function testItEscapesHtmlText() {
expect(EHelper::escapeHtmlText('Text<tag>\'"Hello</tag>'))
->equals("Text&lt;tag&gt;'\"Hello&lt;/tag&gt;");
}
function testItEscapesHtmlAttr() {
expect(EHelper::escapeHtmlAttr('Text<tag>\'"Hello</tag>'))
->equals("Text&lt;tag&gt;&#039;&quot;Hello&lt;/tag&gt;");
}
function testItEscapesLinkAttr() {
expect(EHelper::escapeHtmlLinkAttr('Text<tag>\'"Hello</tag>'))
->equals("Text&lt;tag&gt;&#039;&quot;Hello&lt;/tag&gt;");
expect(EHelper::escapeHtmlLinkAttr('javaScRipt:Text<tag>\'"Hello</tag>'))
->equals("");
expect(EHelper::escapeHtmlLinkAttr('DAta:Text<tag>\'"Hello</tag>'))
->equals("");
}
}