Actions
Defect #43662
openRace condition in addInlineAttachmentMarkup causes cursor movement before image markup is inserted
Status:
New
Priority:
Normal
Assignee:
-
Category:
Wiki
Target version:
-
Start date:
Due date:
% Done:
0%
Estimated time:
Resolution:
Affected version:
Description
Issue¶
In the addInlineAttachmentMarkup() function in attachments.js, there is a race condition where the cursor is moved before the image markup from getInlineAttachmentMarkup() is actually inserted into the textarea.
Root Cause¶
The getInlineAttachmentMarkup() function returns a Promise. However, the cursor movement code is placed outside the .then() callback, causing it to execute immediately without waiting for the promise to resolve:
getInlineAttachmentMarkup(file)
.then(imageMarkup => {
$textarea.val(...); // Asynchronous operation
})
// This executes BEFORE the above promise resolves
cursorPosition = $textarea.prop('selectionStart');
$textarea.prop({
'selectionStart': cursorPosition + 1,
'selectionEnd': cursorPosition + 1
});
Impact¶
- The cursor is positioned before the image markup is inserted
- The cursor position is calculated from the unchanged textarea value
- Image insertion appears broken or cursor behavior is inconsistent
Solution¶
Move the cursor movement code inside the .then() callback and calculate the correct position based on the inserted markup length:
getInlineAttachmentMarkup(file)
.then(imageMarkup => {
$textarea.val(
description.substring(0, cursorPosition)
+ (newLineBefore ? '\n' : '')
+ imageMarkup
+ (newLineAfter ? '\n' : '')
+ description.substring(cursorPosition, description.length)
);
// Move cursor after the inserted markup
var newCursorPosition = cursorPosition + (newLineBefore ? 1 : 0) + imageMarkup.length;
$textarea.prop({
'selectionStart': newCursorPosition + 1,
'selectionEnd': newCursorPosition + 1
});
})
I attached a patch for the latest commit to the trunk (source:/trunk@24291).
Files
No data to display
Actions