[Fix]: Follows more closely latest jQuery's code for .replaceWith() that deals with more edge case like replacing detached elements or replacing with text.