Skip to content

Instantly share code, notes, and snippets.

@raym
Created November 6, 2014 04:47
Show Gist options
  • Save raym/894f773c26f5a071a314 to your computer and use it in GitHub Desktop.
Save raym/894f773c26f5a071a314 to your computer and use it in GitHub Desktop.
Determine direction currently selected text was selected (rtl vs ltr)
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