Просмотр исходного кода

fix 504 by adding document type 'dom'

which is an already loaded instance
croensch 10 лет назад
Родитель
Сommit
865d8e107d
3 измененных файлов с 20 добавлено и 2 удалено
  1. 5 1
      library/Zend/Dom/Query.php
  2. 14 0
      tests/Zend/Dom/QueryTest.php
  3. 1 1
      tests/phpunit.xml

+ 5 - 1
library/Zend/Dom/Query.php

@@ -154,6 +154,9 @@ class Zend_Dom_Query
     {
         $this->_document = $document;
         $this->_docType  = self::DOC_DOM;
+        if (null !== $document->encoding) {
+            $this->setEncoding($document->encoding);
+        }
         return $this;
     }
 
@@ -211,7 +214,7 @@ class Zend_Dom_Query
     /**
      * Retrieve current document
      *
-     * @return string
+     * @return string|DOMDocument
      */
     public function getDocument()
     {
@@ -276,6 +279,7 @@ class Zend_Dom_Query
         switch ($type) {
             case self::DOC_DOM:
                 $domDoc = $this->_document;
+                $success = true;
                 break;
             case self::DOC_XML:
                 try {

+ 14 - 0
tests/Zend/Dom/QueryTest.php

@@ -143,6 +143,8 @@ class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
         $this->query->setDocument('<html><body></body></html>');
         $this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
+        $this->query->setDocument(new DOMDocument());
+        $this->assertEquals(Zend_Dom_Query::DOC_DOM, $this->query->getDocumentType());
     }
 
     public function testQueryingWithoutRegisteringDocumentShouldThrowException()
@@ -229,6 +231,18 @@ class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(2, count($result), $result->getXpathQuery());
     }
 
+    public function testQueryOnDomDocument()
+    {
+        $document = new DOMDocument('1.0', 'utf-8');
+        $document->loadHTML($this->getHtml(), LIBXML_PARSEHUGE);
+        $this->query->setDocument($document);
+        $test = $this->query->query('.foo');
+        $this->assertTrue($test instanceof Zend_Dom_Query_Result);
+        $testDocument = $test->getDocument();
+        $this->assertTrue($testDocument instanceof DOMDocument);
+        $this->assertEquals('utf-8', $testDocument->encoding);
+    }
+
     /**
      * @group ZF-9243
      */

+ 1 - 1
tests/phpunit.xml

@@ -4,10 +4,10 @@
     </testsuite>
 
     <!-- Enable this for proper unit testing code coverage reports
+    -->
     <filter>
         <whitelist>
             <directory suffix=".php">../library/Zend</directory>
         </whitelist>
     </filter>
-    -->
 </phpunit>