Skip to content

Commit

Permalink
Fixed #73, Creator forgets namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Jul 29, 2017
1 parent c522ccd commit 32063e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/FluentDOM/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
19 changes: 19 additions & 0 deletions src/FluentDOM/Namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/FluentDOM/Issues/73Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace FluentDOM {

use FluentDOM\Nodes\Creator;

require_once(__DIR__.'/../TestCase.php');

class Issue73Test extends TestCase {

public function testCreatorDoesNotForgetNamespaces() {
$_ = new Creator();
$_->registerNamespace('atom', 'urn:atom');
$result = $_(
'atom:feed',
$_->each(
[1, 2, 3],
function($number) use ($_) {
return $_(
'atom:entry',
$_('atom:title', $number)
);
}
)
)->getDocument();
$this->assertXmlStringEqualsXmlString(
'<?xml version="1.0"?>
<atom:feed xmlns:atom="urn:atom">
<atom:entry>
<atom:title>1</atom:title>
</atom:entry>
<atom:entry>
<atom:title>2</atom:title>
</atom:entry>
<atom:entry>
<atom:title>3</atom:title>
</atom:entry>
</atom:feed>',
(string)$result->saveXML()
);
}
}
}

0 comments on commit 32063e1

Please sign in to comment.