Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13110 closed Bug (invalid)

Bookmarks sometimes cause cursor to be placed incorrectly

Reported by: Ehman Owned by:
Priority: Normal Milestone:
Component: Core : Selection Version:
Keywords: Cc: arron.caruth@…

Description

Our product uses CKEditor 4.4.6 for email creation.

We recently had to begin using bookmarks, because (due to an add-on) when saving a draft the body of the email needs to be removed and replaced.

Due to the nature of the add-on, the following code is not exactly the same, but serves the same purpose.

function removeUnusableData () {
  var messageBody = getMessageBody(); //gets the body from the cke_1_contents iframe as html
  messageBody = $("<div>" + messageBody + "</div>");
  messageBody.find('div pre:contains("Something related to the add-on")').parents('div:first').remove();
  messageBody.find('div pre:contains("Something else related to the add-on")').parents('div:first').remove();
  return messageBody.html().trim();
}

//replaces the email body with the passed string
function swapBodyInput (str) {
  $('#cke_1_contents iframe').contents().find('body').html(str);
  $('#BodyHTML').val(str);
}

//str is only passed in if the add-on is enabled
//the bug may occur whether or not the add-on is enabled
function saveDraft (str) {
  var bookmarks = htmlEditor.getSelection().createBookmarks2();
  if (str) { //This is only hit if the add-on is running
    swapBodyInput(str); 
  }
  //hits when the add-on is running and CKEditor is enabled 
  else if (!str && isHTML) { //users can choose to edit in plain/text if they so desire
    swapBodyInput(removeUnusableData()); 
  } 

  $.post(formURL, $('form').serialize(), saveDraftCallback, 'json');

  //by the time we reach here, the body has already been removed and replaced
  htmlEditor.getSelection().selectBookmarks(bookmarks); 
} 

Every now and then, the cursor will not be returned to the correct location. It's not every time, so I have yet to nail down precise steps to reproduce the issue. However, it has been reported repeatedly by one person, and a couple of times by another. I have seen it happen once while typing an email.

Change History (5)

comment:1 Changed 4 years ago by Ehman

Summary: Bookmarks sometimes cause cursor to placed incorrectlyBookmarks sometimes cause cursor to be placed incorrectly

comment:2 Changed 4 years ago by Ehman

One thing I will add. After further testing, I found that the bookmarks do not hold the end position of the cursor.

If I have used "End" to place my cursor at the end of a line and begin scrolling up or down a body of text with the up or down arrow keys and the saveDraft function hits while on a line shorter than the line I went to the end of, the cursor will then be at that position moving up or down, instead of the end of each line.

Last edited 4 years ago by Ehman (previous) (diff)

comment:3 Changed 4 years ago by Ehman

The issues occurs if the user is holding the "Shift" key when the saveDraft function is called.

comment:4 Changed 4 years ago by Jakub Ś

Keywords: bookmarks getSelection createBookmarks createBookmarks2 removed
Resolution: invalid
Status: newclosed
Version: 4.4.6
  1. bookmark2 remembers the position of the cursor in element which may be in element which may be in another element... In short, it remembers address of the element and cursor position in it. You have written "by the time we reach here, the body has already been removed and replaced". Please note that if structure of document changes (the structure before the bookmark) then bookmark will not be recreated. I believe this is what is happening in your plugin.
    Please also note that this is not a bug but how CKEditor works.
  1. Replying to comment:2 - createbookmarks2 doesn't use normalize function so it should not change selection. Please note that inside that function you also use jQuery so it is hard to say what is causing this.

I closing this issue as I think it is invalid. @jpehman if you don’t agree, please prepare simplified plugin (plus replication steps) which uses CKEditor API only and shows this problem in default editor.

comment:5 Changed 4 years ago by Ehman

j.swiderski,

Thank you very much for looking into this issue for me. I can see how this would not be a CKEditor bug. I ended up completely bypassing the usage of bookmarks as much as possible to get around the issue we were having with the cursor jumping around the text area.

Once again, I appreciate you looking into this.

Note: See TracTickets for help on using tickets.
© 2003 – 2019 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy