title: Cursor gets stuck in superscript or subscript on replacement
body: <p>I have two button converting selected text to superscript or subscript.
  when i convert the text to superscript then my cursor gets stuck in the superscript line-height and doesnt come back to normal line-height and same error is for subscript.Please if someone could help me resolve this error.
  Thank You.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getText(tag) {
    var text = document.getElementsByClassName("div-input")[0].innerHTML;
    console.log(text);
    var selected = "";
    if (window.getSelection) {
      selected = window.getSelection().toString();
    } else if (document.selection &amp;&amp; document.selection.type != "Control") {
      selected = document.selection.createRange().text;
    }
    var newnode = document.createElement(tag);
    var node = document.createTextNode(selected);
    newnode.appendChild(node);
  
    replaceSelectedText(newnode);
  }
  
  function replaceSelectedText(replacementText) {
    var sel, range;
    if (window.getSelection) {
      sel = window.getSelection();
      if (sel.rangeCount) {
        range = sel.getRangeAt(0);
        range.deleteContents();
        range.insertNode(replacementText);
      }
    } else if (document.selection &amp;&amp; document.selection.createRange) {
      range = document.selection.createRange();
      range.text = replacementText;
    }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  
  &lt;head&gt;
    &lt;title&gt;Question Paper Website&lt;/title&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
    &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;script src="main.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;button class="superscript btn" onclick="getText('sup')"&gt;S&lt;sup&gt;2&lt;/sup&gt;&lt;/button&gt;
    &lt;button class="subscript btn" onclick="getText('sub')"&gt;S&lt;sub&gt;2&lt;/sub&gt;&lt;/button&gt;
    &lt;div contentEditable=true class="div-input" id="questionData" style="display:block;height:300px;width:300px;border:1px solid;"&gt;&lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>