Skip to content

Instantly share code, notes, and snippets.

@strarsis
Forked from deathlyfrantic/dom-insert-after.php
Created March 26, 2018 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strarsis/d8b5410e12b274b2f987429accc10ae5 to your computer and use it in GitHub Desktop.
Save strarsis/d8b5410e12b274b2f987429accc10ae5 to your computer and use it in GitHub Desktop.
why doesn't the DOM spec include an insertAfter method, srsly guys
<?php
/** Inserts a new node after a given reference node. Basically it is the complement to the DOM specification's
* insertBefore() function.
* @param \DOMNode $newNode The node to be inserted.
* @param \DOMNode $referenceNode The reference node after which the new node should be inserted.
* @return \DOMNode The node that was inserted.
*/
function insertAfter(\DOMNode $newNode, \DOMNode $referenceNode)
{
if($referenceNode->nextSibling === null) {
return $referenceNode->parentNode->appendChild($newNode);
} else {
return $referenceNode->parentNode->insertBefore($newNode, $referenceNode->nextSibling);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment