function insertNode($path, $node) { $children = $node->childNodes; $virtual = true; $title = ''; $text = ''; $pos = 0; while ($pos < $children->length && domNodeIsEmpty($children->item($pos))) { $pos++; } if ($pos < $children->length) { $item = $children->item($pos); if ($item->nodeType == XML_ELEMENT_NODE && $item->tagName == 'h') { $title = trim($item->firstChild->data); $virtual = false; $pos++; } } while ($pos < $children->length && domNodeIsEmpty($children->item($pos))) { $pos++; } if ($pos < $children->length) { $item = $children->item($pos); if ($item->nodeType == XML_TEXT_NODE) { $text = trim($item->data); $virtual = false; $pos++; } } if ($path != '') { if ($node->hasAttribute('delete')) { if (!$node->hasAttribute('id') || !$virtual) { die('Malformed XML: delete tag has no id or is virtual'); } $newPath = deleteParagraph(pathCombine($path, 1, $node->getAttribute('id'))); } else { if ($node->hasAttribute('id')) { if (!$virtual) { $newPath = editParagraph(pathCombine($path, 1, $node->getAttribute('id')), $title, $text); } else { $newPath = pathCombine($path, 1, $node->getAttribute('id')); } } else { if (!$virtual) { $newPath = addParagraph($path, 't', $title, $text); } else { $newPath = addParagraph($path, 'f'); } } } } $lastPath = pathCombine($newPath, 0, '0'); while ($pos < $children->length && domNodeIsEmpty($children->item($pos))) { $pos++; } while ($pos < $children->length) { $lastPath = insertNode($lastPath, $children->item($pos++)); while ($pos < $children->length && domNodeIsEmpty($children->item($pos))) { $pos++; } } return $newPath; }
function insertNodes($nodes, $pos, $subRel = false, $subPath = '') { $prevChild = false; while ($pos < $nodes->length && domNodeIsEmpty($nodes->item($pos))) { $pos++; } while ($pos < $nodes->length) { if ($prevChild === false) { $prevChild = insertNode($subRel, $subPath, $nodes->item($pos++)); } else { $prevChild = insertNode($prevChild, '', $nodes->item($pos++)); } while ($pos < $nodes->length && domNodeIsEmpty($nodes->item($pos))) { $pos++; } } return $prevChild; }