Skip to content

Commit

Permalink
Merge pull request #11 from eclipxe13/development
Browse files Browse the repository at this point in the history
Corregir documentHasOverlappedNamespaces para entrar a moveNamespacesToRoot
  • Loading branch information
eclipxe13 committed Jan 11, 2022
2 parents f1bf2fb + 3cea981 commit f02eca7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Internal/XmlNamespaceMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ trait XmlNamespaceMethodsTrait
/**
* @param DOMDocument $document
* @return Generator&iterable<DOMNode>
* @phpstan-impure
* @internal The actual returned class is Generator<DOMNameSpaceNode> (undocumented)
*/
private function iterateNonReservedNamespaces(DOMDocument $document): Generator
{
Expand Down
8 changes: 6 additions & 2 deletions src/XmlDocumentCleaners/MoveNamespaceDeclarationToRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ public function clean(DOMDocument $document): void
private function documentHasOverlappedNamespaces(DOMDocument $document): bool
{
$prefixes = [];
/** $namespaceNode is a DOMNameSpaceNode, parentNode always exists */
foreach ($this->iterateNonReservedNamespaces($document) as $namespaceNode) {
/** @var DOMElement $ownerElement */
$ownerElement = $namespaceNode->parentNode;
/**
* $namespaceNode->nodeName => xmlns:cfdi
* $namespaceNode->nodeValue => http://www.sat.gob.mx/cfd/3
* $namespaceNode->parentNode => DOMElement where namespace definition is
*/
$currentDefinition = [
'namespace' => $namespaceNode->nodeValue,
'owner' => $namespaceNode->parentNode,
'owner' => $ownerElement,
];
if (! isset($prefixes[$namespaceNode->nodeName])) {
$prefixes[$namespaceNode->nodeName] = $currentDefinition;
continue;
}
if ($prefixes[$namespaceNode->nodeName] !== $currentDefinition) {
if ($ownerElement->hasAttribute($namespaceNode->nodeName)
&& $prefixes[$namespaceNode->nodeName] !== $currentDefinition) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function testMoveNamespaceDeclarationToRoot(): void

$expected = $this->createDocument(<<<XML
<r:root xmlns:r="http://tempuri.org/root"
xmlns:foo="http://tempuri.org/foo" xmlns:bar="http://tempuri.org/bar">
xmlns:foo="http://tempuri.org/foo"
xmlns:bar="http://tempuri.org/bar">
<foo:foo/>
<bar:bar/>
<xee/>
Expand Down

0 comments on commit f02eca7

Please sign in to comment.