diff options
-rw-r--r-- | src/HTML5.php | 34 | ||||
-rw-r--r-- | test/HTML5/Html5Test.php | 30 |
2 files changed, 51 insertions, 13 deletions
diff --git a/src/HTML5.php b/src/HTML5.php index 2585fd4..1c46c2b 100644 --- a/src/HTML5.php +++ b/src/HTML5.php @@ -60,20 +60,22 @@ class HTML5 * 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. + * @param array $options + * Configuration options when parsing the HTML * @return \DOMDocument A DOM document. These object type is defined by the libxml * library, and should have been included with your version of PHP. */ - public function load($file) + public function load($file, array $options = array()) { // Handle the case where file is a resource. if (is_resource($file)) { // FIXME: We need a StreamInputStream class. - return $this->loadHTML(stream_get_contents($file)); + return $this->loadHTML(stream_get_contents($file), $options); } $input = new FileInputStream($file); - return $this->parse($input); + return $this->parse($input, $options); } /** @@ -84,14 +86,16 @@ class HTML5 * * @param string $string * A html5 document as a string. + * @param array $options + * Configuration options when parsing the HTML * @return \DOMDocument A DOM document. DOM is part of libxml, which is included with * almost all distribtions of PHP. */ - public function loadHTML($string) + public function loadHTML($string, array $options = array()) { $input = new StringInputStream($string); - return $this->parse($input); + return $this->parse($input, $options); } /** @@ -104,13 +108,15 @@ class HTML5 * 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. + * @param array $options + * Configuration options when parsing the HTML * * @return \DOMDocument A DOM document. These object type is defined by the libxml * library, and should have been included with your version of PHP. */ - public function loadHTMLFile($file) + public function loadHTMLFile($file, array $options = array()) { - return $this->load($file); + return $this->load($file, $options); } /** @@ -118,15 +124,17 @@ class HTML5 * * @param string $string * The html5 fragment as a string. + * @param array $options + * Configuration options when parsing the HTML * * @return \DOMDocumentFragment A DOM fragment. The DOM is part of libxml, which is included with * almost all distributions of PHP. */ - public function loadHTMLFragment($string) + public function loadHTMLFragment($string, array $options = array()) { $input = new StringInputStream($string); - return $this->parseFragment($input); + return $this->parseFragment($input, $options); } /** @@ -155,10 +163,10 @@ class HTML5 * Lower-level loading function. This requires an input stream instead * of a string, file, or resource. */ - public function parse(\Masterminds\HTML5\Parser\InputStream $input) + public function parse(\Masterminds\HTML5\Parser\InputStream $input, array $options = array()) { $this->errors = array(); - $events = new DOMTreeBuilder(false, $this->options); + $events = new DOMTreeBuilder(false, array_merge($this->getOptions(), $options)); $scanner = new Scanner($input); $parser = new Tokenizer($scanner, $events); @@ -174,9 +182,9 @@ class HTML5 * Lower-level loading function. This requires an input stream instead * of a string, file, or resource. */ - public function parseFragment(\Masterminds\HTML5\Parser\InputStream $input) + public function parseFragment(\Masterminds\HTML5\Parser\InputStream $input, array $options = array()) { - $events = new DOMTreeBuilder(true, $this->options); + $events = new DOMTreeBuilder(true, array_merge($this->getOptions(), $options)); $scanner = new Scanner($input); $parser = new Tokenizer($scanner, $events); diff --git a/test/HTML5/Html5Test.php b/test/HTML5/Html5Test.php index 0ab367c..a1a6c9c 100644 --- a/test/HTML5/Html5Test.php +++ b/test/HTML5/Html5Test.php @@ -28,6 +28,36 @@ class Html5Test extends TestCase return $out; } + public function testLoadOptions() + { + // doc + $dom = $this->html5->loadHTML($this->wrap('<t:tag/>'), array( + 'implicitNamespaces' => array('t' => 'http://example.com'), + "xmlNamespaces" => true + )); + $this->assertInstanceOf('\DOMDocument', $dom); + $this->assertEmpty($this->html5->getErrors()); + $this->assertFalse($this->html5->hasErrors()); + + $xpath = new \DOMXPath( $dom ); + $xpath->registerNamespace( "t", "http://example.com" ); + $this->assertEquals(1, $xpath->query( "//t:tag" )->length); + + // doc fragment + $frag = $this->html5->loadHTMLFragment('<t:tag/>', array( + 'implicitNamespaces' => array('t' => 'http://example.com'), + "xmlNamespaces" => true + )); + $this->assertInstanceOf('\DOMDocumentFragment', $frag); + $this->assertEmpty($this->html5->getErrors()); + $this->assertFalse($this->html5->hasErrors()); + + $frag->ownerDocument->appendChild($frag); + $xpath = new \DOMXPath( $frag->ownerDocument ); + $xpath->registerNamespace( "t", "http://example.com" ); + $this->assertEquals(1, $xpath->query( "//t:tag" , $frag)->length); + } + public function testErrors() { $dom = $this->html5->loadHTML('<xx as>'); |