diff --git a/src/FluentDOM/Element.php b/src/FluentDOM/Element.php index edd9222b..9a7cbba8 100644 --- a/src/FluentDOM/Element.php +++ b/src/FluentDOM/Element.php @@ -185,9 +185,9 @@ public function append($value) { ? $value : $this->ownerDocument->importNode($value) ); } elseif ($value instanceof Appendable) { - $namespaces = $this->ownerDocument->namespaces(); + $this->ownerDocument->namespaces()->stash(); $value->appendTo($this); - $this->ownerDocument->namespaces($namespaces); + $this->ownerDocument->namespaces()->unstash(); } elseif ($value instanceof \Closure && !$value instanceof \DOMNode) { $this->append($value()); } elseif (is_array($value)) { diff --git a/src/FluentDOM/Namespaces.php b/src/FluentDOM/Namespaces.php index 152b16c4..89a3b76b 100644 --- a/src/FluentDOM/Namespaces.php +++ b/src/FluentDOM/Namespaces.php @@ -17,6 +17,11 @@ class Namespaces implements NamespaceResolver, \ArrayAccess, \IteratorAggregate, 'xmlns' => 'http://www.w3.org/2000/xmlns/' ]; + /** + * @var array + */ + private $_stash = []; + /** * Namespaces constructor. * @param NULL|array|\Traversable $namespaces @@ -101,6 +106,20 @@ public function getIterator() { return new \ArrayIterator($this->_namespaces); } + /** + * Store current status on the stash + */ + public function stash() { + $this->_stash[] = $this->_namespaces; + } + + /** + * Restore last stashed status from the stash + */ + public function unstash() { + $this->_namespaces = array_pop($this->_stash); + } + /** * @return int */ diff --git a/tests/FluentDOM/Issues/73Test.php b/tests/FluentDOM/Issues/73Test.php new file mode 100644 index 00000000..8fc3c5d2 --- /dev/null +++ b/tests/FluentDOM/Issues/73Test.php @@ -0,0 +1,42 @@ +registerNamespace('atom', 'urn:atom'); + $result = $_( + 'atom:feed', + $_->each( + [1, 2, 3], + function($number) use ($_) { + return $_( + 'atom:entry', + $_('atom:title', $number) + ); + } + ) + )->getDocument(); + $this->assertXmlStringEqualsXmlString( + ' + + + 1 + + + 2 + + + 3 + + ', + (string)$result->saveXML() + ); + } + } +} \ No newline at end of file