Skip to content

Commit

Permalink
Fixing XmlHelper overwriting its Xml property when creating an open e…
Browse files Browse the repository at this point in the history
…lement with elem()

Fixes #1642
  • Loading branch information
markstory committed Apr 16, 2011
1 parent 4395e8a commit 12c2456
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 7 additions & 4 deletions cake/libs/view/helpers/xml.php
Expand Up @@ -38,6 +38,8 @@ class XmlHelper extends AppHelper {
*/ */
var $encoding = 'UTF-8'; var $encoding = 'UTF-8';


var $Xml;
var $XmlElement;
/** /**
* Constructor * Constructor
* *
Expand Down Expand Up @@ -136,7 +138,7 @@ function elem($name, $attrib = array(), $content = null, $endTag = true) {
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag)); $out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));


if (!$endTag) { if (!$endTag) {
$this->Xml =& $elem; $this->XmlElement =& $elem;
} }
return $out; return $out;
} }
Expand All @@ -148,9 +150,10 @@ function elem($name, $attrib = array(), $content = null, $endTag = true) {
* @access public * @access public
*/ */
function closeElem() { function closeElem() {
$name = $this->Xml->name(); $elem = (empty($this->XmlElement)) ? $this->Xml : $this->XmlElement;
if ($parent =& $this->Xml->parent()) { $name = $elem->name();
$this->Xml =& $parent; if ($parent =& $elem->parent()) {
$this->XmlElement =& $parent;
} }
return '</' . $name . '>'; return '</' . $name . '>';
} }
Expand Down
13 changes: 12 additions & 1 deletion cake/tests/cases/libs/view/helpers/xml.test.php
Expand Up @@ -159,7 +159,8 @@ function testRenderElementWithNamespace() {
$result .= $this->Xml->closeElem(); $result .= $this->Xml->closeElem();
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/**
/**
* testRenderElementWithComplexContent method * testRenderElementWithComplexContent method
* *
* @access public * @access public
Expand Down Expand Up @@ -284,4 +285,14 @@ function testHeader() {
$expected = '<?xml encoding="UTF-8" someOther="value" ?>'; $expected = '<?xml encoding="UTF-8" someOther="value" ?>';
$this->assertIdentical($result, $expected); $this->assertIdentical($result, $expected);
} }

/**
* test that calling elem() and then header() doesn't break
*
* @return void
*/
function testElemThenHeader() {
$this->Xml->elem('test', array(), 'foo', false);
$this->assertPattern('/<\?xml/', $this->Xml->header());
}
} }

0 comments on commit 12c2456

Please sign in to comment.