Add check for ensuring an element has a parentNode before attempting to replace it with a substitued element. Add tests for element substibution.