Snap selection to word.

Currently extends both sides each time you snap. :(

HTML

<button onClick="javascript:snapSelectionToWord();">Snap</button>
<div contenteditable="true" onmouseup="snapSelectionToWord()">
    <p>Hello, <b>her</b>e is some &nbsp; <i>nice text</i>. Please try selecting some text.</p>
    <p><b><u>Then, click the [Snap] button a few times just for fun.</u></b></p>
    <p>उत्तर प्रदेश: बदलेगा बाबा साहेब आंबेडकर का नाम, जोड़ा जाएगा पिता का नाम 'रामजी' भी

द वायर स्टाफ

प्रमुख सचिव द्वारा जारी निर्देश में कहा गया है कि संविधान की आठवीं सूची में डॉ. भीमराव आंबेडकर का नाम ' डॉ। भीमराव रामजी आंबेडकर ' लिखा हुआ है। राज्य सरकार के समस्त अभिलेखों में अब उनका सही नाम दर्ज होगा।

लखनऊ : उत्तर प्रदेश में अब भारत के संविधान निर्माता बाबा साहेब भीमराव आंबेडकर का नाम बदले जाने की तैयारी है।

डॉ. आंबेडकर के नाम के साथ अब उनके पिता का भी नाम जोड़ा जाएगा। उनके पिता का नाम 'रामजी मालोजी सकपाल ' था। अब आंबेडकर के नाम में आधिकारिक रूप से उनके पिता के नाम में से 'रामजी' शब्द जोड़े जाने की तैयारी है।

आज तक की रिपोर्ट के अनुसार, राज्यपाल राम नाइक की सलाह के बाद योगी सरकार ने ये फैसला लिया है। अब बाबा साहेब का नाम डॉ. भीमराव रामजी आंबेडकर होगा।

टाइम्स ऑफ इंडिया की रिपोर्ट के अनुसार, राज्य सरकार ने बुधवार को सभी विभागों और लखनऊ व इलाहाबाद की हाईकोर्ट बेंचों को अपने दस्तावेजों और रिकॉर्ड में डॉ. भीमराव आंबेडकर के प्रयोग को डॉ. भीमराव रामजी आंबेडकर के साथ स्थानांतरित करने का आदेश दिया, जैसा कि संविधान के पृष्ठों पर उन्होंने हस्ताक्षर किया था। .</p>
    <p>Etiam lacinia odio in nisi porttitor dapibus. Maecenas vitae nulla purus, a eleifend est. Vivamus ut quam nibh, euismod imperdiet elit. Praesent nisl sapien, suscipit sit amet cursus vehicula, facilisis eu ligula. Nunc dapibus erat sed odio elementum non fringilla leo semper. Fusce vitae tellus sem. Nam sit amet euismod ipsum. Sed pharetra tempor pharetra. Etiam congue elit quis nisi pretium eget facilisis dolor malesuada. Etiam blandit velit eu purus semper ut malesuada eros rutrum. Morbi ullamcorper pretium metus, at pretium nisl lacinia sit amet. In pellentesque porttitor risus et sodales. In hac...

CSS

p {margin-bottom: 1.3em;}

JavaScript

function snapSelectionToWord() {
    var sel;

    // Check for existence of window.getSelection() and that it has a
    // modify() method. IE 9 has both selection APIs but no modify() method.
    if (window.getSelection && (sel = window.getSelection()).modify) {
        sel = window.getSelection();
       // if (!sel.isCollapsed) {

            // Detect if selection is backwards
            var range = document.createRange();
            range.setStart(sel.anchorNode, sel.anchorOffset);
            range.setEnd(sel.focusNode, sel.focusOffset);
            var backwards = range.collapsed;
            range.detach();

            // modify() works on the focus of the selection
            var endNode = sel.focusNode, endOffset = sel.focusOffset;
            sel.collapse(sel.anchorNode, sel.anchorOffset);
            if (backwards) {
                //sel.modify("move", "backward", "character");
                sel.modify("move", "forward", "word");
                sel.extend(endNode, endOffset);
                //sel.modify("extend", "forward", "character");
                sel.modify("extend", "backward", "word");

            } else {
                sel.modify("move", "forward", "character");
                sel.modify("move", "backward", "word");
                sel.extend(endNode, endOffset);
                sel.modify("extend", "backward", "character");
                sel.modify("extend", "forward", "word");
            }
        //}
    } else if ( (sel = document.selection) && sel.type != "Control") {
        var textRange = sel.createRange();
        if (textRange.text) {
            textRange.expand("word");
            // Move the end back to not include the word's trailing space(s),
            // if necessary
            while (/\s$/.test(textRange.text)) {
                textRange.moveEnd("character", -1);
            }
            textRange.select();
        }
    }
}