Add output helpers
[MAILPOET-1891]
This commit is contained in:
committed by
M. Shull
parent
756ebe673b
commit
d07f64038b
28
lib/Newsletter/Renderer/EscapeHelper.php
Normal file
28
lib/Newsletter/Renderer/EscapeHelper.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
26
tests/unit/Newsletter/Renderer/EscapeHelperTest.php
Normal file
26
tests/unit/Newsletter/Renderer/EscapeHelperTest.php
Normal 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<tag>'\"Hello</tag>");
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItEscapesHtmlAttr() {
|
||||||
|
expect(EHelper::escapeHtmlAttr('Text<tag>\'"Hello</tag>'))
|
||||||
|
->equals("Text<tag>'"Hello</tag>");
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItEscapesLinkAttr() {
|
||||||
|
expect(EHelper::escapeHtmlLinkAttr('Text<tag>\'"Hello</tag>'))
|
||||||
|
->equals("Text<tag>'"Hello</tag>");
|
||||||
|
expect(EHelper::escapeHtmlLinkAttr('javaScRipt:Text<tag>\'"Hello</tag>'))
|
||||||
|
->equals("");
|
||||||
|
expect(EHelper::escapeHtmlLinkAttr('DAta:Text<tag>\'"Hello</tag>'))
|
||||||
|
->equals("");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user