Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created January 13, 2016 18:23
Show Gist options
  • Save lovasoa/e7ac08aa4f0d07ab5933 to your computer and use it in GitHub Desktop.
Save lovasoa/e7ac08aa4f0d07ab5933 to your computer and use it in GitHub Desktop.
Forbid "incoherent" selection in a document (selection that can not be expressed as a new tag in the DOM tree)
document.onmousemove = function(evt) {
var s = window.getSelection();
if (s.rangeCount === 0) return;
while(s.rangeCount > 1) s.removeRange(s.getRangeAt(1));
r = s.getRangeAt(0);
if (r.startContainer.parentNode !== r.endContainer.parentNode) {
r.setEnd(r.startContainer, r.startContainer.length);
evt.preventDefault();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment