Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit ac7a10d

Browse files
committed
Merge pull request zendframework#602 from croensch/issue-504
Fixes zendframework#504 - Cannot parse huge documents in Zend_Dom_Query
2 parents 6630f41 + c0ce7ce commit ac7a10d

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

library/Zend/Dom/Query.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ class Zend_Dom_Query
4848
/**#@+
4949
* Document types
5050
*/
51+
const DOC_DOM = 'docDom';
5152
const DOC_XML = 'docXml';
5253
const DOC_HTML = 'docHtml';
5354
const DOC_XHTML = 'docXhtml';
5455
/**#@-*/
5556

5657
/**
57-
* @var string
58+
* @var string|DOMDocument
5859
*/
5960
protected $_document;
6061

@@ -85,7 +86,7 @@ class Zend_Dom_Query
8586
/**
8687
* Constructor
8788
*
88-
* @param null|string $document
89+
* @param null|string|DOMDocument $document
8990
* @param null|string $encoding
9091
*/
9192
public function __construct($document = null, $encoding = null)
@@ -119,12 +120,15 @@ public function getEncoding()
119120
/**
120121
* Set document to query
121122
*
122-
* @param string $document
123+
* @param string|DOMDocument $document
123124
* @param null|string $encoding Document encoding
124125
* @return Zend_Dom_Query
125126
*/
126127
public function setDocument($document, $encoding = null)
127128
{
129+
if ($document instanceof DOMDocument) {
130+
return $this->setDocumentDom($document);
131+
}
128132
if (0 === strlen($document)) {
129133
return $this;
130134
}
@@ -142,6 +146,20 @@ public function setDocument($document, $encoding = null)
142146
return $this->setDocumentHtml($document, $encoding);
143147
}
144148

149+
/**
150+
* @param DOMDocument $document
151+
* @param string $encoding
152+
*/
153+
public function setDocumentDom(DOMDocument $document)
154+
{
155+
$this->_document = $document;
156+
$this->_docType = self::DOC_DOM;
157+
if (null !== $document->encoding) {
158+
$this->setEncoding($document->encoding);
159+
}
160+
return $this;
161+
}
162+
145163
/**
146164
* Register HTML document
147165
*
@@ -196,7 +214,7 @@ public function setDocumentXml($document, $encoding = null)
196214
/**
197215
* Retrieve current document
198216
*
199-
* @return string
217+
* @return string|DOMDocument
200218
*/
201219
public function getDocument()
202220
{
@@ -259,6 +277,10 @@ public function queryXpath($xpathQuery, $query = null)
259277
}
260278
$type = $this->getDocumentType();
261279
switch ($type) {
280+
case self::DOC_DOM:
281+
$domDoc = $this->_document;
282+
$success = true;
283+
break;
262284
case self::DOC_XML:
263285
try {
264286
$domDoc = Zend_Xml_Security::scan($document, $domDoc);

tests/Zend/Dom/QueryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function testDocumentTypeShouldBeAutomaticallyDiscovered()
143143
$this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
144144
$this->query->setDocument('<html><body></body></html>');
145145
$this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
146+
$this->query->setDocument(new DOMDocument());
147+
$this->assertEquals(Zend_Dom_Query::DOC_DOM, $this->query->getDocumentType());
146148
}
147149

148150
public function testQueryingWithoutRegisteringDocumentShouldThrowException()
@@ -229,6 +231,24 @@ public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
229231
$this->assertEquals(2, count($result), $result->getXpathQuery());
230232
}
231233

234+
public function testQueryOnDomDocument()
235+
{
236+
$xml = <<<EOF
237+
<?xml version="1.0" encoding="UTF-8" ?>
238+
<foo>
239+
<bar class="baz"/>
240+
</foo>
241+
EOF;
242+
$document = new DOMDocument();
243+
$document->loadXML($xml, 524288 /* LIBXML_PARSEHUGE */);
244+
$this->query->setDocument($document);
245+
$test = $this->query->query('.baz');
246+
$this->assertTrue($test instanceof Zend_Dom_Query_Result);
247+
$testDocument = $test->getDocument();
248+
$this->assertTrue($testDocument instanceof DOMDocument);
249+
$this->assertEquals('UTF-8', $testDocument->encoding);
250+
}
251+
232252
/**
233253
* @group ZF-9243
234254
*/

0 commit comments

Comments
 (0)