diff options
author | Matt Butcher <[email protected]> | 2013-06-27 16:02:50 -0500 |
---|---|---|
committer | Matt Butcher <[email protected]> | 2013-06-27 16:02:50 -0500 |
commit | 5921b94e0a6b5c1abb3ecf6a25e2292bca68a58b (patch) | |
tree | 345ca68190ac611b909dd3a8881abc5f52d4eca7 | |
parent | d57b7408f1c6a7bdafd63b24b2fd248e959e294f (diff) | |
parent | 206e34460fe8161a3e10eba8b75df291568a8b56 (diff) |
Merge branch 'master' of github.com:Masterminds/html5-php
-rw-r--r-- | src/HTML5/Serializer/OutputRules.php | 2 | ||||
-rw-r--r-- | test/HTML5/Html5Test.php | 8 | ||||
-rw-r--r-- | test/HTML5/Serializer/SerializerTest.php | 96 |
3 files changed, 79 insertions, 27 deletions
diff --git a/src/HTML5/Serializer/OutputRules.php b/src/HTML5/Serializer/OutputRules.php index edea729..a37fdd3 100644 --- a/src/HTML5/Serializer/OutputRules.php +++ b/src/HTML5/Serializer/OutputRules.php @@ -90,7 +90,7 @@ class OutputRules implements \HTML5\Serializer\RulesInterface { * The text node to write. */ public function text($ele) { - if (isset($ele->parentNode) && Elements::isA($ele->parentNode->tagName, Elements::TEXT_RAW)) { + if (isset($ele->parentNode) && isset($ele->parentNode->tagName) && Elements::isA($ele->parentNode->tagName, Elements::TEXT_RAW)) { $this->wr($ele->data); return; } diff --git a/test/HTML5/Html5Test.php b/test/HTML5/Html5Test.php index e122509..14fcb2d 100644 --- a/test/HTML5/Html5Test.php +++ b/test/HTML5/Html5Test.php @@ -34,6 +34,14 @@ class Html5Test extends TestCase { $this->assertRegExp('|<p>This is a test.</p>|', $saved); } + public function testSaveHTMLFragment() { + $fragment = '<section id="Foo"><div class="Bar">Baz</div></section>'; + $dom = \HTML5::loadHTMLFragment($fragment); + + $string = \HTML5::saveHTML($dom); + $this->assertEquals($fragment, $string); + } + public function testSave() { $dom = \HTML5::load(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); diff --git a/test/HTML5/Serializer/SerializerTest.php b/test/HTML5/Serializer/SerializerTest.php index 2c59474..7cfe44f 100644 --- a/test/HTML5/Serializer/SerializerTest.php +++ b/test/HTML5/Serializer/SerializerTest.php @@ -19,7 +19,7 @@ class SerializerTest extends \HTML5\Tests\TestCase { * Parse and serialize a string. */ protected function cycle($html) { - $dom = \HTML5::loadHTML($html); + $dom = \HTML5::loadHTML('<!DOCTYPE html><html><body>' . $html . '</body></html>'); $options = \HTML5::options(); $ser = new Serializer($dom, $options); $out = $ser->saveHTML(); @@ -27,16 +27,13 @@ class SerializerTest extends \HTML5\Tests\TestCase { return $out; } - /** - * Wrap a html5 fragment in a html5 document to run through the parser. - * - * @param string $markup - * - * @return string - * html5 fragment wrapped in a document. - */ - protected function prepareHtml($markup) { - return '<!DOCTYPE html><html><body>' . $markup . '</body></html>'; + protected function cycleFragment($fragment) { + $dom = \HTML5::loadHTMLFragment($fragment); + $options = \HTML5::options(); + $ser = new Serializer($dom, $options); + $out = $ser->saveHTML(); + + return $out; } public function testSaveHTML() { @@ -80,67 +77,114 @@ class SerializerTest extends \HTML5\Tests\TestCase { public function testElements() { // Should have content. - $res = $this->cycle($this->prepareHtml('<div>FOO</div>')); + $res = $this->cycle('<div>FOO</div>'); $this->assertRegExp('|<div>FOO</div>|', $res); // Should be empty - $res = $this->cycle($this->prepareHtml('<span></span>')); + $res = $this->cycle('<span></span>'); + $this->assertRegExp('|<span></span>|', $res); + + // Should have content. + $res = $this->cycleFragment('<div>FOO</div>'); + $this->assertRegExp('|<div>FOO</div>|', $res); + + // Should be empty + $res = $this->cycleFragment('<span></span>'); $this->assertRegExp('|<span></span>|', $res); // Should have no closing tag. - $res = $this->cycle($this->prepareHtml('<hr>')); + $res = $this->cycle('<hr>'); $this->assertRegExp('|<hr></body>|', $res); } public function testAttributes() { - $res = $this->cycle($this->prepareHtml('<div attr="val">FOO</div>')); + $res = $this->cycle('<div attr="val">FOO</div>'); + $this->assertRegExp('|<div attr="val">FOO</div>|', $res); + + // XXX: Note that spec does NOT require attrs in the same order. + $res = $this->cycle('<div attr="val" class="even">FOO</div>'); + $this->assertRegExp('|<div attr="val" class="even">FOO</div>|', $res); + + $res = $this->cycle('<div xmlns:foo="http://example.com">FOO</div>'); + $this->assertRegExp('|<div xmlns:foo="http://example.com">FOO</div>|', $res); + + $res = $this->cycleFragment('<div attr="val">FOO</div>'); $this->assertRegExp('|<div attr="val">FOO</div>|', $res); // XXX: Note that spec does NOT require attrs in the same order. - $res = $this->cycle($this->prepareHtml('<div attr="val" class="even">FOO</div>')); + $res = $this->cycleFragment('<div attr="val" class="even">FOO</div>'); $this->assertRegExp('|<div attr="val" class="even">FOO</div>|', $res); - $res = $this->cycle($this->prepareHtml('<div xmlns:foo="http://example.com">FOO</div>')); + $res = $this->cycleFragment('<div xmlns:foo="http://example.com">FOO</div>'); $this->assertRegExp('|<div xmlns:foo="http://example.com">FOO</div>|', $res); } public function testPCData() { - $res = $this->cycle($this->prepareHtml('<a>This is a test.</a>')); + $res = $this->cycle('<a>This is a test.</a>'); $this->assertRegExp('|This is a test.|', $res); - $res = $this->cycle($this->prepareHtml('This + $res = $this->cycleFragment('<a>This is a test.</a>'); + $this->assertRegExp('|This is a test.|', $res); + + $res = $this->cycle('This + is + a + test.'); + + // Check that newlines are there, but don't count spaces. + $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); + + $res = $this->cycleFragment('This is a - test.')); + test.'); // Check that newlines are there, but don't count spaces. $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); - $res = $this->cycle($this->prepareHtml('<a>This <em>is</em> a test.</a>')); + $res = $this->cycle('<a>This <em>is</em> a test.</a>'); + $this->assertRegExp('|This <em>is</em> a test.|', $res); + + $res = $this->cycleFragment('<a>This <em>is</em> a test.</a>'); $this->assertRegExp('|This <em>is</em> a test.|', $res); } public function testUnescaped() { - $res = $this->cycle($this->prepareHtml('<script>2 < 1</script>')); + $res = $this->cycle('<script>2 < 1</script>'); $this->assertRegExp('|2 < 1|', $res); - $res = $this->cycle($this->prepareHtml('<style>div>div>div</style>')); + $res = $this->cycle('<style>div>div>div</style>'); + $this->assertRegExp('|div>div>div|', $res); + + $res = $this->cycleFragment('<script>2 < 1</script>'); + $this->assertRegExp('|2 < 1|', $res); + + $res = $this->cycleFragment('<style>div>div>div</style>'); $this->assertRegExp('|div>div>div|', $res); } public function testEntities() { - $res = $this->cycle($this->prepareHtml('<a>Apples & bananas.</a>')); + $res = $this->cycle('<a>Apples & bananas.</a>'); + $this->assertRegExp('|Apples & bananas.|', $res); + + $res = $this->cycleFragment('<a>Apples & bananas.</a>'); $this->assertRegExp('|Apples & bananas.|', $res); } public function testComment() { - $res = $this->cycle($this->prepareHtml('a<!-- This is a test. -->b')); + $res = $this->cycle('a<!-- This is a test. -->b'); + $this->assertRegExp('|<!-- This is a test. -->|', $res); + + $res = $this->cycleFragment('a<!-- This is a test. -->b'); $this->assertRegExp('|<!-- This is a test. -->|', $res); } public function testCDATA() { - $res = $this->cycle($this->prepareHtml('a<![CDATA[ This <is> a test. ]]>b')); + $res = $this->cycle('a<![CDATA[ This <is> a test. ]]>b'); + $this->assertRegExp('|<!\[CDATA\[ This <is> a test\. \]\]>|', $res); + + $res = $this->cycleFragment('a<![CDATA[ This <is> a test. ]]>b'); $this->assertRegExp('|<!\[CDATA\[ This <is> a test\. \]\]>|', $res); } } |