Skip to main content

Bug Tracker

Side navigation

#15136 closed bug (invalid)

Opened June 06, 2014 06:56PM UTC

Closed June 24, 2014 08:31AM UTC

.wrap() clears text selection

Reported by: modular Owned by: modular
Priority: low Milestone: None
Component: unfiled Version: 1.11.1
Keywords: Cc:
Blocked by: Blocking:

Select some text.

Invoke a script that does wrap and unwrap.

See your selection changed or cleared.

Can be demonstrated on

select some text in the demo and press the button

Why this is a problem:

I encountered a script today on a website that uses wrap to create a 'share on twitter' icon (on mousedown).

This behavior is not intended, and a hidden 'feature' that should not be there.

    $.fn.share = function(options) {
        var option = $.extend($.fn.share.defaults,options);
            init : function(shareable) {
                var self = this;
                $.fn.share.defaults.shareable = shareable;
            getContent : function() {
                var current_url = window.location.href
                var selected_text = this.getSelection('string').substring(0,option.maxLength-(current_url.length+option.author_name.length+7));
                var text = encodeURIComponent('\\"'+selected_text+'\\" '+'--@'+option.author_name+' '+current_url)
                return '<a onclick="\\''+option.shareLink+text+'\\',\\'_''\\',\\'location=yes,height=570,width=520,scrollbars=yes,status=yes\\')"><i class="fa fa-twitter fa-lg"/></a>';
            getSelection : function(share) {
                    return (share=='string')?String(window.getSelection().getRangeAt(0)).replace(/\\s{2,}/g, ' '):window.getSelection().getRangeAt(0);
                else if(document.selection){
                    return (share=='string')?document.selection.createRange().text.replace(/\\s{2,}/g, ' '):document.selection.createRange();
            popOver : function() {
                if(this.getSelection('string').length < option.minLength)
                var data = this.getContent();
                var range = this.getSelection();
                var newNode = document.createElement("mark");
                $('.'+option.className).popover({trigger:'manual', placement: option.placement, html: true 
                , content:function(){
                        return data;
            destroy : function(){
    $.fn.share.defaults = {
        shareLink : "",
        minLength  : 2,
        maxLength  : 140,
        target     : "blank",
        className  : "share",
        placement  : "top",

Attachments (0)
Change History (5)

Changed June 06, 2014 06:59PM UTC by modular comment:1

When a script like that removes your selection every time you right click, you cannot access the context menu, and that is a bug.

Changed June 06, 2014 10:09PM UTC by dmethvin comment:2

owner: → modular
status: newpending
select some text in the demo and press the button

Wnat button? There are no buttons in the demos.

Changed June 07, 2014 06:37PM UTC by modular comment:3

status: pendingnew

Sorry, wrong link:

The button is on the unwrap demo page:

If you select one line, the selection will clear.

If you select 3 lines, the selection will first clear the first line, then reselect the first and clear the third.


Changed June 09, 2014 04:02PM UTC by dmethvin comment:4

status: newpending

The act of manipulating the DOM causes the browser to clear the selection. Is there something in particular you think jQuery is doing that it should not do to avoid clearing the selection? Have you been able to do that using pure DOM for example?

Changed June 24, 2014 08:31AM UTC by trac-o-bot comment:5

resolution: → invalid
status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!