title: Getting the position of cursor in contentEditable return wrong position
body: <p>I am trying to get the cursor position when I click a contenteditable element, I've already found the same question answered <a href="https://stackoverflow.com/questions/6846230/coordinates-of-selected-text-in-browser-page">here</a> and it works most of the time, but there is an issue in the following case :</p>
  
  <p><a href="http://jsfiddle.net/mody5/ts5pjoqd/4/" rel="nofollow noreferrer">http://jsfiddle.net/mody5/ts5pjoqd/4/</a></p>
  
  <p>I added a margin of 5px for the contentEditable.</p>
  
  <p>If you put the cursor at the beginning of the first line, it show the left position of 5px which is correct, if you put the cursor at the beginning of the second line it shows a big number than 5px</p>
  
  <p><em>Note : the margin of 5px is added only for clarity reason, even without a margin the issue is still there</em></p>
  
  <p>Is there a fix for that ?</p>
  
  <p><strong>the code of jsfiddle :</strong></p>
  
  <p>HTML :</p>
  
  <pre><code>&lt;div contenteditable="true" style="margin:5px;"&gt;
  &lt;span&gt;Lorem &lt;em&gt;ipsum&lt;/em&gt; dolor&lt;/span&gt; sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  &lt;code&gt;Lorem ipsum&lt;/code&gt; dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  &lt;br&gt;
  &lt;strong style="font-weight:bold;"&gt;Coords :&lt;/strong&gt; &lt;span id="coords"&gt;&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>javascript :</p>
  
  <pre><code>function getSelectionCoords(win) {
      win = win || window;
      var doc = win.document;
      var sel = doc.selection, range, rects, rect;
      var x = 0, y = 0;
      if (sel) {
          if (sel.type != "Control") {
              range = sel.createRange();
              range.collapse(true);
              x = range.boundingLeft;
              y = range.boundingTop;
          }
      } else if (win.getSelection) {
          sel = win.getSelection();
          if (sel.rangeCount) {
              range = sel.getRangeAt(0).cloneRange();
              if (range.getClientRects) {
                  range.collapse(true);
                  rects = range.getClientRects();
                  if (rects.length &gt; 0) {
                      rect = rects[0];
                  }
                  x = rect.left;
                  y = rect.top;
              }
              // Fall back to inserting a temporary element
              if (x == 0 &amp;&amp; y == 0) {
                  var span = doc.createElement("span");
                  if (span.getClientRects) {
                      // Ensure span has dimensions and position by
                      // adding a zero-width space character
                      span.appendChild( doc.createTextNode("\u200b") );
                      range.insertNode(span);
                      rect = span.getClientRects()[0];
                      x = rect.left;
                      y = rect.top;
                      var spanParent = span.parentNode;
                      spanParent.removeChild(span);
  
                      // Glue any broken text nodes back together
                      spanParent.normalize();
                  }
              }
          }
      }
      return { x: x, y: y };
  }
  
  document.onmouseup = function() {
      var coords = getSelectionCoords(window);
      document.getElementById("coords").innerHTML = coords.x + ", " + coords.y;
  };
  </code></pre>