diff --git a/lib/codemirror.js b/lib/codemirror.js
index aa125b66e27a9773a0b4ff2168c84f1426bd0a8a..292eda966dc5540ec209e44e6505caf1968b781a 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -1971,7 +1971,7 @@
       if (found)
         return badPos(Pos(found.line, found.ch + dist), bad);
       else
-        dist += after.textContent.length;
+        dist += before.textContent.length;
     }
   }