summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/HTML5.php4
-rw-r--r--src/HTML5/Serializer/Serializer.php (renamed from src/HTML5/Serializer.php)2
-rw-r--r--src/HTML5/Serializer/Traverser.php (renamed from src/HTML5/Traverser.php)59
-rw-r--r--test/HTML5/Serializer/SerializerTest.php (renamed from test/HTML5/SerializerTest.php)12
-rw-r--r--test/HTML5/Serializer/TraverserTest.php78
6 files changed, 105 insertions, 52 deletions
diff --git a/README.md b/README.md
index ee1837f..4e5ac00 100644
--- a/README.md
+++ b/README.md
@@ -45,7 +45,7 @@ $dom = HTML5::parse($html);
print HTML5::saveHTML($dom);
// Or save it to a file:
-HTML5::save('out.html');
+HTML5::save($dom, 'out.html');
?>
```
diff --git a/src/HTML5.php b/src/HTML5.php
index c28e4c1..31db307 100644
--- a/src/HTML5.php
+++ b/src/HTML5.php
@@ -14,7 +14,7 @@ class HTML5 extends \HTML5\Parser {
* Save a DOM into a given file as HTML5.
*/
public static function save($dom, $file) {
- $serializer = new \HTML5\Serializer($dom);
+ $serializer = new \HTML5\Serializer\Serializer($dom);
return $serializer->save($file);
}
@@ -22,7 +22,7 @@ class HTML5 extends \HTML5\Parser {
* Convert a DOM into an HTML5 string.
*/
public static function saveHTML($dom) {
- $serializer = new \HTML5\Serializer($dom);
+ $serializer = new \HTML5\Serializer\Serializer($dom);
return $serializer->saveHTML();
}
}
diff --git a/src/HTML5/Serializer.php b/src/HTML5/Serializer/Serializer.php
index 2bed0e8..f16bbe2 100644
--- a/src/HTML5/Serializer.php
+++ b/src/HTML5/Serializer/Serializer.php
@@ -2,7 +2,7 @@
/**
* A simple serializer that walks the DOM tree and outputs HTML5.
*/
-namespace HTML5;
+namespace HTML5\Serializer;
/**
* Transform a DOM into an HTML5 document.
diff --git a/src/HTML5/Traverser.php b/src/HTML5/Serializer/Traverser.php
index 25375dd..6d5e805 100644
--- a/src/HTML5/Traverser.php
+++ b/src/HTML5/Serializer/Traverser.php
@@ -1,5 +1,5 @@
<?php
-namespace HTML5;
+namespace HTML5\Serializer;
/**
* Traverser for walking a DOM tree.
@@ -12,32 +12,7 @@ namespace HTML5;
*/
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(
@@ -58,19 +33,7 @@ class Traverser {
* input, keygen, link, meta, param, source, track or wbr element, then
* continue on to the next child node at this point.
*/
- static $unary_elements = array(
- 'area' => 1,
- 'base' => 1,
- 'basefont' => 1,
- 'bgsound' => 1,
- 'br' => 1,
- 'col' => 1,
- 'command' => 1,
- 'embed' => 1,
- 'frame' => 1,
- 'hr' => 1,
- 'img' => 1,
- );
+ static $unary_elements = 'area|base|basefont|bgsound|br|col|command|embed|frame|hr|img';
/** Namespaces that should be treated as "local" to HTML5. */
static $local_ns = array(
@@ -293,16 +256,28 @@ class Traverser {
/**
* Is an unary tag.
+ *
+ * @param string $name
+ * The name of the element to test.
+ *
+ * @return bool
+ * True if Unary and false otherwise.
*/
protected function isUnary($name) {
- return isset(self::$unary_elements[$name]);
+ return (bool)preg_match('/^(' . self::$unary_elements . ')$/i', $name);
}
/**
* 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/SerializerTest.php b/test/HTML5/Serializer/SerializerTest.php
index ee8804e..a06fd50 100644
--- a/test/HTML5/SerializerTest.php
+++ b/test/HTML5/Serializer/SerializerTest.php
@@ -3,9 +3,9 @@
namespace HTML5\Tests;
-use \HTML5\Serializer;
+use \HTML5\Serializer\Serializer;
-require_once 'TestCase.php';
+require_once __DIR__ . '/../TestCase.php';
/**
* Test the Serializer.
@@ -13,14 +13,14 @@ require_once 'TestCase.php';
* These tests are all dependent upon the parser. So if the parser
* fails, the results of the serializer tests may not be conclusive.
*/
-class SerializerTest extends TestCase {
+class SerializerTest extends \HTML5\Tests\TestCase {
/**
* Parse and serialize a string.
*/
protected function cycle($html) {
$dom = \HTML5::parse($html);
- $ser = new \HTML5\Serializer($dom, FALSE);
+ $ser = new Serializer($dom, FALSE);
$out = $ser->saveHTML();
return $out;
@@ -32,7 +32,7 @@ class SerializerTest extends TestCase {
$dom = \HTML5::parse($html);
$this->assertTrue($dom instanceof \DOMDocument, "Canary");
- $ser = new \HTML5\Serializer($dom, FALSE);
+ $ser = new Serializer($dom, FALSE);
$out = $ser->saveHTML();
$this->assertTrue(count($out) >= count($html), 'Byte counts');
@@ -47,7 +47,7 @@ class SerializerTest extends TestCase {
$dom = \HTML5::parse($html);
$this->assertTrue($dom instanceof \DOMDocument, "Canary");
- $ser = new \HTML5\Serializer($dom, FALSE);
+ $ser = new Serializer($dom, FALSE);
$out = fopen("php://temp", "w");
$ser->save($out);
diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php
new file mode 100644
index 0000000..0717a75
--- /dev/null
+++ b/test/HTML5/Serializer/TraverserTest.php
@@ -0,0 +1,78 @@
+<?php
+namespace HTML5\Tests;
+
+use \HTML5\Serializer\Traverser;
+
+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: ' . strtoupper($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: ' . strtoupper($tag));
+ }
+ }
+
+ public function testIsUnary() {
+ $elements = array( 'area', 'base', 'basefont', 'bgsound', 'br', 'col',
+ 'command', 'embed', 'frame', 'hr', 'img',
+ );
+
+ // Mocking the required input because there is no checking.
+ $t = new Traverser('', '');
+ $method = $this->getProtectedMethod('isUnary');
+
+ foreach ($elements as $element) {
+ $this->assertTrue($method->invoke($t, $element), 'Unary test failed on: ' . $element);
+
+ // Also test the uppercase version.
+ $this->assertTrue($method->invoke($t, strtoupper($element)), 'Unary test failed on: ' . strtoupper($element));
+ }
+
+ $nonblocks = array('span', 'a', 'div');
+ foreach ($nonblocks as $tag) {
+ $this->assertFalse($method->invoke($t, $tag), 'Unary test failed on: ' . $tag);
+
+ // Also test the uppercase version.
+ $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Unary test failed on: ' . strtoupper($tag));
+ }
+ }
+
+} \ No newline at end of file