cvs: pear /XML_Feed_Parser/Parser Atom.php AtomElement.php Type.php /XML_Feed_Parser/tests atomValues.php
| From: | James Stewart | Date: | Sat, 15 Oct 2005 14:13:52 +0000 |
| Subject: | cvs: pear /XML_Feed_Parser/Parser Atom.php AtomElement.php Type.php /XML_Feed_Parser/tests atomValues.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-35577@lists.php.net to get a copy of this message | ||
jystewart Sat Oct 15 10:13:52 2005 EDT
Modified files:
/pear/XML_Feed_Parser/Parser Atom.php AtomElement.php Type.php
/pear/XML_Feed_Parser/tests atomValues.php
Log:
Changed handling of xml:base in Type.php to use the DOM baseURI value
Changed handling of atom text constructs and atom:content to honour allowed content types
Reordered test slightly
http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/Atom.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/Atom.php diff -u pear/XML_Feed_Parser/Parser/Atom.php:1.1 pear/XML_Feed_Parser/Parser/Atom.php:1.2 --- pear/XML_Feed_Parser/Parser/Atom.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/Atom.php Sat Oct 15 10:13:49 2005 @@ -17,7 +17,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: Atom.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: Atom.php,v 1.2 2005/10/15 14:13:49 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -140,7 +140,7 @@ $entries = $this->xpath->query("//atom:entry[atom:id='$id']"); if ($entries->length > 0) { - $xmlBase = $this->getBase($entries->item(0)); + $xmlBase = $entries->item(0)->baseURI; $entry = new $this->itemElement($entries->item(0), $this, $xmlBase); return $entry; } @@ -166,7 +166,7 @@ if (isset($arguments['param'])) { $parameter = $arguments['param']; } else { -' $parameter = 'name'; + $parameter = 'name'; } $param = $section->item($offset)->getElementsByTagName($parameter); if ($param->length == 0) { @@ -181,27 +181,51 @@ * * @todo Build in attribute support * @todo Handle elements that recur + * @todo Clarify overlap with getContent() * @param string $method The name of the text construct we want * @param array $arguments An array which we hope gives a 'param' * @return string */ protected function getText($method, $arguments) { - $tags = $this->model->getElementsByTagName($method); - if ($tags->length > 0) { - if ($tags->item(0)->hasChildNodes() and - $tags->item(0)->childNodes->length > 1) { - $value = ''; - foreach ($tags->item(0)->childNodes as $child) { - $simple = simplexml_import_dom($child); - $value .= $simple->asXML(); - } - return $value; - } else { - return $tags->item(0)->nodeValue; - } - } - return false; + $tags = $this->model->getElementsByTagName($method); + if ($tags->length == 0) { + return false; + } + + $content = $tags->item(0); + + if ($content->hasAttribute("type")) { + $type = $content->getAttribute("type"); + } else { + $type = "text"; + } + + switch ($type) { + case 'text': + return $content->nodeValue; + break; + case 'html': + return str_replace("<", "<", $content->nodeValue); + break; + case 'xhtml': + $container = $content->getElementsByTagName("div"); + if ($container->length == 0) { + return false; + } + $contents = $container->item(0); + if ($contents->hasChildNodes()) { + /* Iterate through, applying xml:base and store the result */ + $result = ""; + foreach ($contents->childNodes as $node) { + $result .= $this->traverseNode($node); + } + return $result; + } + break; + break; + } + return false; } /** http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/AtomElement.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/AtomElement.php diff -u pear/XML_Feed_Parser/Parser/AtomElement.php:1.1 pear/XML_Feed_Parser/Parser/AtomElement.php:1.2 --- pear/XML_Feed_Parser/Parser/AtomElement.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/AtomElement.php Sat Oct 15 10:13:50 2005 @@ -17,7 +17,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: AtomElement.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: AtomElement.php,v 1.2 2005/10/15 14:13:50 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -133,33 +133,55 @@ * If there is content we return it, if not and there's a 'src' attribute * we return the value of that instead. * - * @todo Be clearer about content types + * @todo Work out overlap with general text construct + * @todo Get clarity on what to do with other mime-typed content * @return string|false */ - function getContent() - { - $tags = $this->model->getElementsByTagName('content'); - if ($tags->length > 0) { - $value = $tags->item(0); - if ($value->hasChildNodes()) { - $content = ''; - foreach ($value->childNodes as $child) { - if ($child instanceof DOMText) { - $content .= $child->nodeValue; - } else { - $simple = simplexml_import_dom($child); - $content .= $simple->asXML(); - } - } - return $content; - } else if ($value->nodeValue) { - return $value->nodeValue; - } else if ($value->getAttribute('src')) { - return $this->addBase($value->getAttribute('src'), $value); - } - } - return false; - } + function getContent() + { + $tags = $this->model->getElementsByTagName('content'); + if ($tags->length == 0) { + return false; + } + + $content = $tags->item(0); + + if ($content->getAttribute('src')) { + return $this->addBase($content->getAttribute('src'), $content); + } + if ($content->hasAttribute("type")) { + $type = $content->getAttribute("type"); + } else { + $type = "text"; + } + + switch ($type) { + case 'text': + return $content->nodeValue; + break; + case 'html': + return str_replace("<", "<", $content->nodeValue); + break; + case 'xhtml': + $container = $content->getElementsByTagName("div"); + if ($container->length == 0) { + return false; + } + $contents = $container->item(0); + if ($contents->hasChildNodes()) { + /* Iterate through, applying xml:base and store the result */ + $result = ""; + foreach ($contents->childNodes as $node) { + $result .= $this->traverseNode($node); + } + return $result; + } + break; + default: + break; + } + return false; + } /** * The Atom spec doesn't provide for an enclosure element, but it is http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/Type.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/Type.php diff -u pear/XML_Feed_Parser/Parser/Type.php:1.1 pear/XML_Feed_Parser/Parser/Type.php:1.2 --- pear/XML_Feed_Parser/Parser/Type.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/Type.php Sat Oct 15 10:13:50 2005 @@ -18,7 +18,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: Type.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: Type.php,v 1.2 2005/10/15 14:13:50 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -39,13 +39,6 @@ public $model; /** - * We don't particularly need to use this for this class, but it's helpful - * to make inheritance work. - * @var string - */ - protected $xmlBase; - - /** * For iteration we'll want a count of the number of entries * @var int */ @@ -109,57 +102,44 @@ } /** - * We will often need to extract the xml:base values that apply to a - * link. This method iterates through the heirarchy and extracts the - * relevant attributes, and then combines them. + * We have other methods which will traverse the DOM and work out the different + * xml:base declarations we need to be aware of. We then need to combine them. + * If a declaration starts with a protocol then we restart the string. If it + * starts with a / then we add on to the domain name. Otherwise we simply tag + * it on to the end. * - * @param DOMElement The starting node - * @return string + * @param string $base - the base to add the link to + * @param string $link */ - function getBase($thisNode) + function combineBases($base, $link) { - /* We'll need some containers and settings */ - $bases = array(); - $combinedBase = $this->xmlBase; - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $combinedBase, $results); - isset($results[1]) ? $firstLayer = $results[1] : $firstLayer = ''; - - $nameSpace = 'http://www.w3.org/XML/1998/namespace'; - - /* Iterate up the tree and grab all parent xml:bases */ - while ($thisNode instanceof DOMElement) { - if ($thisNode->hasAttributes()) { - $test = $thisNode->attributes->getNamedItemNS($nameSpace, 'base'); - if ($test) { - array_push($bases, $test->nodeValue); - } - } - $thisNode = $thisNode->parentNode; - } - - /* if starts with a protocol then restart the string. if starts with a / then - * add on to the domain name. otherwise tag on to the end */ - $bases = array_reverse($bases); - - foreach ($bases as $base) { - if (preg_match('/^[A-Za-z]+:\/\//', $base)) { - $combinedBase = $base; - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $base, $results); - $firstLayer = $results[1]; - } else if (preg_match('/^\//', $base)) { - $combinedBase = $firstLayer . $base; - } else { - $combinedBase .= $base; + if (preg_match('/^[A-Za-z]+:\/\//', $link)) { + return $link; + } else if (preg_match('/^\//', $link)) { + /* Extract domain and suffix link to that */ + preg_match('/^([A-Za-z]+:\/\/.*)?\/*/', $base, $results); + $firstLayer = $results[0]; + return $firstLayer . "/" . $link; + } else if (preg_match('/^\.\.\//', $base)) { + /* Step up link to find place to be */ + preg_match('/^((\.\.\/)+)(.*)$/', $link, $bases); + $suffix = $bases[3]; + $count = preg_match_all('/\.\.\//', $bases[1], $steps); + $url = explode("/", $base); + for ($i = 0; $i <= $count; $i++) { + array_pop($url); } - + return implode("/", $url) . "/" . $suffix; + } else { + /* Just stick it on the end */ + return $base . $link; } return $combinedBase; } /** - * getBase gets us the xml:base data. We then need to process that with regard - * to our current link. This function does that and returns the link in as - * complete a form as possible. + * Gets us the xml:base data and then processes that with regard + * to our current link. * * @param string * @param DOMElement @@ -171,15 +151,7 @@ return $link; } - $base = $this->getBase($element); - - if (preg_match('/^\//', $link)) { - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $base, $results); - $root = $results[1]; - return $root . $link; - } else { - return $base . $link; - } + return $this->combineBases($element->baseURI, $link); } /** @@ -193,7 +165,7 @@ if (! isset($this->entries[$offset])) { $entries = $this->model->getElementsByTagName($this->itemElement); if ($entries->length > 0) { - $xmlBase = $this->getBase($entries->item($offset)); + $xmlBase = $entries->item($offset)->baseURI; $this->entries[$offset] = new $this->itemClass( $entries->item($offset), $this, $xmlBase); } else { @@ -287,6 +259,63 @@ return 0; } + /** + * We need a couple of methods to access XHTML content stored in feeds. + * This is because we dereference all xml:base references before returning + * the element. This method handles the attributes. + * + * @param DOMElement $node + * @return string + */ + function processXHTMLAttributes($node) { + $return = ''; + foreach ($node->attributes as $attribute) { + if ($attribute->name == 'src' or $attribute->name == 'href') { + $attribute->value = $this->addBase($attribute->value, $attribute); + } + $return .= $attribute->name . '="' . $attribute->value .'" '; + } + return " " . trim($return); + } + + /** + * We need a couple of methods to access XHTML content stored in feeds. + * This is because we dereference all xml:base references before returning + * the element. This method recurs through the tree descending from the node + * and builds our string + * + * @param DOMElement $node + * @return string + */ + function traverseNode($node) + { + $content = ""; + + /* Add the opening of this node to the content */ + if ($node instanceof DOMElement) { + $content .= "<" . $node->tagName . + $this->processAttributes($node) . ">"; + } + + /* Process children */ + if ($node->hasChildNodes()) { + foreach ($node->childNodes as $child) { + $content .= $this->traverseNode($child); + } + } + + if ($node instanceof DOMText) { + $content .= $node->nodeValue; + } + + /* Add the closing of this node to the content */ + if ($node instanceof DOMElement) { + $content .= "</" . $node->tagName . ">"; + } + + return $content; + } + /** * Return an XML serialization of the feed, should it be required. Most * users however, will already have a serialization that they used when http://cvs.php.net/diff.php/pear/XML_Feed_Parser/tests/atomValues.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/tests/atomValues.php diff -u pear/XML_Feed_Parser/tests/atomValues.php:1.1 pear/XML_Feed_Parser/tests/atomValues.php:1.2 --- pear/XML_Feed_Parser/tests/atomValues.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/tests/atomValues.php Sat Oct 15 10:13:52 2005 @@ -1,6 +1,6 @@ <?php -require_once 'XML/Feed/Parser.php'; +require_once '../Parser.php'; require_once 'PHPUnit.php'; class XML_Feed_Parser_Atom_valueValidity_TestCase extends PHPUnit_TestCase @@ -22,7 +22,7 @@ function test_FeedTitle() { $value = "dive into mark"; - $this->assertEquals($this->feed->title, $value); + $this->assertEquals($value, $this->feed->title); } function test_feedSubtitle() @@ -103,9 +103,7 @@ function test_entryContent() { - $value = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> - <p><i>[Update: The Atom draft is finished.]</i></p> - </div>"; + $value = "<p><i>[Update: The Atom draft is finished.]</i></p>"; $content = trim($this->entry->content); $content = preg_replace("/\t/", " ", $content); $content = preg_replace("/( )+/", " ", $content);
http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/Atom.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/Atom.php diff -u pear/XML_Feed_Parser/Parser/Atom.php:1.1 pear/XML_Feed_Parser/Parser/Atom.php:1.2 --- pear/XML_Feed_Parser/Parser/Atom.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/Atom.php Sat Oct 15 10:13:49 2005 @@ -17,7 +17,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: Atom.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: Atom.php,v 1.2 2005/10/15 14:13:49 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -140,7 +140,7 @@ $entries = $this->xpath->query("//atom:entry[atom:id='$id']"); if ($entries->length > 0) { - $xmlBase = $this->getBase($entries->item(0)); + $xmlBase = $entries->item(0)->baseURI; $entry = new $this->itemElement($entries->item(0), $this, $xmlBase); return $entry; } @@ -166,7 +166,7 @@ if (isset($arguments['param'])) { $parameter = $arguments['param']; } else { -' $parameter = 'name'; + $parameter = 'name'; } $param = $section->item($offset)->getElementsByTagName($parameter); if ($param->length == 0) { @@ -181,27 +181,51 @@ * * @todo Build in attribute support * @todo Handle elements that recur + * @todo Clarify overlap with getContent() * @param string $method The name of the text construct we want * @param array $arguments An array which we hope gives a 'param' * @return string */ protected function getText($method, $arguments) { - $tags = $this->model->getElementsByTagName($method); - if ($tags->length > 0) { - if ($tags->item(0)->hasChildNodes() and - $tags->item(0)->childNodes->length > 1) { - $value = ''; - foreach ($tags->item(0)->childNodes as $child) { - $simple = simplexml_import_dom($child); - $value .= $simple->asXML(); - } - return $value; - } else { - return $tags->item(0)->nodeValue; - } - } - return false; + $tags = $this->model->getElementsByTagName($method); + if ($tags->length == 0) { + return false; + } + + $content = $tags->item(0); + + if ($content->hasAttribute("type")) { + $type = $content->getAttribute("type"); + } else { + $type = "text"; + } + + switch ($type) { + case 'text': + return $content->nodeValue; + break; + case 'html': + return str_replace("<", "<", $content->nodeValue); + break; + case 'xhtml': + $container = $content->getElementsByTagName("div"); + if ($container->length == 0) { + return false; + } + $contents = $container->item(0); + if ($contents->hasChildNodes()) { + /* Iterate through, applying xml:base and store the result */ + $result = ""; + foreach ($contents->childNodes as $node) { + $result .= $this->traverseNode($node); + } + return $result; + } + break; + break; + } + return false; } /** http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/AtomElement.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/AtomElement.php diff -u pear/XML_Feed_Parser/Parser/AtomElement.php:1.1 pear/XML_Feed_Parser/Parser/AtomElement.php:1.2 --- pear/XML_Feed_Parser/Parser/AtomElement.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/AtomElement.php Sat Oct 15 10:13:50 2005 @@ -17,7 +17,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: AtomElement.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: AtomElement.php,v 1.2 2005/10/15 14:13:50 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -133,33 +133,55 @@ * If there is content we return it, if not and there's a 'src' attribute * we return the value of that instead. * - * @todo Be clearer about content types + * @todo Work out overlap with general text construct + * @todo Get clarity on what to do with other mime-typed content * @return string|false */ - function getContent() - { - $tags = $this->model->getElementsByTagName('content'); - if ($tags->length > 0) { - $value = $tags->item(0); - if ($value->hasChildNodes()) { - $content = ''; - foreach ($value->childNodes as $child) { - if ($child instanceof DOMText) { - $content .= $child->nodeValue; - } else { - $simple = simplexml_import_dom($child); - $content .= $simple->asXML(); - } - } - return $content; - } else if ($value->nodeValue) { - return $value->nodeValue; - } else if ($value->getAttribute('src')) { - return $this->addBase($value->getAttribute('src'), $value); - } - } - return false; - } + function getContent() + { + $tags = $this->model->getElementsByTagName('content'); + if ($tags->length == 0) { + return false; + } + + $content = $tags->item(0); + + if ($content->getAttribute('src')) { + return $this->addBase($content->getAttribute('src'), $content); + } + if ($content->hasAttribute("type")) { + $type = $content->getAttribute("type"); + } else { + $type = "text"; + } + + switch ($type) { + case 'text': + return $content->nodeValue; + break; + case 'html': + return str_replace("<", "<", $content->nodeValue); + break; + case 'xhtml': + $container = $content->getElementsByTagName("div"); + if ($container->length == 0) { + return false; + } + $contents = $container->item(0); + if ($contents->hasChildNodes()) { + /* Iterate through, applying xml:base and store the result */ + $result = ""; + foreach ($contents->childNodes as $node) { + $result .= $this->traverseNode($node); + } + return $result; + } + break; + default: + break; + } + return false; + } /** * The Atom spec doesn't provide for an enclosure element, but it is http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser/Type.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/Parser/Type.php diff -u pear/XML_Feed_Parser/Parser/Type.php:1.1 pear/XML_Feed_Parser/Parser/Type.php:1.2 --- pear/XML_Feed_Parser/Parser/Type.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/Parser/Type.php Sat Oct 15 10:13:50 2005 @@ -18,7 +18,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1 - * @version CVS: $Id: Type.php,v 1.1 2005/10/11 14:29:50 jystewart Exp $ + * @version CVS: $Id: Type.php,v 1.2 2005/10/15 14:13:50 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -39,13 +39,6 @@ public $model; /** - * We don't particularly need to use this for this class, but it's helpful - * to make inheritance work. - * @var string - */ - protected $xmlBase; - - /** * For iteration we'll want a count of the number of entries * @var int */ @@ -109,57 +102,44 @@ } /** - * We will often need to extract the xml:base values that apply to a - * link. This method iterates through the heirarchy and extracts the - * relevant attributes, and then combines them. + * We have other methods which will traverse the DOM and work out the different + * xml:base declarations we need to be aware of. We then need to combine them. + * If a declaration starts with a protocol then we restart the string. If it + * starts with a / then we add on to the domain name. Otherwise we simply tag + * it on to the end. * - * @param DOMElement The starting node - * @return string + * @param string $base - the base to add the link to + * @param string $link */ - function getBase($thisNode) + function combineBases($base, $link) { - /* We'll need some containers and settings */ - $bases = array(); - $combinedBase = $this->xmlBase; - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $combinedBase, $results); - isset($results[1]) ? $firstLayer = $results[1] : $firstLayer = ''; - - $nameSpace = 'http://www.w3.org/XML/1998/namespace'; - - /* Iterate up the tree and grab all parent xml:bases */ - while ($thisNode instanceof DOMElement) { - if ($thisNode->hasAttributes()) { - $test = $thisNode->attributes->getNamedItemNS($nameSpace, 'base'); - if ($test) { - array_push($bases, $test->nodeValue); - } - } - $thisNode = $thisNode->parentNode; - } - - /* if starts with a protocol then restart the string. if starts with a / then - * add on to the domain name. otherwise tag on to the end */ - $bases = array_reverse($bases); - - foreach ($bases as $base) { - if (preg_match('/^[A-Za-z]+:\/\//', $base)) { - $combinedBase = $base; - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $base, $results); - $firstLayer = $results[1]; - } else if (preg_match('/^\//', $base)) { - $combinedBase = $firstLayer . $base; - } else { - $combinedBase .= $base; + if (preg_match('/^[A-Za-z]+:\/\//', $link)) { + return $link; + } else if (preg_match('/^\//', $link)) { + /* Extract domain and suffix link to that */ + preg_match('/^([A-Za-z]+:\/\/.*)?\/*/', $base, $results); + $firstLayer = $results[0]; + return $firstLayer . "/" . $link; + } else if (preg_match('/^\.\.\//', $base)) { + /* Step up link to find place to be */ + preg_match('/^((\.\.\/)+)(.*)$/', $link, $bases); + $suffix = $bases[3]; + $count = preg_match_all('/\.\.\//', $bases[1], $steps); + $url = explode("/", $base); + for ($i = 0; $i <= $count; $i++) { + array_pop($url); } - + return implode("/", $url) . "/" . $suffix; + } else { + /* Just stick it on the end */ + return $base . $link; } return $combinedBase; } /** - * getBase gets us the xml:base data. We then need to process that with regard - * to our current link. This function does that and returns the link in as - * complete a form as possible. + * Gets us the xml:base data and then processes that with regard + * to our current link. * * @param string * @param DOMElement @@ -171,15 +151,7 @@ return $link; } - $base = $this->getBase($element); - - if (preg_match('/^\//', $link)) { - preg_match('/^([A-Za-z]+:\/\/.*?)\//', $base, $results); - $root = $results[1]; - return $root . $link; - } else { - return $base . $link; - } + return $this->combineBases($element->baseURI, $link); } /** @@ -193,7 +165,7 @@ if (! isset($this->entries[$offset])) { $entries = $this->model->getElementsByTagName($this->itemElement); if ($entries->length > 0) { - $xmlBase = $this->getBase($entries->item($offset)); + $xmlBase = $entries->item($offset)->baseURI; $this->entries[$offset] = new $this->itemClass( $entries->item($offset), $this, $xmlBase); } else { @@ -287,6 +259,63 @@ return 0; } + /** + * We need a couple of methods to access XHTML content stored in feeds. + * This is because we dereference all xml:base references before returning + * the element. This method handles the attributes. + * + * @param DOMElement $node + * @return string + */ + function processXHTMLAttributes($node) { + $return = ''; + foreach ($node->attributes as $attribute) { + if ($attribute->name == 'src' or $attribute->name == 'href') { + $attribute->value = $this->addBase($attribute->value, $attribute); + } + $return .= $attribute->name . '="' . $attribute->value .'" '; + } + return " " . trim($return); + } + + /** + * We need a couple of methods to access XHTML content stored in feeds. + * This is because we dereference all xml:base references before returning + * the element. This method recurs through the tree descending from the node + * and builds our string + * + * @param DOMElement $node + * @return string + */ + function traverseNode($node) + { + $content = ""; + + /* Add the opening of this node to the content */ + if ($node instanceof DOMElement) { + $content .= "<" . $node->tagName . + $this->processAttributes($node) . ">"; + } + + /* Process children */ + if ($node->hasChildNodes()) { + foreach ($node->childNodes as $child) { + $content .= $this->traverseNode($child); + } + } + + if ($node instanceof DOMText) { + $content .= $node->nodeValue; + } + + /* Add the closing of this node to the content */ + if ($node instanceof DOMElement) { + $content .= "</" . $node->tagName . ">"; + } + + return $content; + } + /** * Return an XML serialization of the feed, should it be required. Most * users however, will already have a serialization that they used when http://cvs.php.net/diff.php/pear/XML_Feed_Parser/tests/atomValues.php?r1=1.1&r2=1.2&ty=u Index: pear/XML_Feed_Parser/tests/atomValues.php diff -u pear/XML_Feed_Parser/tests/atomValues.php:1.1 pear/XML_Feed_Parser/tests/atomValues.php:1.2 --- pear/XML_Feed_Parser/tests/atomValues.php:1.1 Tue Oct 11 10:29:50 2005 +++ pear/XML_Feed_Parser/tests/atomValues.php Sat Oct 15 10:13:52 2005 @@ -1,6 +1,6 @@ <?php -require_once 'XML/Feed/Parser.php'; +require_once '../Parser.php'; require_once 'PHPUnit.php'; class XML_Feed_Parser_Atom_valueValidity_TestCase extends PHPUnit_TestCase @@ -22,7 +22,7 @@ function test_FeedTitle() { $value = "dive into mark"; - $this->assertEquals($this->feed->title, $value); + $this->assertEquals($value, $this->feed->title); } function test_feedSubtitle() @@ -103,9 +103,7 @@ function test_entryContent() { - $value = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> - <p><i>[Update: The Atom draft is finished.]</i></p> - </div>"; + $value = "<p><i>[Update: The Atom draft is finished.]</i></p>"; $content = trim($this->entry->content); $content = preg_replace("/\t/", " ", $content); $content = preg_replace("/( )+/", " ", $content);