jquery-ui icon indicating copy to clipboard operation
jquery-ui copied to clipboard

Fixes removing the namespace from an event name

Open Eydamos opened this issue 3 years ago • 5 comments

Bootstrap uses events which need to include a namespace. E.g. the event name to bind to showing a modal is show.bs.modal. Just setting the event on show will not work.

Example:

$(document).on('show', '.modal', function() { console.log('this will never get fired')});
$(document).on('show.bs.modal', '.modal', function() { console.log('this will be fired')})

The issue when using jquery-UI's _on method is that the namespace gets cut off. So this will not work:

$.widget('mywidget', {
    _create: function() {
        this._on({
            'show.bs.modal .modal': function() {
                alert('this will not be fired');
            }
        });
    }
});

The issue is the regex in the _on method. It does not allow a dot in the event name and also it searches for zero to unlimited amount of spaces to split of the selector. This results into the following:

var match = event.match( /^([\w:-]*)\s*(.*)$/ );
var eventName = match[ 1 ] + instance.eventNamespace; // "show.mywidget"
var selector = match[ 2 ]; // ".bs.modal .modal"

To prevent this I changed the regex to also allow dots so namespaces will not be broken apart and also to require at least one whitespace.

var match = event.match( /^([\w:.-]*)\s+(.*)$/ );
var eventName = match[ 1 ] + instance.eventNamespace; // "show.bs.modal.mywidget"
var selector = match[ 2 ]; // ".modal"

Eydamos avatar Sep 01 '22 13:09 Eydamos

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: TimWerdin / name: Tim Werdin (cc6a600067c4d71e57101336a251bc279e785875)

Thanks for the PR. All changes require tests, though. I’m not sure why existing tests didn’t run for this PR but maybe that would happen on the next update to the PR.

mgol avatar Oct 08 '22 18:10 mgol

Closing and reopening in order to run the tests.

fnagel avatar Oct 10 '22 08:10 fnagel

@TimWerdin in addition to writing new tests, you need to ensure existing ones pass. You can now see that's not the case right now.

mgol avatar Oct 10 '22 12:10 mgol

I was on vacation until yesterday. I will have a look this week

Eydamos avatar Oct 17 '22 11:10 Eydamos