Skip to content

Commit

Permalink
Fix: wrang event dispatching kupriyanenko#51
Browse files Browse the repository at this point in the history
  • Loading branch information
kupriyanenko committed Apr 16, 2015
1 parent f42552f commit 15a23a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jBone.event = {
// if propagation is not stopped
!(event && event.isImmediatePropagationStopped());
i++) {
expectedTarget = null;
eventOptions = {};
handler = handlerQueue[i];
handler.data && (eventOptions.data = handler.data);
Expand All @@ -135,20 +136,26 @@ jBone.event = {
// event handler with selector
else if (
// if target and selected element the same
~(targets = jBone(el).find(handler.selector)).indexOf(e.target) ||
~(targets = jBone(el).find(handler.selector)).indexOf(e.target) && (expectedTarget = e.target) ||
// if one of element matched with selector contains target
(el !== e.target && el.contains(e.target))
) {
l = targets.length;

// get element matched with selector
for (; j < length; j++) {
if (targets[j] && targets[j].contains(e.target)) {
expectedTarget = targets[j];
if (!expectedTarget) {
l = targets.length;
j = 0;

for (; j < l; j++) {
if (targets[j] && targets[j].contains(e.target)) {
expectedTarget = targets[j];
}
}
}

expectedTarget = expectedTarget || e.target;
if (!expectedTarget) {
continue;
}

eventOptions.currentTarget = expectedTarget;
event = new BoneEvent(e, eventOptions);

Expand Down
39 changes: 39 additions & 0 deletions test/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,45 @@ describe('jBone Event', function() {
expect(counter).be.eql(3);
});

it('on(event, target, callback) with complex selecors', function() {
var $div = jBone('<div class="container"><span class="target"><input type="text" /><input class="click" type="text" /></span></div>'),
counter = 0;

jBone('#app').html($div);

$div.on('click', '.target > input:not(.click)', function() {
counter++;
});

$div.on('click', '.target > input.click', function() {
counter++;
});

$div.find('input.click').trigger('click');
expect(counter).be.eql(1);

$div.find('input:not(.click)').trigger('click');
expect(counter).be.eql(2);
});

it('on(event, target, callback) with wrong selecors', function() {
var $div = jBone('<div class="container"><span class="target"><i class="click"></i></span><span class="wrong" type="text"><i class="click"></i></span></div>'),
counter = 0;

jBone('#app').html($div);

$div.on('click', '.wrong', function() {
counter++;
});

$div.on('click', '.target', function() {
counter++;
});

$div.find('.click').eq(0).trigger('click');
expect(counter).be.eql(1);
});

it('on(event, target, callback) preventDefault', function() {
var a = jBone('<div><span><p></p></span></div>');

Expand Down

0 comments on commit 15a23a9

Please sign in to comment.