Fix: Better html detection

This commit is contained in:
Laurent Destailleur 2014-05-07 19:06:15 +02:00
parent 182dd774b6
commit 1bc3558307
2 changed files with 73 additions and 19 deletions

View File

@ -3414,13 +3414,13 @@ function picto_required()
* Clean a string from all HTML tags and entities
*
* @param string $StringHtml String to clean
* @param string $removelinefeed Replace also all lines feeds by a space
* @param string $removelinefeed Replace also all lines feeds by a space, otherwise only last one are removed
* @param string $pagecodeto Encoding of input/output string
* @return string String cleaned
*/
function dol_string_nohtmltag($StringHtml,$removelinefeed=1,$pagecodeto='UTF-8')
{
$pattern = "/<[^>]+>/";
$pattern = "/<[^<>]+>/";
$temp = dol_html_entity_decode($StringHtml,ENT_COMPAT,$pagecodeto);
$temp = preg_replace($pattern,"",$temp);
@ -3486,7 +3486,6 @@ function dol_htmlentitiesbr($stringtoencode,$nl2brmode=0,$pagecodefrom='UTF-8')
$newstring=strtr($newstring,array('&'=>'__and__','<'=>'__lt__','>'=>'__gt__','"'=>'__dquot__'));
$newstring=dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom); // Make entity encoding
$newstring=strtr($newstring,array('__and__'=>'&','__lt__'=>'<','__gt__'=>'>','__dquot__'=>'"'));
//$newstring=strtr($newstring,array('__li__'=>"<li>\n")); // Restore <li>\n
}
else
{
@ -3530,7 +3529,7 @@ function dol_htmlcleanlastbr($stringtodecode)
* Replace html_entity_decode functions to manage errors
*
* @param string $a Operand a
* @param string $b Operand b
* @param string $b Operand b (ENT_QUOTES=convert simple and double quotes)
* @param string $c Operand c
* @return string String decoded
*/
@ -3670,17 +3669,10 @@ function dol_textishtml($msg,$option=0)
{
if (preg_match('/<html/i',$msg)) return true;
elseif (preg_match('/<body/i',$msg)) return true;
elseif (preg_match('/<b>/i',$msg)) return true;
elseif (preg_match('/<br/i',$msg)) return true;
elseif (preg_match('/<div/i',$msg)) return true;
elseif (preg_match('/<em>/i',$msg)) return true;
elseif (preg_match('/<font/i',$msg)) return true;
elseif (preg_match('/<img/i',$msg)) return true;
elseif (preg_match('/<i>/i',$msg)) return true;
elseif (preg_match('/<li/i',$msg)) return true;
elseif (preg_match('/<span/i',$msg)) return true;
elseif (preg_match('/<strong/i',$msg)) return true;
elseif (preg_match('/<table/i',$msg)) return true;
elseif (preg_match('/<(b|em|i)>/i',$msg)) return true;
elseif (preg_match('/<(br|div|font|img|li|span|strong|table)>/i',$msg)) return true;
elseif (preg_match('/<(br|div|font|img|li|span|strong|table)\s+[^<>\/]*>/i',$msg)) return true;
elseif (preg_match('/<(br|div|font|img|li|span|strong|table)\s+[^<>\/]*\/>/i',$msg)) return true;
elseif (preg_match('/&[A-Z0-9]{1,6};/i',$msg)) return true; // Html entities names (http://www.w3schools.com/tags/ref_entities.asp)
elseif (preg_match('/&#[0-9]{2,3};/i',$msg)) return true; // Html entities numbers (http://www.w3schools.com/tags/ref_entities.asp)
return false;
@ -3688,7 +3680,7 @@ function dol_textishtml($msg,$option=0)
}
/**
* Concat 2 descriptions (second one after first one)
* Concat 2 descriptions (second one after first one with a new line separator if required)
* text1 html + text2 html => text1 + '<br>' + text2
* text1 html + text2 txt => text1 + '<br>' + dol_nl2br(text2)
* text1 txt + text2 html => dol_nl2br(text1) + '<br>' + text2

View File

@ -168,20 +168,35 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
$input='xxx <b>yyy</b> zzz';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='xxx<br>';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='text with <div>some div</div>';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='text with HTML &nbsp; entities';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='xxx<br>';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='xxx<br >';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='xxx<br style="eee">';
$after=dol_textishtml($input);
$this->assertTrue($after);
$input='xxx<br style="eee" >';
$after=dol_textishtml($input);
$this->assertTrue($after);
// False
$input='xxx < br>';
$after=dol_textishtml($input);
$this->assertFalse($after);
$input='xxx <email@email.com>'; // <em> is html, <em... is not
$after=dol_textishtml($input);
$this->assertFalse($after);
$input='xxx <brstyle="ee">';
$after=dol_textishtml($input);
$this->assertFalse($after);
}
@ -208,6 +223,53 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
return true;
}
/**
* testDolConcat
*
* @return boolean
*/
public function testDolConcat()
{
$text1="A string 1"; $text2="A string 2"; // text 1 and 2 are text, concat need only \n
$after=dol_concatdesc($text1, $text2);
$this->assertEquals("A string 1\nA string 2",$after);
$text1="A<br>string 1"; $text2="A string 2"; // text 1 is html, concat need <br>\n
$after=dol_concatdesc($text1, $text2);
$this->assertEquals("A<br>string 1<br>\nA string 2",$after);
$text1="A string 1"; $text2="A <b>string</b> 2"; // text 2 is html, concat need <br>\n
$after=dol_concatdesc($text1, $text2);
$this->assertEquals("A string 1<br>\nA <b>string</b> 2",$after);
return true;
}
/**
* testDolStringNohtmltag
*
* @return boolean
*/
public function testDolStringNohtmltag()
{
$text="A\nstring\n";
$after=dol_string_nohtmltag($text,0);
$this->assertEquals("A\nstring",$after,"test1");
$text="A <b>string<b>\n\nwith html tag and '<' chars<br>\n";
$after=dol_string_nohtmltag($text, 0);
$this->assertEquals("A string\n\nwith html tag and '<' chars",$after,"test2");
$text="A <b>string<b>\n\nwith tag with < chars<br>\n";
$after=dol_string_nohtmltag($text, 1);
$this->assertEquals("A string with tag with < chars",$after,"test3");
return true;
}
/**
* testDolHtmlEntitiesBr
*