Last active
February 17, 2023 12:15
-
-
Save deathlyfrantic/cd8d7ef8ba91544cdf06 to your computer and use it in GitHub Desktop.
why doesn't the DOM spec include an insertAfter method, srsly guys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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
Is there any chance you could MIT or Apache 2 license this snippet?