Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
68 changes: 58 additions & 10 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class Query
*/
protected $_docType;

/**
* Document encoding
* @var null|string
*/
protected $_encoding;

/**
* XPath namespaces
* @var array
Expand All @@ -72,68 +78,104 @@ class 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;
}

/**
* 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;
}

/**
* 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;
}

Expand Down Expand Up @@ -192,8 +234,14 @@ public function queryXpath($xpathQuery, $query = null)
throw new Exception\RuntimeException('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:
Expand Down Expand Up @@ -247,4 +295,4 @@ protected function _getNodeList($document, $xpathQuery)
$xpathQuery = (string) $xpathQuery;
return $xpath->query($xpathQuery);
}
}
}
41 changes: 41 additions & 0 deletions test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,45 @@ public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes()
$results = $this->query->execute('input[type="hidden"][value="0"]');
$this->assertEquals(1, count($results));
}

/**
* @group ZF-3938
*/
public function testAllowsSpecifyingEncodingAtConstruction()
{
$doc = new 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->execute('.foo');
$this->assertInstanceof('\\Zend\\Dom\\NodeList', $test);
$doc = $test->getDocument();
$this->assertInstanceof('\\DOMDocument', $doc);
$this->assertEquals('utf-8', $doc->encoding);
}

}

0 comments on commit a75eec7

Please sign in to comment.