You are here

private function cf_dom::p_change_element in Common Functionality 7.2

Changes the element from one type to another.

Parameters

DOMNode $element: The element whose type will be changed.

string $type: The new element type to use.

Return value

bool The changed element on success, FALSE otherwise.

1 call to cf_dom::p_change_element()
cf_dom::change_element in modules/cf_dom/classes/cf_dom.php
Changes the element from one type to another.

File

modules/cf_dom/classes/cf_dom.php, line 547
Provides the cf_dom handling class.

Class

cf_dom
The cf_dom class assists in setting up and managing the custom dom object.

Code

private function p_change_element($element, $type) {
  $parent = $element->parentNode;
  $new = $this->dom
    ->createElement($type);
  if (!$new instanceof DOMNode) {
    return FALSE;
  }
  if ($element
    ->hasAttributes()) {
    foreach ($element->attributes as $attribute) {
      $new
        ->setAttribute($attribute->name, $attribute->value);
    }
  }
  if ($element
    ->hasChildNodes()) {
    foreach ($element->childNodes as $child) {
      $new
        ->appendChild($child
        ->cloneNode(TRUE));
    }
  }
  if ($parent instanceof DOMNode) {
    $child = $parent
      ->replaceChild($new, $element);
  }
  else {
    $this->dom
      ->appendChild($element);
    $child = $this->dom
      ->replaceChild($new, $element);
    if ($child instanceof DOMNode) {
      $parent = $child->parentNode;
      if ($parent instanceof DOMNode) {
        $this->dom
          ->removeChild($child);
      }
    }
    else {
      $this->dom
        ->removeChild($element);
    }
  }
  if ($child instanceof DOMNode) {
    return $child;
  }
  return FALSE;
}