Skip to content

Commit

Permalink
Added trim() to XML input when importing an XML or HTML string. Allow…
Browse files Browse the repository at this point in the history
…s for slight errors in output XML found in the wild where extra space can be introduced in error.
  • Loading branch information
padraic committed Aug 4, 2012
1 parent 984d799 commit 327061e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions library/Zend/Feed/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@ public static function importString($string)
$libxml_errflag = libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$dom = new DOMDocument;
$status = $dom->loadXML($string);
$status = $dom->loadXML(trim($string));
libxml_disable_entity_loader(false);
libxml_use_internal_errors($libxml_errflag);

if (!$status) {
// Build error message
$error = libxml_get_last_error();
if ($error && $error->message) {
$error->message = trim($error->message);
$errormsg = "DOMDocument cannot parse XML: {$error->message}";
} else {
$errormsg = "DOMDocument cannot parse XML: Please check the XML document's validity";
Expand All @@ -298,7 +299,7 @@ public static function importString($string)
$reader = new Feed\Atom($dom, $type);
} else {
throw new Exception\RuntimeException('The URI used does not point to a '
. 'valid Atom, RSS or RDF feed that Zend_Feed_Reader can parse.');
. 'valid Atom, RSS or RDF feed that Zend\Feed\Reader can parse.');
}
return $reader;
}
Expand Down Expand Up @@ -340,13 +341,14 @@ public static function findFeedLinks($uri)
$libxml_errflag = libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$dom = new DOMDocument;
$status = $dom->loadHTML($responseHtml);
$status = $dom->loadHTML(trim($responseHtml));
libxml_disable_entity_loader(false);
libxml_use_internal_errors($libxml_errflag);
if (!$status) {
// Build error message
$error = libxml_get_last_error();
if ($error && $error->message) {
$error->message = trim($error->message);
$errormsg = "DOMDocument cannot parse HTML: {$error->message}";
} else {
$errormsg = "DOMDocument cannot parse HTML: Please check the XML document's validity";
Expand Down
7 changes: 7 additions & 0 deletions tests/Zend/Feed/Reader/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function tearDown()
Reader\Reader::reset();
}

public function testStringImportTrimsContentToAllowSlightlyInvalidXml()
{
$feed = Reader\Reader::importString(
' ' . file_get_contents($this->_feedSamplePath.'/Reader/rss20.xml')
);
}

public function testDetectsFeedIsRss20()
{
$feed = Reader\Reader::importString(
Expand Down

0 comments on commit 327061e

Please sign in to comment.