Skip to content

Instantly share code, notes, and snippets.

@deathlyfrantic
Last active February 17, 2023 12:15
Show Gist options
  • Save deathlyfrantic/cd8d7ef8ba91544cdf06 to your computer and use it in GitHub Desktop.
Save deathlyfrantic/cd8d7ef8ba91544cdf06 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);
}
}
@Wargog
Copy link

Wargog commented Nov 1, 2018

Is there any chance you could MIT or Apache 2 license this snippet?

@JiveDig
Copy link

JiveDig commented Apr 27, 2021

Such a great solution. Thanks for much for this. My last fix for the lack of insertAfter() was waaaayy too complicated. Glad I search around this time around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment