Only detach the incoming elements to replaceWith if they're DOM nodes. Fixes #5986.