I have still found one bug when the selection is done using Shift + Left | Right arrow keys. If the text is selected that way and then f.e. the Bold button clicked, then:
— if the Shift + LEFT arrow key pressed, the first char from the
— if the Shift + RIGHT arrow pressed, the last char from the
right is removed.ChatGPT explanation:
What you're seeing is a timing issue rather than really a getSelection() bug. With keydown, the browser hasn't necessarily finished updating the DOM selection when your handler runs. setTimeout(..., 0 helps, but selection behavior can still vary depending on the element and browser. Still, it lets the browser finish applying the keyboard-driven selection before you inspect it.
So... The updated code for the function is as follows by now:
function getSelectionText(event) {
var selectedText='';
const element = event.target;
if (element instanceof HTMLTextAreaElement) {
setTimeout(() => { selectedText = element.value.substring(element.selectionStart, element.selectionEnd); }, 0);
}
else{
if (window.getSelection) {
selectedText=window.getSelection().toString();
}
}
return selectedText;
}
BTW I've checked the current version of form.js (which is now updated also here on this forum), and it also properly handles selection of the Shift + Home | End buttons combination.