From: Aleksander Ɓukasz Date: Fri, 1 Aug 2014 08:57:41 +0000 (+0200) Subject: editor: selection fix - handle Zero Width Space in an empty text element X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/021d30c6418d5b38ed0b8baef80afbaed35ec612?ds=sidebyside editor: selection fix - handle Zero Width Space in an empty text element --- diff --git a/src/editor/modules/documentCanvas/canvas/selection.js b/src/editor/modules/documentCanvas/canvas/selection.js index fbae482..4de392b 100644 --- a/src/editor/modules/documentCanvas/canvas/selection.js +++ b/src/editor/modules/documentCanvas/canvas/selection.js @@ -121,21 +121,25 @@ return { /* globals window */ var nativeSelection = window.getSelection(), params = {}, - element; + element, anchorElement, focusElement; + if(nativeSelection.focusNode) { if(nativeSelection.isCollapsed && isText(nativeSelection.focusNode)) { + element = canvas.getDocumentElement(nativeSelection.focusNode); params = { type: 'caret', - element: canvas.getDocumentElement(nativeSelection.focusNode), - offset: nativeSelection.focusOffset + element: element, + offset: element.isEmpty() ? 0 : nativeSelection.focusOffset }; } else if(isText(nativeSelection.focusNode) && isText(nativeSelection.anchorNode)) { + anchorElement = canvas.getDocumentElement(nativeSelection.anchorNode); + focusElement = canvas.getDocumentElement(nativeSelection.focusNode); params = { type: 'textSelection', - anchorElement: canvas.getDocumentElement(nativeSelection.anchorNode), - anchorOffset: nativeSelection.anchorOffset, - focusElement: canvas.getDocumentElement(nativeSelection.focusNode), - focusOffset: nativeSelection.focusOffset + anchorElement: anchorElement, + anchorOffset: anchorElement.isEmpty() ? 0 : nativeSelection.anchorOffset, + focusElement: focusElement, + focusOffset: focusElement.isEmpty() ? 0 : nativeSelection.focusOffset }; } } else if((element = canvas.getCurrentNodeElement())) {