Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/manuwhat/strip-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
manuwhat committed Jun 23, 2019
2 parents 09a0ce1 + 0aae3bf commit 7825575
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 41 deletions.
37 changes: 9 additions & 28 deletions htmlstriptestinBrowser.php
Expand Up @@ -2,7 +2,7 @@


$DIR=__DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR;
foreach (array('DOMNodeRecursiveIterator','beforeStrip','prepareStrip','htmlStripHelper','htmlStrip') as $file) {
foreach (array('DOMNodeRecursiveIterator', 'beforeStrip', 'prepareStrip', 'htmlStripHelper', 'htmlStrip') as $file) {
require_once($DIR.$file.'.php');
}

Expand All @@ -11,36 +11,17 @@
$x='<script type="text/javascript" async="" src="./Transitioning%20from%20Data%20Developer%20to%20Data%20Scientist%20-%20Statistics%20for%20Data%20Science_files/f.txt"></script>';
$data='aaaaa<?php echo here ; ?><!doctype><html><head>'.$x.'</head><!-- a comment --> <body> <?php echo here ; ?><br><br> <h2 onmousedown="alert(\'keke\');">u</h2><p></p><h2>a</h2></body></html>b2b2 ';

$hstrip=new htmlStrip($data, 'replace', array('<h2>',false));
var_dump($hstrip->go()===strip_tags($data, '<h2>'));//true
$hstrip=new htmlStrip($data, 'replace', array('<h2>', false));
var_dump($hstrip->go()===strip_tags($data, '<h2>')); //true


$hstrip=new htmlStrip($data);
var_dump($hstrip->go()===strip_tags($data));//true

$hstrip=new htmlStrip($data, 'replace', array('',true), array('src',true));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src'));//false

$hstrip=new htmlStrip($data, 'replace', array('',true), array('src',false));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'type'));//false
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src'));//true;
$data=$html = "
<html>
\r\n\t
<body>
<ul style=''>
<li style='display: inline;' class='foo'>
\xc3\xa0
</li>
<li class='foo' style='display: inline;'>
\xc3\xa1
</li>
</ul>
</body>
\r\n\t
</html>
";
$hstrip=new htmlStrip(mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'), 'replace', array('',true), array('',true),);
var_dump($hstrip->go()===strip_tags($data)); //true

$hstrip=new htmlStrip($data, 'replace', array('', true), array('src', true));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src')); //false


$hstrip=new htmlStrip($data, 'replace', array('',true), array('src',false));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), ' type'));//false
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src'));//true;
4 changes: 2 additions & 2 deletions src/beforeStrip.php
Expand Up @@ -24,11 +24,11 @@ public function __construct($html)
array($this->doctype, $this->html_tag, '</html>', $this->head, '</head>', $this->body, '</body>'),
array('<doctypetag'.substr($this->doctype, 9), '<htmltag '.substr($this->html_tag, 5), '</htmltag>', '<headtag '.substr($this->head, 5), '</headtag>', '<bodytag '.substr($this->body, 5), '</bodytag>'),
$html
).'</doctypetag></div>';
).'</doctypetag></div>';

$preprocessed=token_get_all($html);

$HTML=array_filter($preprocessed, function ($v) {
$HTML=array_filter($preprocessed, function($v) {
return is_array($v)&&$v[0]===T_INLINE_HTML;
});
$PHP=array_diff_key($preprocessed, $HTML);
Expand Down
20 changes: 11 additions & 9 deletions src/htmlStripHelper.php
Expand Up @@ -374,12 +374,14 @@ abstract class htmlstripHelper
'<video>' => 1,
'<wbr>' => 1,
);

protected function loadHTML($html)
{
if (!strlen($html)) {
if (!strlen($html)) {
throw new \InvalidArgumentException("Empty string given");
}
$xml = new \DOMDocument();
//Suppress warnings: proper error handling is beyond scope of example
libxml_use_internal_errors(true);

$true=$xml->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
Expand All @@ -393,7 +395,7 @@ protected function handleTags($notAllowedTags, $callback, $callback1)
if (!is_array($notAllowedTags)) {
return false;
}
if (count($notAllowedTags) !== 2) {
if (count($notAllowedTags)!==2) {
return false;
}
$notAllowedTags=array_values($notAllowedTags);
Expand All @@ -404,7 +406,7 @@ protected function handleTags($notAllowedTags, $callback, $callback1)
}
if (is_array($notAllowedTags)) {
$notAllowedTags=array_filter(array_map($callback, $notAllowedTags), $callback1);
$this->allowedTags=!$keep?array_fill_keys($notAllowedTags, 1):array_diff_key(self::$tags, array_flip($notAllowedTags));
$this->allowedTags=!$keep ?array_fill_keys($notAllowedTags, 1) : array_diff_key(self::$tags, array_flip($notAllowedTags));
} else {
return false;
}
Expand All @@ -416,17 +418,17 @@ protected function handleAttributes($notAllowedAttributes, $callback, $callback2
if (!is_array($notAllowedAttributes)) {
return false;
}
if (count($notAllowedAttributes) !== 2) {
if (count($notAllowedAttributes)!==2) {
return false;
}
$keep=(bool)$notAllowedAttributes[1];
$notAllowedAttributes=$notAllowedAttributes[0];
if (is_string($notAllowedAttributes)) {
$notAllowedAttributes= explode(',', $notAllowedAttributes);
$notAllowedAttributes=explode(',', $notAllowedAttributes);
}
if (is_array($notAllowedAttributes)) {
$notAllowedAttributes=array_filter(array_map($callback, $notAllowedAttributes), $callback2);
$this->allowedAttributes=!$keep?array_fill_keys($notAllowedAttributes, 1):array_diff_key(self::$attributes, array_flip($notAllowedAttributes));
$this->allowedAttributes=!$keep ?array_fill_keys($notAllowedAttributes, 1) : array_diff_key(self::$attributes, array_flip($notAllowedAttributes));
} else {
return false;
}
Expand All @@ -445,8 +447,8 @@ protected static function handlePhp($is_php, $domDoc, &$allowed_tags)
protected static function handleMainHtmlTags(&$result, &$allowed_tags)
{
$result=str_replace(
array('<doctypetag','</doctypetag>','<headtag' ,'</headtag','<htmltag','</htmltag','<bodytag','</bodytag'),
array('<!doctype','','<head','</head','<html','</html','<body','</body'),
array('<doctypetag', '</doctypetag>', '<headtag', '</headtag', '<htmltag', '</htmltag', '<bodytag', '</bodytag'),
array('<!doctype', '', '<head', '</head', '<html', '</html', '<body', '</body'),
$result
);
if (!isset($allowed_tags['<doctypetag>'])) {
Expand All @@ -457,7 +459,7 @@ protected static function handleMainHtmlTags(&$result, &$allowed_tags)
protected static function handleComments($domDoc, &$allowed_tags)
{
if (!isset($allowed_tags['<!-- -->'])) {
$xpath = new \DOMXPath($domDoc);
$xpath=new \DOMXPath($domDoc);
$DomComments=$xpath->query("//comment()");
foreach ($DomComments as $DomComment) {
$DomComment->parentNode->removeChild($DomComment);
Expand Down
4 changes: 2 additions & 2 deletions src/prepareStrip.php
Expand Up @@ -26,13 +26,13 @@ public function getPrepared()

public function prepareHtml()
{
return array_map(function ($v) {
return array_map(function($v) {
return is_array($v) ? $v[1] : $v;
}, $this->prepocessed->getHTML());
}
public function preparePhp()
{
return array_map(function ($v) {
return array_map(function($v) {
return is_array($v)&&($v[0]===T_OPEN_TAG||$v[0]===T_CLOSE_TAG) ? ($v[0]===T_OPEN_TAG ? '<php>' : '</php>') : (is_array($v) ? $v[1] : $v);
}, $this->prepocessed->getPHP());
}
Expand Down

0 comments on commit 7825575

Please sign in to comment.