[webkit-dev] Edit mode bug inserting <br> ?

Diego Taylor diego.taylor at gmail.com
Mon Jan 5 13:49:23 PST 2009


Hi,

I am intercepting the return key in edit mode and adding a <br> in the
cursor position, but the first time I press the return key at the end of the
text it doesn't move the cursor after the inserted br. Any help is really
appreciated, this is the source code (tested on Safari):

---------- o ----------
<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   </head>
   <body>
      <script language='javascript' type='text/javascript'>
         document.body.contentEditable = true;
         document.designMode = 'on';
         function onKeyPress(ev) {
            console.log('Key Pressed');
            if(ev.keyCode==13) {
               insertBR();
               ev.preventDefault();
            }
         }
         document.addEventListener('keypress', onKeyPress);
         function insertBR()
         {
            var s = window.getSelection();
            var r = s.getRangeAt(0);

            var br = document.createElement('br');
            r.insertNode(br);
            var newRange = document.createRange();
            newRange.setStartAfter(br);
            newRange.setEndAfter(br);


            s.removeAllRanges();
            r.detach();
            s.addRange(newRange);
         }
      </script>
   <div>Here is the place to play.</div>
   </body>
</html>
---------- o ----------

Thanks,
Diego
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20090105/5b0f8c3e/attachment.html>


More information about the webkit-dev mailing list