Preserve element attributes on rename
authorAlexander Ebert <ebert@woltlab.com>
Wed, 30 Jan 2019 12:51:24 +0000 (13:51 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 30 Jan 2019 12:51:24 +0000 (13:51 +0100)
wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php

index 3947a06e1c0ab8cdd0e19472f2861bad372f9c44..57fce6e751a697477ed5af861dc18571d986c4f0 100644 (file)
@@ -163,10 +163,18 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
         * 
         * @param       \DOMElement     $element        old element
         * @param       string          $tagName        tag name for the new element
+        * @param       bool            $preserveAttributes     retain attributes for the new element
         * @return      \DOMElement     newly created element
         */
-       public function renameTag(\DOMElement $element, $tagName) {
+       public function renameTag(\DOMElement $element, $tagName, $preserveAttributes = false) {
                $newElement = $this->document->createElement($tagName);
+               if ($preserveAttributes) {
+                       /** @var \DOMNode $attribute */
+                       foreach ($element->attributes as $attribute) {
+                               $newElement->setAttribute($attribute->nodeName, $attribute->nodeValue);
+                       }
+               }
+               
                $element->parentNode->insertBefore($newElement, $element);
                while ($element->hasChildNodes()) {
                        $newElement->appendChild($element->firstChild);
@@ -223,7 +231,7 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
                $this->nodeData[] = [
                        'data' => $data,
                        'identifier' => $nodeIdentifier,
-                       'object' => $htmlNode
+                       'object' => $htmlNode,
                ];
        }