Explorar o código

ZF-3938: Add ability to set document encoding

- Added ability to set document encoding in Zend_Dom_Query:
  - at instantiation
  - via mutator
  - via additional argument to setDocument*() methods

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23062 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew %!s(int64=15) %!d(string=hai) anos
pai
achega
3d41b9bfe9
Modificáronse 2 ficheiros con 96 adicións e 9 borrados
  1. 56 9
      library/Zend/Dom/Query.php
  2. 40 0
      tests/Zend/Dom/QueryTest.php

+ 56 - 9
library/Zend/Dom/Query.php

@@ -65,6 +65,12 @@ class Zend_Dom_Query
     protected $_docType;
 
     /**
+     * Document encoding
+     * @var null|string
+     */
+    protected $_encoding;
+
+    /**
      * XPath namespaces
      * @var array
      */
@@ -76,42 +82,70 @@ class Zend_Dom_Query
      * @param  null|string $document
      * @return void
      */
-    public function __construct($document = null)
+    public function __construct($document = null, $encoding = null)
     {
+        $this->setEncoding($encoding);
         $this->setDocument($document);
     }
 
     /**
+     * Set document encoding
+     * 
+     * @param  string $encoding 
+     * @return Zend_Dom_Query
+     */
+    public function setEncoding($encoding)
+    {
+        $this->_encoding = (null === $encoding) ? null : (string) $encoding;
+        return $this;
+    }
+
+    /**
+     * Get document encoding
+     * 
+     * @return null|string
+     */
+    public function getEncoding()
+    {
+        return $this->_encoding;
+    }
+
+    /**
      * Set document to query
      *
      * @param  string $document
+     * @param  null|string $encoding Document encoding
      * @return Zend_Dom_Query
      */
-    public function setDocument($document)
+    public function setDocument($document, $encoding = null)
     {
         if (0 === strlen($document)) {
             return $this;
         }
         // breaking XML declaration to make syntax highlighting work
         if ('<' . '?xml' == substr(trim($document), 0, 5)) {
-            return $this->setDocumentXml($document);
+            return $this->setDocumentXml($document, $encoding);
         }
         if (strstr($document, 'DTD XHTML')) {
-            return $this->setDocumentXhtml($document);
+            return $this->setDocumentXhtml($document, $encoding);
         }
-        return $this->setDocumentHtml($document);
+        return $this->setDocumentHtml($document, $encoding);
     }
 
     /**
      * Register HTML document
      *
      * @param  string $document
+     * @param  null|string $encoding Document encoding
      * @return Zend_Dom_Query
      */
-    public function setDocumentHtml($document)
+    public function setDocumentHtml($document, $encoding = null)
     {
         $this->_document = (string) $document;
         $this->_docType  = self::DOC_HTML;
+        if (null !== $encoding) {
+            $this->setEncoding($encoding);
+        }
         return $this;
     }
 
@@ -119,12 +153,16 @@ class Zend_Dom_Query
      * Register XHTML document
      *
      * @param  string $document
+     * @param  null|string $encoding Document encoding
      * @return Zend_Dom_Query
      */
-    public function setDocumentXhtml($document)
+    public function setDocumentXhtml($document, $encoding = null)
     {
         $this->_document = (string) $document;
         $this->_docType  = self::DOC_XHTML;
+        if (null !== $encoding) {
+            $this->setEncoding($encoding);
+        }
         return $this;
     }
 
@@ -132,12 +170,16 @@ class Zend_Dom_Query
      * Register XML document
      *
      * @param  string $document
+     * @param  null|string $encoding Document encoding
      * @return Zend_Dom_Query
      */
-    public function setDocumentXml($document)
+    public function setDocumentXml($document, $encoding = null)
     {
         $this->_document = (string) $document;
         $this->_docType  = self::DOC_XML;
+        if (null !== $encoding) {
+            $this->setEncoding($encoding);
+        }
         return $this;
     }
 
@@ -197,8 +239,13 @@ class Zend_Dom_Query
             throw new Zend_Dom_Exception('Cannot query; no document registered');
         }
 
+        $encoding = $this->getEncoding();
         libxml_use_internal_errors(true);
-        $domDoc = new DOMDocument;
+        if (null === $encoding) {
+            $domDoc = new DOMDocument('1.0');
+        } else {
+            $domDoc = new DOMDocument('1.0', $encoding);
+        }
         $type   = $this->getDocumentType();
         switch ($type) {
             case self::DOC_XML:

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

@@ -271,6 +271,46 @@ EOF;
         $results = $this->query->query('input[type="hidden"][value="0"]');
         $this->assertEquals(1, count($results));
     }
+
+    /**
+     * @group ZF-3938
+     */
+    public function testAllowsSpecifyingEncodingAtConstruction()
+    {
+        $doc = new Zend_Dom_Query($this->getHtml(), 'iso-8859-1');
+        $this->assertEquals('iso-8859-1', $doc->getEncoding());
+    }
+
+    /**
+     * @group ZF-3938
+     */
+    public function testAllowsSpecifyingEncodingWhenSettingDocument()
+    {
+        $this->query->setDocument($this->getHtml(), 'iso-8859-1');
+        $this->assertEquals('iso-8859-1', $this->query->getEncoding());
+    }
+
+    /**
+     * @group ZF-3938
+     */
+    public function testAllowsSpecifyingEncodingViaSetter()
+    {
+        $this->query->setEncoding('iso-8859-1');
+        $this->assertEquals('iso-8859-1', $this->query->getEncoding());
+    }
+
+    /**
+     * @group ZF-3938
+     */
+    public function testSpecifyingEncodingSetsEncodingOnDomDocument()
+    {
+        $this->query->setDocument($this->getHtml(), 'utf-8');
+        $test = $this->query->query('.foo');
+        $this->assertType('Zend_Dom_Query_Result', $test);
+        $doc  = $test->getDocument();
+        $this->assertType('DOMDocument', $doc);
+        $this->assertEquals('utf-8', $doc->encoding);
+    }
 }
 
 // Call Zend_Dom_QueryTest::main() if this source file is executed directly.