Преглед изворни кода

ZF-9243: allow suppression of DOM parsing errors

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21156 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew пре 16 година
родитељ
комит
393b7d13fc
3 измењених фајлова са 49 додато и 1 уклоњено
  1. 25 1
      library/Zend/Dom/Query.php
  2. 13 0
      tests/Zend/Dom/QueryTest.php
  3. 11 0
      tests/Zend/Dom/_files/bad-sample.html

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

@@ -53,6 +53,12 @@ class Zend_Dom_Query
     protected $_document;
 
     /**
+     * DOMDocument errors, if any
+     * @var false|array
+     */
+    protected $_documentErrors = false;
+
+    /**
      * Document type
      * @var string
      */
@@ -80,7 +86,8 @@ class Zend_Dom_Query
         if (0 === strlen($document)) {
             return $this;
         }
-        if ('<?xml' == substr(trim($document), 0, 5)) {
+        // breaking XML declaration to make syntax highlighting work
+        if ('<' . '?xml' == substr(trim($document), 0, 5)) {
             return $this->setDocumentXml($document);
         }
         if (strstr($document, 'DTD XHTML')) {
@@ -149,6 +156,16 @@ class Zend_Dom_Query
     }
 
     /**
+     * Get any DOMDocument errors found
+     * 
+     * @return false|array
+     */
+    public function getDocumentErrors()
+    {
+        return $this->_documentErrors;
+    }
+
+    /**
      * Perform a CSS selector query
      *
      * @param  string $query
@@ -174,6 +191,7 @@ class Zend_Dom_Query
             throw new Zend_Dom_Exception('Cannot query; no document registered');
         }
 
+        libxml_use_internal_errors(true);
         $domDoc = new DOMDocument;
         $type   = $this->getDocumentType();
         switch ($type) {
@@ -186,6 +204,12 @@ class Zend_Dom_Query
                 $success = $domDoc->loadHTML($document);
                 break;
         }
+        $errors = libxml_get_errors();
+        if (!empty($errors)) {
+            $this->_documentErrors = $errors;
+            libxml_clear_errors();
+        }
+        libxml_use_internal_errors(false);
 
         if (!$success) {
             require_once 'Zend/Dom/Exception.php';

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

@@ -230,6 +230,19 @@ class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
         $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
         $this->assertEquals(2, count($result), $result->getXpathQuery());
     }
+
+    /**
+     * @group ZF-9243
+     */
+    public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
+    {
+        $file = file_get_contents(dirname(__FILE__) . '/_files/bad-sample.html');
+        $this->query->setDocument($file);
+        $this->query->query('p');
+        $errors = $this->query->getDocumentErrors();
+        $this->assertTrue(is_array($errors));
+        $this->assertTrue(0 < count($errors));
+    }
 }
 
 // Call Zend_Dom_QueryTest::main() if this source file is executed directly.

+ 11 - 0
tests/Zend/Dom/_files/bad-sample.html

@@ -0,0 +1,11 @@
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
+  <link href="foo.bar.css">
+  <title>bad HTMl sample</title>
+</head>
+<body>
+    <p>foo<i>la la la
+        <div><span>text</div>
+</body>
+</html>