Skip to content

Commit

Permalink
Remove copyElement utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Nov 7, 2022
1 parent e2177e9 commit 318bdfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 57 deletions.
19 changes: 16 additions & 3 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public function __construct(DOMElement $xml)
$this->setLocalName($xml->localName);
$this->setNamespaceURI($xml->namespaceURI);
$this->setPrefix($xml->prefix);

$this->xml = Utils::copyElement($xml);
$this->xml = $xml;
}


Expand Down Expand Up @@ -272,6 +271,20 @@ public static function fromXML(DOMElement $xml): static
*/
public function toXML(DOMElement $parent = null): DOMElement
{
return Utils::copyElement($this->xml, $parent);
if ($parent === null) {
$doc = DOMDocumentFactory::create();
} else {
$doc = $parent->ownerDocument;
Assert::notNull($doc);
}

if ($parent === null) {
$parent = $doc;
}

/** @psalm-var \DOMDocument $doc */
$parent->appendChild($doc->importNode($this->getXML(), true));

return $doc->documentElement;
}
}
54 changes: 0 additions & 54 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

use DateTimeImmutable;
use DOMElement;
use DOMNode;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Utils\XPath;

use function intval;
use function gmmktime;
use function preg_match;
use function trim;

/**
Expand All @@ -22,55 +17,6 @@
*/
class Utils
{
/**
* Make an exact copy the specific \DOMElement.
*
* @param \DOMElement $element The element we should copy.
* @param \DOMElement|null $parent The target parent element.
* @return \DOMElement The copied element.
*/
public static function copyElement(DOMElement $element, DOMElement $parent = null): DOMElement
{
if ($parent === null) {
$document = DOMDocumentFactory::create();
} else {
$document = $parent->ownerDocument;
Assert::notNull($document);
/** @psalm-var \DOMDocument $document */
}

$namespaces = [];
for ($e = $element; $e instanceof DOMNode; $e = $e->parentNode) {
$xpCache = XPath::getXPath($e);
foreach (XPath::xpQuery($e, './namespace::*', $xpCache) as $ns) {
$prefix = $ns->localName;
if ($prefix === 'xml' || $prefix === 'xmlns') {
continue;
} elseif (!isset($namespaces[$prefix])) {
$namespaces[$prefix] = $ns->nodeValue;
}
}
}

/** @var \DOMElement $newElement */
$newElement = $document->importNode($element, true);
if ($parent === null) {
$parent = $document;
}
/* We need to append the child to the parent before we add the namespaces. */
$parent->appendChild($newElement);

foreach ($namespaces as $prefix => $uri) {
/** @psalm-suppress PossiblyNullArgument */
$newElement->setAttributeNS($uri, $prefix . ':__ns_workaround__', 'tmp');
/** @psalm-suppress PossiblyNullArgument */
$newElement->removeAttributeNS($uri, '__ns_workaround__');
}

return $newElement;
}


/**
* Extract localized strings from a set of nodes.
*
Expand Down

0 comments on commit 318bdfd

Please sign in to comment.