Skip to content

Commit

Permalink
Fix attributes pollution #56 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-zahariev authored and freekmurze committed Feb 20, 2018
1 parent a6e1cf9 commit 799ac12
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected function addCollectionNode(DOMElement $element, $value)
return;
}

$child = $element->cloneNode();
$child = new DOMElement($element->tagName);
$element->parentNode->appendChild($child);
$this->convertElement($child, $value);
}
Expand All @@ -177,7 +177,7 @@ protected function addSequentialNode(DOMElement $element, $value)
return;
}

$child = $element->cloneNode();
$child = new DOMElement($element->tagName);
$child->nodeValue = htmlspecialchars($value);
$element->parentNode->appendChild($child);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,18 @@ public function and_cdata_values_can_also_be_set_in_simplexmlelement_style()
],
]));
}

/** @test */
public function it_doesnt_pollute_attributes_in_collection_and_sequential_nodes()
{
$this->assertMatchesSnapshot(ArrayToXml::convert([
'books' => [
'book' => [
['name' => 'A', '@attributes' => ['z' => 1]],
['name' => 'B'],
['name' => 'C'],
]
]
]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php return '<?xml version="1.0"?>
<root><books><book z="1"><name>A</name></book><book><name>B</name></book><book><name>C</name></book></books></root>
';

0 comments on commit 799ac12

Please sign in to comment.