Skip to content

Commit

Permalink
fix #2858 RSS helper bug with namedspaced keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlaefer committed Jul 29, 2012
1 parent 5413b67 commit 19f0e72
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
52 changes: 52 additions & 0 deletions lib/Cake/Test/Case/View/Helper/RssHelperTest.php
Expand Up @@ -717,4 +717,56 @@ public function testElementAttrNotInParent() {
$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);
}
}
15 changes: 12 additions & 3 deletions lib/Cake/View/Helper/RssHelper.php
Expand Up @@ -256,6 +256,8 @@ public function item($att = array(), $elements = array()) {
$attrib = $val;
$val = null;
break;
default:
$attrib = $att;
}
if (!is_null($val) && $escape) {
$val = h($val);
Expand Down Expand Up @@ -312,7 +314,12 @@ public function elem($name, $attrib = array(), $content = null, $endTag = true)

$xml = '<' . $name;
if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"';
$xml .= ' xmlns';
if (is_array($namespace)) {
$xml .= ':' . $namespace['prefix'];
$namespace = $namespace['url'];
}
$xml .= '="' . $namespace . '"';
}
$bareName = $name;
if (strpos($name, ':') !== false) {
Expand All @@ -329,8 +336,10 @@ public function elem($name, $attrib = array(), $content = null, $endTag = true)
$xml .= '>' . $content . '</' . $name . '>';
$elem = Xml::build($xml, array('return' => 'domdocument'));
$nodes = $elem->getElementsByTagName($bareName);
foreach ($attrib as $key => $value) {
$nodes->item(0)->setAttribute($key, $value);
if ($attrib) {
foreach ($attrib as $key => $value) {
$nodes->item(0)->setAttribute($key, $value);
}
}
foreach ($children as $k => $child) {
$child = $elem->createElement($name, $child);
Expand Down

0 comments on commit 19f0e72

Please sign in to comment.