From e56291d00e9ccf1d2bc2f0274fcbcdc6b7af0516 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Wed, 4 Jun 2014 08:59:18 +0200 Subject: Refactored HTML5 class (no more static methods) and explicit error handling --- README.md | 7 +- example.php | 4 +- src/HTML5.php | 206 +++++++++++++++--------------- test/HTML5/Html5Test.php | 102 ++++++++------- test/HTML5/Parser/DOMTreeBuilderTest.php | 1 + test/HTML5/Parser/EventStack.php | 4 +- test/HTML5/Serializer/OutputRulesTest.php | 93 +++++++------- test/HTML5/Serializer/TraverserTest.php | 30 +++-- test/HTML5/TestCase.php | 5 + 9 files changed, 242 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 746ab4d..e82e64c 100644 --- a/README.md +++ b/README.md @@ -59,13 +59,14 @@ $html = <<< 'HERE' HERE; // Parse the document. $dom is a DOMDocument. -$dom = HTML5::loadHTML($html); +$html5 = new HTML5(); +$dom = $html5->loadHTML($html); // Render it as HTML5: -print HTML5::saveHTML($dom); +print $html5->saveHTML($dom); // Or save it to a file: -HTML5::save($dom, 'out.html'); +$html5->save($dom, 'out.html'); ?> ``` diff --git a/example.php b/example.php index 2694aa4..ed0d86e 100644 --- a/example.php +++ b/example.php @@ -24,8 +24,8 @@ $html = <<< 'HERE' HERE; -$dom = \HTML5::loadHTML($html); +$dom = \HTML5Helper::loadHTML($html); print "Converting to HTML 5\n"; -\HTML5::save($dom, fopen("php://stdin", 'w')); +\HTML5Helper::save($dom, fopen("php://stdin", 'w')); diff --git a/src/HTML5.php b/src/HTML5.php index 7295fb4..2ec6fc9 100644 --- a/src/HTML5.php +++ b/src/HTML5.php @@ -1,81 +1,89 @@ FALSE, + 'encode_entities' => FALSE ); + private $errors = array(); + + public function __construct(array $options = array()) { + $this->options = array_merge($this->options, $options); + } + /** + * Get the default options. + * + * @return array + * The default options. + */ + public function getOptions() { + return $this->options; + } /** * Load and parse an HTML file. * - * This will apply the HTML5 parser, which is tolerant of many - * varieties of HTML, including XHTML 1, HTML 4, and well-formed HTML - * 3. Note that in these cases, not all of the old data will be + * This will apply the HTML5 parser, which is tolerant of many + * varieties of HTML, including XHTML 1, HTML 4, and well-formed HTML + * 3. Note that in these cases, not all of the old data will be * preserved. For example, XHTML's XML declaration will be removed. * * The rules governing parsing are set out in the HTML 5 spec. * * @param string $file - * The path to the file to parse. If this is a resource, it is - * assumed to be an open stream whose pointer is set to the first + * The path to the file to parse. If this is a resource, it is + * assumed to be an open stream whose pointer is set to the first * byte of input. * @return \DOMDocument - * A DOM document. These object type is defined by the libxml + * A DOM document. These object type is defined by the libxml * library, and should have been included with your version of PHP. */ - public static function load($file) { - + public function load($file) { // Handle the case where file is a resource. if (is_resource($file)) { // FIXME: We need a StreamInputStream class. - return static::loadHTML(stream_get_contents($file)); + return $this->loadHTML(stream_get_contents($file)); } $input = new FileInputStream($file); - return static::parse($input); + return $this->parse($input); } - /** * Parse a HTML Document from a string. - * - * Take a string of HTML 5 (or earlier) and parse it into a + * + * Take a string of HTML 5 (or earlier) and parse it into a * DOMDocument. * * @param string $string * A html5 document as a string. * @return \DOMDocument - * A DOM document. DOM is part of libxml, which is included with + * A DOM document. DOM is part of libxml, which is included with * almost all distribtions of PHP. */ - public static function loadHTML($string) { + public function loadHTML($string) { $input = new StringInputStream($string); - return static::parse($input); + return $this->parse($input); } - /** * Convenience function to load an HTML file. * @@ -83,18 +91,17 @@ class HTML5 { * PHP DOM implementation. It simply calls load(). * * @param string $file - * The path to the file to parse. If this is a resource, it is - * assumed to be an open stream whose pointer is set to the first + * The path to the file to parse. If this is a resource, it is + * assumed to be an open stream whose pointer is set to the first * byte of input. * * @return \DOMDocument - * A DOM document. These object type is defined by the libxml + * A DOM document. These object type is defined by the libxml * library, and should have been included with your version of PHP. */ - public static function loadHTMLFile($file, $options = NULL) { - return static::load($file, $options); + public function loadHTMLFile($string) { + return $this->load($string); } - /** * Parse a HTML fragment from a string. * @@ -105,11 +112,62 @@ class HTML5 { * A DOM fragment. The DOM is part of libxml, which is included with * almost all distributions of PHP. */ - public static function loadHTMLFragment($string) { + public function loadHTMLFragment($string) { $input = new StringInputStream($string); - return static::parseFragment($input); + return $this->parseFragment($input); + } + /** + * Return all errors encountered into parsing phase + * @return array + */ + public function getErrors() { + return $this->errors; + } + /** + * Return true it some errors were encountered into parsing phase + * @return bool + */ + public function hasErrors() { + return count($this->errors)>0; } + /** + * Parse an input stream. + * + * Lower-level loading function. This requires an input stream instead + * of a string, file, or resource. + */ + public function parse(\HTML5\Parser\InputStream $input) { + $this->errors = array(); + $events = new DOMTreeBuilder(); + $scanner = new Scanner($input); + $parser = new Tokenizer($scanner, $events); + + $parser->parse(); + + $document = $events->document(); + + if($document){ + $this->errors = $document->errors; + } + + return $document; + } + /** + * Parse an input stream where the stream is a fragment. + * + * Lower-level loading function. This requires an input stream instead + * of a string, file, or resource. + */ + public function parseFragment(\HTML5\Parser\InputStream $input) { + $events = new DOMTreeBuilder(TRUE); + $scanner = new Scanner($input); + $parser = new Tokenizer($scanner, $events); + + $parser->parse(); + + return $events->fragment(); + } /** * Save a DOM into a given file as HTML5. * @@ -120,19 +178,19 @@ class HTML5 { * @param array $options * Configuration options when serializing the DOM. These include: * - encode_entities: Text written to the output is escaped by default and not all - * entities are encoded. If this is set to TRUE all entities will be encoded. - * Defaults to FALSE. + * entities are encoded. If this is set to TRUE all entities will be encoded. + * Defaults to FALSE. */ - public static function save($dom, $file, $options = array()) { - $options = $options + static::options(); + public function save($dom, $file, $options = array()) { $close = TRUE; if (is_resource($file)) { $stream = $file; $close = FALSE; - } + } else { $stream = fopen($file, 'w'); } + $options = array_merge($this->getOptions(), $options); $rules = new OutputRules($stream, $options); $trav = new Traverser($dom, $stream, $rules, $options); @@ -142,7 +200,6 @@ class HTML5 { fclose($stream); } } - /** * Convert a DOM into an HTML5 string. * @@ -151,70 +208,15 @@ class HTML5 { * @param array $options * Configuration options when serializing the DOM. These include: * - encode_entities: Text written to the output is escaped by default and not all - * entities are encoded. If this is set to TRUE all entities will be encoded. - * Defaults to FALSE. + * entities are encoded. If this is set to TRUE all entities will be encoded. + * Defaults to FALSE. * * @return string * A HTML5 documented generated from the DOM. */ - public static function saveHTML($dom, $options = array()) { + public function saveHTML($dom, $options = array()) { $stream = fopen('php://temp', 'w'); - static::save($dom, $stream, $options); - return stream_get_contents($stream, -1, 0); - } - - /** - * Parse an input stream. - * - * Lower-level loading function. This requires an input stream instead - * of a string, file, or resource. - */ - public static function parse(\HTML5\Parser\InputStream $input) { - $events = new DOMTreeBuilder(); - $scanner = new Scanner($input); - $parser = new Tokenizer($scanner, $events); - - $parser->parse(); - - return $events->document(); - } - - /** - * Parse an input stream where the stream is a fragment. - * - * Lower-level loading function. This requires an input stream instead - * of a string, file, or resource. - */ - public static function parseFragment(\HTML5\Parser\InputStream $input) { - $events = new DOMTreeBuilder(TRUE); - $scanner = new Scanner($input); - $parser = new Tokenizer($scanner, $events); - - $parser->parse(); - - return $events->fragment(); + $this->save($dom, $stream, array_merge($this->getOptions(), $options)); + return stream_get_contents($stream, - 1, 0); } - - /** - * Get the default options. - * - * @return array - * The default options. - */ - public static function options() { - return static::$options; - } - - /** - * Set a default option. - * - * @param string $name - * The option name. - * @param mixed $value - * The option value. - */ - public static function setOption($name, $value) { - static::$options[$name] = $value; - } - } diff --git a/test/HTML5/Html5Test.php b/test/HTML5/Html5Test.php index 29ce83b..b2d3388 100644 --- a/test/HTML5/Html5Test.php +++ b/test/HTML5/Html5Test.php @@ -3,83 +3,98 @@ namespace HTML5\Tests; class Html5Test extends TestCase { + public function setUp() + { + $this->html5 = $this->getInstance(); + } /** * Parse and serialize a string. */ protected function cycle($html) { - $dom = \HTML5::loadHTML('' . $html . ''); - $out = \HTML5::saveHTML($dom); + + $dom = $this->html5->loadHTML('' . $html . ''); + $out = $this->html5->saveHTML($dom); return $out; } protected function cycleFragment($fragment) { - $dom = \HTML5::loadHTMLFragment($fragment); - $out = \HTML5::saveHTML($dom); + + $dom = $this->html5->loadHTMLFragment($fragment); + $out = $this->html5->saveHTML($dom); return $out; } + public function testErrors() { + $dom = $this->html5->loadHTML(''); + $this->assertInstanceOf('\DOMDocument', $dom); + + $this->assertNotEmpty($this->html5->getErrors()); + $this->assertTrue($this->html5->hasErrors()); + } + public function testLoad() { - $dom = \HTML5::load(__DIR__ . '/Html5Test.html'); + $dom = $this->html5->load(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); + $this->assertFalse($this->html5->hasErrors()); $file = fopen(__DIR__ . '/Html5Test.html', 'r'); - $dom = \HTML5::load($file); + $dom = $this->html5->load($file); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); - $dom = \HTML5::loadHTMLFile(__DIR__ . '/Html5Test.html'); + $dom = $this->html5->loadHTMLFile(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); } public function testLoadHTML() { $contents = file_get_contents(__DIR__ . '/Html5Test.html'); - $dom = \HTML5::loadHTML($contents); + $dom = $this->html5->loadHTML($contents); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); } public function testLoadHTMLFragment() { $fragment = '
Baz
'; - $dom = \HTML5::loadHTMLFragment($fragment); + $dom = $this->html5->loadHTMLFragment($fragment); $this->assertInstanceOf('\DOMDocumentFragment', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); } public function testSaveHTML() { - $dom = \HTML5::load(__DIR__ . '/Html5Test.html'); + $dom = $this->html5->load(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); - $saved = \HTML5::saveHTML($dom); + $saved = $this->html5->saveHTML($dom); $this->assertRegExp('|

This is a test.

|', $saved); } public function testSaveHTMLFragment() { $fragment = '
Baz
'; - $dom = \HTML5::loadHTMLFragment($fragment); + $dom = $this->html5->loadHTMLFragment($fragment); - $string = \HTML5::saveHTML($dom); + $string = $this->html5->saveHTML($dom); $this->assertEquals($fragment, $string); } public function testSave() { - $dom = \HTML5::load(__DIR__ . '/Html5Test.html'); + $dom = $this->html5->load(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); // Test resource $file = fopen('php://temp', 'w'); - \HTML5::save($dom, $file); + $this->html5->save($dom, $file); $content = stream_get_contents($file, -1, 0); $this->assertRegExp('|

This is a test.

|', $content); // Test file $tmpfname = tempnam(sys_get_temp_dir(), "html5-php"); - \HTML5::save($dom, $tmpfname); + $this->html5->save($dom, $tmpfname); $content = file_get_contents($tmpfname); $this->assertRegExp('|

This is a test.

|', $content); unlink($tmpfname); @@ -89,33 +104,35 @@ class Html5Test extends TestCase { // then tries to read that document again. This makes sure we are reading, // and generating a document that works at a high level. public function testItWorks() { - $dom = \HTML5::load(__DIR__ . '/Html5Test.html'); + $dom = $this->html5->load(__DIR__ . '/Html5Test.html'); $this->assertInstanceOf('\DOMDocument', $dom); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); - $saved = \HTML5::saveHTML($dom); + $saved = $this->html5->saveHTML($dom); - $dom2 = \HTML5::loadHTML($saved); + $dom2 = $this->html5->loadHTML($saved); $this->assertInstanceOf('\DOMDocument', $dom2); - $this->assertEmpty($dom2->errors); + $this->assertEmpty($this->html5->getErrors()); } public function testConfig() { - $options = \HTML5::options(); + $html5 = $this->getInstance(); + $options = $html5->getOptions(); $this->assertEquals(FALSE, $options['encode_entities']); - \HTML5::setOption('foo', 'bar'); - \HTML5::setOption('encode_entities', TRUE); - $options = \HTML5::options(); + $html5 = $this->getInstance(array('foo' => 'bar', 'encode_entities'=> TRUE)); + $options = $html5->getOptions(); $this->assertEquals('bar', $options['foo']); $this->assertEquals(TRUE, $options['encode_entities']); // Need to reset to original so future tests pass as expected. - \HTML5::setOption('encode_entities', FALSE); + //$this->getInstance()->setOption('encode_entities', FALSE); + } public function testSvg() { - $dom = \HTML5::loadHTML(' + + $dom = $this->html5->loadHTML('
foo bar baz
@@ -132,7 +149,7 @@ class Html5Test extends TestCase { '); - $this->assertEmpty($dom->errors, print_r($dom->errors, TRUE)); + $this->assertEmpty($this->html5->getErrors()); // Test a mixed case attribute. $list = $dom->getElementsByTagName('svg'); @@ -149,14 +166,14 @@ class Html5Test extends TestCase { $this->assertEquals('textPath', $textPath->tagName); $this->assertNotEquals('textpath', $textPath->tagName); - $html = \HTML5::saveHTML($dom); + $html = $this->html5->saveHTML($dom); $this->assertRegExp('||',$html); $this->assertRegExp('||',$html); } public function testMathMl() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -170,7 +187,7 @@ class Html5Test extends TestCase { '); - $this->assertEmpty($dom->errors); + $this->assertEmpty($this->html5->getErrors()); $list = $dom->getElementsByTagName('math'); $this->assertNotEmpty($list->length); @@ -184,26 +201,25 @@ class Html5Test extends TestCase { $this->assertEquals('http://www.example.com/mathops/multiops.html#plusminus', $csymbol->getAttribute('definitionURL')); $this->assertFalse($csymbol->hasAttribute('definitionurl')); - $html = \HTML5::saveHTML($dom); + $html = $this->html5->saveHTML($dom); $this->assertRegExp('||',$html); $this->assertRegExp('|y|',$html); } public function testUnknownElements() { - // The : should not have special handling accourding to section 2.9 of the // spec. This is differenant than XML. Since we don't know these elements // they are handled as normal elements. Note, to do this is really // an invalid example and you should not embed prefixed xml in html5. - $dom = \HTML5::loadHTMLFragment(" + $dom = $this->html5->loadHTMLFragment(" Big rectangle thing 40 80 um, yeah"); - $this->assertEmpty($dom->errors); - $markup = \HTML5::saveHTML($dom); + $this->assertEmpty($this->html5->getErrors()); + $markup = $this->html5->saveHTML($dom); $this->assertRegExp('|Big rectangle thing|',$markup); $this->assertRegExp('|um, yeah|',$markup); } diff --git a/test/HTML5/Parser/DOMTreeBuilderTest.php b/test/HTML5/Parser/DOMTreeBuilderTest.php index dde8393..ab5378a 100644 --- a/test/HTML5/Parser/DOMTreeBuilderTest.php +++ b/test/HTML5/Parser/DOMTreeBuilderTest.php @@ -347,3 +347,4 @@ class DOMTreeBuilderTest extends \HTML5\Tests\TestCase { $this->assertEquals('foo', $div->textContent); } } + diff --git a/test/HTML5/Parser/EventStack.php b/test/HTML5/Parser/EventStack.php index ffa4f12..acbe3f5 100644 --- a/test/HTML5/Parser/EventStack.php +++ b/test/HTML5/Parser/EventStack.php @@ -87,6 +87,4 @@ class EventStack implements EventHandler { $this->store('pi', func_get_args()); } -} -class EventStackParseError extends \Exception { -} +} \ No newline at end of file diff --git a/test/HTML5/Serializer/OutputRulesTest.php b/test/HTML5/Serializer/OutputRulesTest.php index 8326ef9..34c530d 100644 --- a/test/HTML5/Serializer/OutputRulesTest.php +++ b/test/HTML5/Serializer/OutputRulesTest.php @@ -17,7 +17,10 @@ class OutputRulesTest extends \HTML5\Tests\TestCase {

This is a test.

'; - + public function setUp() + { + $this->html5 = $this->getInstance(); + } /** * Using reflection we make a protected method accessible for testing. * @@ -42,9 +45,9 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function getOutputRules($options = array()) { - $options = $options + \HTML5::options(); + $options = $options + $this->html5->getOptions(); $stream = fopen('php://temp', 'w'); - $dom = \HTML5::loadHTML($this->markup); + $dom = $this->html5->loadHTML($this->markup); $r = new OutputRules($stream, $options); $t = new Traverser($dom, $stream, $r, $options); @@ -52,11 +55,11 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testDocument() { - $dom = \HTML5::loadHTML('foo'); + $dom = $this->html5->loadHTML('foo'); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $r->document($dom); $expected = '' . PHP_EOL . 'foo' . PHP_EOL; @@ -64,11 +67,11 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testDoctype() { - $dom = \HTML5::loadHTML('foo'); + $dom = $this->html5->loadHTML('foo'); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $m = $this->getProtectedMethod('doctype'); $m->invoke($r, 'foo'); @@ -76,7 +79,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testElement() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -89,8 +92,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $r->element($list->item(0)); @@ -98,7 +101,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testOpenTag() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -106,8 +109,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $m = $this->getProtectedMethod('openTag'); @@ -116,7 +119,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testCData() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
@@ -124,14 +127,14 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $r->cdata($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
@@ -142,8 +145,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { $dom->getElementById('foo')->appendChild(new \DOMCdataSection("]]>Foo<[![CDATA test ]]>")); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $r->cdata($list->item(0)->childNodes->item(0)); @@ -151,7 +154,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testComment() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
@@ -159,15 +162,15 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $r->comment($list->item(0)->childNodes->item(0)); $this->assertEquals('', stream_get_contents($stream, -1, 0)); - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
@@ -176,8 +179,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { $dom->getElementById('foo')->appendChild(new \DOMComment(' --> Foo -->')); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); $r->comment($list->item(0)->childNodes->item(0)); @@ -188,7 +191,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testText() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML(' @@ -196,22 +199,22 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('script'); $r->text($list->item(0)->childNodes->item(0)); $this->assertEquals('baz();', stream_get_contents($stream, -1, 0)); - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML(' '); $dom->getElementById('foo')->appendChild(new \DOMText('')); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $item = $dom->getElementById('foo'); $r->text($item->firstChild); @@ -276,7 +279,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testAttrs() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -284,8 +287,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('div'); @@ -297,7 +300,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testSvg() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -313,8 +316,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('svg'); $r->element($list->item(0)); @@ -325,7 +328,7 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testMath() { - $dom = \HTML5::loadHTML(' + $dom = $this->html5->loadHTML('
foo bar baz
@@ -340,8 +343,8 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { '); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $list = $dom->getElementsByTagName('math'); $r->element($list->item(0)); @@ -351,11 +354,11 @@ class OutputRulesTest extends \HTML5\Tests\TestCase { } function testProcessorInstruction() { - $dom = \HTML5::loadHTMLFragment(''); + $dom = $this->html5->loadHTMLFragment(''); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $r->processorInstruction($dom->firstChild); $content = stream_get_contents($stream, -1, 0); diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php index 7f5f5ed..c63ea2b 100644 --- a/test/HTML5/Serializer/TraverserTest.php +++ b/test/HTML5/Serializer/TraverserTest.php @@ -17,7 +17,10 @@ class TraverserTest extends \HTML5\Tests\TestCase {

This is a test.

'; - + public function setUp() + { + $this->html5 = $this->getInstance(); + } /** * Using reflection we make a protected method accessible for testing. * @@ -36,8 +39,9 @@ class TraverserTest extends \HTML5\Tests\TestCase { function getTraverser() { $stream = fopen('php://temp', 'w'); - $dom = \HTML5::loadHTML($this->markup); - $t = new Traverser($dom, $stream, \HTML5::options()); + + $dom = $this->html5->loadHTML($this->markup); + $t = new Traverser($dom, $stream, $html5->getOptions()); // We return both the traverser and stream so we can pull from it. return array($t, $stream); @@ -49,10 +53,12 @@ class TraverserTest extends \HTML5\Tests\TestCase { // use a stream in temp space. $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $dom = \HTML5::loadHTML($this->markup); + $html5 = $this->getInstance(); + + $r = new OutputRules($stream, $this->html5->getOptions()); + $dom = $this->html5->loadHTML($this->markup); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $t = new Traverser($dom, $stream, $r, $html5->getOptions()); $this->assertInstanceOf('\HTML5\Serializer\Traverser', $t); } @@ -60,13 +66,13 @@ class TraverserTest extends \HTML5\Tests\TestCase { function testFragment() { $html = 'foo
bar
'; $input = new \HTML5\Parser\StringInputStream($html); - $dom = \HTML5::parseFragment($input); + $dom = $this->html5->parseFragment($input); $this->assertInstanceOf('\DOMDocumentFragment', $dom); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $out = $t->walk(); $this->assertEquals($html, stream_get_contents($stream, -1, 0)); @@ -75,13 +81,13 @@ class TraverserTest extends \HTML5\Tests\TestCase { function testProcessorInstruction() { $html = ''; $input = new \HTML5\Parser\StringInputStream($html); - $dom = \HTML5::parseFragment($input); + $dom = $this->html5->parseFragment($input); $this->assertInstanceOf('\DOMDocumentFragment', $dom); $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, \HTML5::options()); - $t = new Traverser($dom, $stream, $r, \HTML5::options()); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); $out = $t->walk(); $this->assertEquals($html, stream_get_contents($stream, -1, 0)); diff --git a/test/HTML5/TestCase.php b/test/HTML5/TestCase.php index 836614e..7e98498 100644 --- a/test/HTML5/TestCase.php +++ b/test/HTML5/TestCase.php @@ -9,6 +9,11 @@ class TestCase extends \PHPUnit_Framework_TestCase { // Placeholder. Why is PHPUnit emitting warnings about no tests? } + public function getInstance(array $options = array()) + { + return new \HTML5($options); + } + protected function wrap($fragment) { return self::DOC_OPEN . $fragment . self::DOC_CLOSE; } -- cgit v1.2.3