Created
November 6, 2014 04:47
-
-
Save raym/894f773c26f5a071a314 to your computer and use it in GitHub Desktop.
Determine direction currently selected text was selected (rtl vs ltr)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var selectionIsRTL = function(sel) { | |
sel = sel || document.getSelection(); | |
position = sel.anchorNode.compareDocumentPosition(sel.focusNode); | |
return ( | |
// nodes are same if position === 0 | |
// selection direction is RTL if anchorOffset > focusOffset | |
!position && sel.anchorOffset > sel.focusOffset || | |
// focus preceeds anchor | |
position === Node.DOCUMENT_POSITION_PRECEDING | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment