Skip to content

Commit

Permalink
Merge pull request #27 from rvagg/64f1ffc4fc409a246970b319beef4322570…
Browse files Browse the repository at this point in the history
…29862

IE6,7,8 don't get fixEvent() goodness for custom events
  • Loading branch information
fat committed Sep 17, 2011
2 parents 786962d + 64f1ffc commit 567fbaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/bean.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
listener = W3C_MODEL ? function (element, type, fn, add) {
element[add ? addEvent : removeEvent](type, fn, false);
} : function (element, type, fn, add, custom) {
custom && add && (element['_on' + custom] = element['_on' + custom] || 0);
if (custom && add && element['_on' + custom] === null) {
element['_on' + custom] = 0;
}
element[add ? attachEvent : detachEvent]('on' + type, fn);
},

Expand All @@ -48,9 +50,10 @@
},

customHandler = function (element, fn, type, condition, args) {
return function (e) {
if (condition ? condition.apply(this, arguments) : W3C_MODEL ? true : e && e.propertyName == '_on' + type || !e) {
fn.apply(element, Array.prototype.slice.call(arguments, e ? 0 : 1).concat(args));
return function (event) {
if (condition ? condition.apply(this, arguments) : W3C_MODEL ? true : event && event.propertyName == '_on' + type || !event) {
event = event ? fixEvent(event || ((this.ownerDocument || this.document || this).parentWindow || context).event) : null;
fn.apply(element, Array.prototype.slice.call(arguments, event ? 0 : 1).concat(args));
}
};
},
Expand Down
21 changes: 20 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//stub this for ie crap
if (!window.console) {
window.console = { log: function () {}}
}
Expand Down Expand Up @@ -111,6 +110,12 @@ sink('add', function (test, ok) {
Syn.click(el2);
});

test('add: shouldn\'t trigger event when adding additional custom event listeners', 0, function () {
var el = document.getElementById('input');
bean.add(el, 'foo', function () {ok(true, 'additional custom event listeners trigger event 1')});
bean.add(el, 'foo', function () {ok(true, 'additional custom event listeners trigger event 2')});
});

})

sink('fire', function (test, ok) {
Expand Down Expand Up @@ -193,6 +198,20 @@ sink('event object', function (test, ok) {
Syn.click(el);
});

test('event: should have stop propagation method on custom event', 1, function () {
var el = document.getElementById('foo');
bean.remove(el);
bean.add(el, 'customEvent', function (e) {ok(e.stopPropagation != null, 'has stop propagation')});
bean.fire(el, 'customEvent');
});

test('event: should have preventDefault method on custom event', 1, function () {
var el = document.getElementById('foo');
bean.remove(el);
bean.add(el, 'customEvent', function (e) {ok(e.preventDefault != null, 'has prevent default method')});
bean.fire(el, 'customEvent');
});

test('event: should have keyCode', 1, function () {
var el = document.getElementById('input');
bean.add(el, 'keypress', function (e) {
Expand Down

0 comments on commit 567fbaa

Please sign in to comment.