Separate <p> tests from <br> tests

This commit is contained in:
Kijin Sung 2017-06-29 18:44:52 +09:00
parent 804533222c
commit 3c772bf1cf
2 changed files with 9 additions and 4 deletions

View file

@ -606,15 +606,18 @@ function is_html_content($str)
{ {
$str = preg_replace('![\r\n]+!', "\n", utf8_trim(utf8_clean($str))); $str = preg_replace('![\r\n]+!', "\n", utf8_trim(utf8_clean($str)));
$line_count = substr_count($str, "\n") + 1; $line_count = substr_count($str, "\n") + 1;
$tag_count = preg_match_all('!(?:^<(?:p|div)(?:>|\s*[a-z])|<(?:/p|/div|br\s?/?)>$)!im', $str); $p_tag_count = preg_match_all('!(?:^<(?:p|div|h[1-6])(?:>|\s*[a-z])|</(?:p|div|h[1-6])>$)!im', $str);
if ($tag_count > 4 || ($tag_count > 0 && $tag_count >= $line_count - 1)) if ($p_tag_count > 4 || ($p_tag_count > 0 && $p_tag_count >= $line_count * 2))
{ {
return true; return true;
} }
else $br_tag_count = preg_match_all('!<br\s?/?>$!im', $str);
if ($br_tag_count > 4 || ($br_tag_count > 0 && $br_tag_count >= $line_count - 1))
{ {
return false; return true;
} }
return false;
} }
/** /**

View file

@ -178,6 +178,8 @@ class FunctionsTest extends \Codeception\TestCase\Test
$this->assertTrue(is_html_content("<div>Hello<br>\r\n\n\n\n\nWorld</div>")); $this->assertTrue(is_html_content("<div>Hello<br>\r\n\n\n\n\nWorld</div>"));
$this->assertFalse(is_html_content("You have to use a <p> tag.")); $this->assertFalse(is_html_content("You have to use a <p> tag."));
$this->assertFalse(is_html_content("This is multiline content.\n<p> tag is here.\nOther lines are here, too.<br>\nMost lines don't have any tags.")); $this->assertFalse(is_html_content("This is multiline content.\n<p> tag is here.\nOther lines are here, too.<br>\nMost lines don't have any tags."));
$this->assertFalse(is_html_content("<p> tag is unbalanced here.\nAnother line!<br />\nAnd a dangling line..."));
$this->assertFalse(is_html_content("Looks like a broken editor<br />\nthat produced\nthis kind of\nstring!</div>"));
} }
public function testIsEmptyHTMLContent() public function testIsEmptyHTMLContent()