Schlaefer 2012-07-29 14:24:46 +02:00
parent 5413b672eb
commit 19f0e72d58
2 changed files with 64 additions and 3 deletions

View file

@ -717,4 +717,56 @@ class RssHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }
public function testElementNamespaceWithoutPrefix() {
$item = array(
'creator' => 'Alex',
);
$attributes = array(
'namespace' => 'http://link.com'
);
$result = $this->Rss->item($attributes, $item);
$expected = array(
'item' => array(
'xmlns' => 'http://link.com'
),
'creator' => array(
'xmlns' => 'http://link.com'
),
'Alex',
'/creator',
'/item'
);
$this->assertTags($result, $expected, true);
}
public function testElementNamespaceWithPrefix() {
$item = array(
'title' => 'Title',
'creator' => 'Alex'
);
$attributes = array(
'namespace' => array(
'prefix' => 'dc',
'url' => 'http://link.com'
)
);
$result = $this->Rss->item($attributes, $item);
$expected = array(
'item' => array(
'xmlns:dc' => 'http://link.com'
),
'title' => array(
'xmlns:dc' => 'http://link.com'
),
'Title',
'/title',
'creator' => array(
'xmlns:dc' => 'http://link.com'
),
'Alex',
'/creator',
'/item'
);
$this->assertTags($result, $expected, true);
}
} }

View file

@ -256,6 +256,8 @@ class RssHelper extends AppHelper {
$attrib = $val; $attrib = $val;
$val = null; $val = null;
break; break;
default:
$attrib = $att;
} }
if (!is_null($val) && $escape) { if (!is_null($val) && $escape) {
$val = h($val); $val = h($val);
@ -312,7 +314,12 @@ class RssHelper extends AppHelper {
$xml = '<' . $name; $xml = '<' . $name;
if (!empty($namespace)) { if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"'; $xml .= ' xmlns';
if (is_array($namespace)) {
$xml .= ':' . $namespace['prefix'];
$namespace = $namespace['url'];
}
$xml .= '="' . $namespace . '"';
} }
$bareName = $name; $bareName = $name;
if (strpos($name, ':') !== false) { if (strpos($name, ':') !== false) {
@ -329,9 +336,11 @@ class RssHelper extends AppHelper {
$xml .= '>' . $content . '</' . $name . '>'; $xml .= '>' . $content . '</' . $name . '>';
$elem = Xml::build($xml, array('return' => 'domdocument')); $elem = Xml::build($xml, array('return' => 'domdocument'));
$nodes = $elem->getElementsByTagName($bareName); $nodes = $elem->getElementsByTagName($bareName);
if ($attrib) {
foreach ($attrib as $key => $value) { foreach ($attrib as $key => $value) {
$nodes->item(0)->setAttribute($key, $value); $nodes->item(0)->setAttribute($key, $value);
} }
}
foreach ($children as $k => $child) { foreach ($children as $k => $child) {
$child = $elem->createElement($name, $child); $child = $elem->createElement($name, $child);
$nodes->item(0)->appendChild($child); $nodes->item(0)->appendChild($child);