diff options
author | Olav Morken <olav.morken@uninett.no> | 2010-04-21 12:55:24 +0000 |
---|---|---|
committer | Olav Morken <olav.morken@uninett.no> | 2010-04-21 12:55:24 +0000 |
commit | 712914bea6ca9dda397576540b06d18aeb1cef2b (patch) | |
tree | 88281a2b398af69d8a626f762bf5c9a990a46c07 /lib/SAML2 | |
parent | 7c5b8004711ba667f2613865bb8e4edc2583e163 (diff) | |
download | simplesamlphp-712914bea6ca9dda397576540b06d18aeb1cef2b.zip simplesamlphp-712914bea6ca9dda397576540b06d18aeb1cef2b.tar.gz simplesamlphp-712914bea6ca9dda397576540b06d18aeb1cef2b.tar.bz2 |
SAML2_Utils: add copyElement function.
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2256 44740490-163a-0410-bde0-09ae8108e29a
Diffstat (limited to 'lib/SAML2')
-rw-r--r-- | lib/SAML2/Utils.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/SAML2/Utils.php b/lib/SAML2/Utils.php index afcd53e..d7cd5c4 100644 --- a/lib/SAML2/Utils.php +++ b/lib/SAML2/Utils.php @@ -138,6 +138,50 @@ class SAML2_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) { + + if ($parent === NULL) { + $document = new DOMDocument(); + } else { + $document = $parent->ownerDocument; + } + + $namespaces = array(); + for ($e = $element; $e !== NULL; $e = $e->parentNode) { + foreach (SAML2_Utils::xpQuery($e, './namespace::*') as $ns) { + $prefix = $ns->localName; + if ($prefix === 'xml' || $prefix === 'xmlns') { + continue; + } + $uri = $ns->nodeValue; + if (!isset($namespaces[$prefix])) { + $namespaces[$prefix] = $uri; + } + } + } + + $newElement = $document->importNode($element, TRUE); + if ($parent !== NULL) { + /* We need to append the child to the parent before we add the namespaces. */ + $parent->appendChild($newElement); + } + + foreach ($namespaces as $prefix => $uri) { + $newElement->setAttributeNS($uri, $prefix . ':__ns_workaround__', 'tmp'); + $newElement->removeAttributeNS($uri, '__ns_workaround__'); + } + + return $newElement; + } + + + /** * Parse a boolean attribute. * * @param DOMElement $node The element we should fetch the attribute from. |