diff options
author | Asmir Mustafic <[email protected]> | 2015-01-17 19:38:02 +0100 |
---|---|---|
committer | Asmir Mustafic <[email protected]> | 2015-01-17 19:38:02 +0100 |
commit | dd5485347a39fec8c858e392efac49b9a1e9356c (patch) | |
tree | 01e1342ad17b1ed7b2614c81ea9c0cc9d457be37 | |
parent | 398ebb68c9395a67858d230d0610aa3676bebdc7 (diff) | |
parent | d7b47c0a4e1080770ad624ce096fb6d564d4600b (diff) |
Merge pull request #72 from Masterminds/parser-options
Added $options parameter to HTML reading methods
-rw-r--r-- | composer.json | 2 | ||||
-rw-r--r-- | src/HTML5.php | 34 | ||||
-rw-r--r-- | test/HTML5/Html5Test.php | 30 |
3 files changed, 52 insertions, 14 deletions
diff --git a/composer.json b/composer.json index 54b2764..9101239 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0-dev" + "dev-master": "2.1-dev" } } } 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>'); |