Browse Source

ZF-11376: Fix XHTML document detection when beginning with XML declaration

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24794 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 years ago
parent
commit
af5cfb482e
2 changed files with 20 additions and 0 deletions
  1. 4 0
      library/Zend/Dom/Query.php
  2. 16 0
      tests/Zend/Dom/QueryTest.php

+ 4 - 0
library/Zend/Dom/Query.php

@@ -124,6 +124,10 @@ class Zend_Dom_Query
         }
         // breaking XML declaration to make syntax highlighting work
         if ('<' . '?xml' == substr(trim($document), 0, 5)) {
+            if (preg_match('/\?'.'>\s+<html[^>]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) {
+                $this->_xpathNamespaces[] = $matches[1];
+                return $this->setDocumentXhtml($document, $encoding);
+            }
             return $this->setDocumentXml($document, $encoding);
         }
         if (strstr($document, 'DTD XHTML')) {

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

@@ -309,6 +309,22 @@ EOF;
         $this->assertType('DOMDocument', $doc);
         $this->assertEquals('utf-8', $doc->encoding);
     }
+    
+    /**
+     * @group ZF-11376
+     */
+    public function testXhtmlDocumentWithXmlDeclaration()
+    {
+        $xhtmlWithXmlDecl = <<<EOB
+<?xml version="1.0" encoding="UTF-8" ?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head><title /></head>
+    <body><p>Test paragraph.</p></body>
+</html>
+EOB;
+        $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
+        $this->assertEquals(1, $this->query->query('//p')->count());
+    }
 }
 
 // Call Zend_Dom_QueryTest::main() if this source file is executed directly.