I ran your example and it works fine. The problem shows when I take the output and save it to an SQL database and then try to display it in a web page. I am wondering it this is an issue with the database and the editor? I ask because there does not seem to be a problem with the TM in a regular textbox.
To get around the issue, I have added a button to insert <sup>TM</sup>.