Login Register

Migrate: dojo.html.clearSelection

I'm in the process of migrating from Dojo 0.4.3 to Dojo 1.1.1.

I cannot find a replacement for dojo.html.clearSelection in Dojo 1.1.1. I looked at the html/select.js source in Dojo 0.4.3. The function is more than two lines, so I thought I would ask before attempting to make it work with Dojo 1.1.1.

Sample code

For anyone else who is missing this feature, here is a hack:

/**
 * This method copied from the dojo/html/selection.js file (v0.4.3).
 */

libClearSelection = function(){
        // summary: deselect the current selection to make it empty
        var _window = dojo.global;
        var _document = dojo.doc;
        try{
                if(_window["getSelection"]){
                        if(dojo.isSafari){
                                // pulled from WebCore/ecma/kjs_window.cpp, line 2536
                                _window.getSelection().collapse();
                        }else{
                                _window.getSelection().removeAllRanges();
                        }
                }else if(_document.selection){
                        if(_document.selection.empty){
                                _document.selection.empty();
                        }else if(_document.selection.clear){
                                _document.selection.clear();
                        }
                }
                return true;
        }catch(e){
                console.debug(e);
                return false;
        }
}