diff options
-rw-r--r-- | src/HTML5/Serializer/Traverser.php | 35 | ||||
-rw-r--r-- | test/HTML5/Serializer/TraverserTest.php | 43 |
2 files changed, 51 insertions, 27 deletions
diff --git a/src/HTML5/Serializer/Traverser.php b/src/HTML5/Serializer/Traverser.php index 9acc617..34faf5c 100644 --- a/src/HTML5/Serializer/Traverser.php +++ b/src/HTML5/Serializer/Traverser.php @@ -12,32 +12,7 @@ namespace HTML5\Serializer; */ class Traverser { - // TODO: Refactor this into an element mask. - static $block_elements = array( - 'html' => 1, - 'body' => 1, - 'head' => 1, - 'p' => 1, - 'div' => 1, - 'h1' => 1, - 'h2' => 1, - 'h3' => 1, - 'h4' => 1, - 'h5' => 1, - 'h6' => 1, - 'title' => 1, - 'script' => 1, - 'link' => 1, - 'meta' => 1, - 'section' => 1, - 'article' => 1, - 'table' => 1, - 'tbody' => 1, - 'tr' => 1, - 'th' => 1, - 'td' => 1, - //'form' => 1, - ); + static $block_elements = 'html|body|head|p|div|h[1-6]|title|script|link|meta|section|article|table|tbody|tr|th|td'; // TODO: Refactor this into an element mask. static $literal_elements = array( @@ -300,9 +275,15 @@ class Traverser { /** * Is block element. + * + * @param string $name + * The name of the element to test if a block level element + * + * @return bool + * If the element is block level or not. */ protected function isBlock($name) { - return isset(self::$block_elements[$name]); + return (bool)preg_match('/^(' . self::$block_elements . ')$/i', $name); } protected function isLiteral($element) { diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php index c7e43d8..8638e22 100644 --- a/test/HTML5/Serializer/TraverserTest.php +++ b/test/HTML5/Serializer/TraverserTest.php @@ -7,4 +7,47 @@ require_once __DIR__ . '/../TestCase.php'; class TraverserTest extends \HTML5\Tests\TestCase { + /** + * Using reflection we make a protected method accessible for testing. + * + * @param string $name + * The name of the method on the Traverser class to test. + * + * @return \ReflectionMethod + * \ReflectionMethod for the specified method + */ + function getProtectedMethod($name) { + $class = new \ReflectionClass('\HTML5\Serializer\Traverser'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + public function testIsBlock() { + $blocks = array('html', 'body', 'head', 'p', 'div', 'h1', 'h2', 'h3', 'h4', + 'h5', 'h6', 'title', 'script', 'link', 'meta', 'section', 'article', + 'table', 'tbody', 'tr', 'th', 'td', + //'form', + ); + + // Mocking the required input because there is no checking. + $t = new Traverser('', ''); + $method = $this->getProtectedMethod('isBlock'); + + foreach ($blocks as $block) { + $this->assertTrue($method->invoke($t, $block), 'Block test failed on: ' . $block); + + // Also test the uppercase version. + $this->assertTrue($method->invoke($t, strtoupper($block)), 'Block test failed on: ' . $block); + } + + $nonblocks = array('span', 'a', 'img'); + foreach ($nonblocks as $tag) { + $this->assertFalse($method->invoke($t, $tag), 'Block test failed on: ' . $tag); + + // Also test the uppercase version. + $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Block test failed on: ' . $tag); + } + } + }
\ No newline at end of file |