Skip to content

Commit

Permalink
Merge pull request #207 from canjs/disabled-elements-removed-event
Browse files Browse the repository at this point in the history
Firefox throws an error when a custom event is dispatched on a disabled input element
  • Loading branch information
phillipskevin authored Mar 27, 2017
2 parents 237ada2 + 5095bd1 commit d5f9c1c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
13 changes: 13 additions & 0 deletions dom/dispatch/dispatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ test("basic synthetic events", function () {

});

test("synthetic events on disabled element", function () {
expect(1);
var input = document.createElement("input");
input.disabled = true;

domEvents.addEventListener.call(input, "foo", function(){
ok(true, "called back");
});

document.getElementById("qunit-fixture").appendChild(input);
domDispatch.call(input, "foo", [], false);
});

test("more complex synthetic events", function () {
var div = document.createElement("div");
var arr = [];
Expand Down
14 changes: 13 additions & 1 deletion dom/events/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
},
dispatch: function(event, args, bubbles){
var doc = _document();
var ret;
var dispatchingOnDisabled = this.disabled;

var ev = doc.createEvent('HTMLEvents');
var isString = typeof event === "string";
Expand All @@ -33,6 +35,16 @@ module.exports = {
assign(ev, event);
}
ev.args = args;
return this.dispatchEvent(ev);
// In FireFox, dispatching an event on a disabled element throws an error.
// So ensure the mutatedNode is not disabled.
// https://bugzilla.mozilla.org/show_bug.cgi?id=329509
if(dispatchingOnDisabled) {
this.disabled = false;
}
ret = this.dispatchEvent(ev);
if(dispatchingOnDisabled) {
this.disabled = true;
}
return ret;
}
};
20 changes: 17 additions & 3 deletions dom/events/inserted/inserted-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function runTest(name, MUT_OBS) {
}
});


asyncTest("basic insertion with mutation observer", function () {
asyncTest("basic insertion", function () {
var div = document.createElement("div");

domEvents.addEventListener.call(div,"inserted", function(){
Expand All @@ -28,6 +27,22 @@ function runTest(name, MUT_OBS) {

domMutate.appendChild.call(document.getElementById("qunit-fixture"), div);
});

asyncTest("basic disabled insertion", function () {
expect(1);
var input = document.createElement("input");
input.disabled = true;

domEvents.addEventListener.call(input,"inserted", function(){
ok(true, "called back");
start();
});

// With no mutation observer this test will not pass without a setTimeout
setTimeout(function(){
domMutate.appendChild.call(document.getElementById("qunit-fixture"), input);
}, 20);
});
asyncTest("parent then child inserted - appendChild", function () {
expect(1);
var div = document.createElement("div");
Expand Down Expand Up @@ -55,7 +70,6 @@ function runTest(name, MUT_OBS) {
ok(true, "called back");
start();
});

domMutate.appendChild.call(document.getElementById("qunit-fixture"), div);
});

Expand Down
31 changes: 30 additions & 1 deletion dom/events/removed/removed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ if(_MutationObserver) {
document.getElementById("qunit-fixture").removeChild(div);
});

asyncTest("with mutation observer - disabled removal - removeChild", function () {
var input = document.createElement("removed");
input.disabled = true;

domEvents.addEventListener.call(input,"removed", function(){
ok(true, "called back");
start();
});

document.getElementById("qunit-fixture").appendChild(input);
document.getElementById("qunit-fixture").removeChild(input);
});

asyncTest("with mutation observer - basic removal - replaceChild", function () {
var div = document.createElement("div");
var div2 = document.createElement("div");
Expand Down Expand Up @@ -112,7 +125,7 @@ if(_MutationObserver) {
});
}

asyncTest("basic insertion without mutation observer - removeChild", function(){
asyncTest("basic removal without mutation observer - removeChild", function(){
getMutationObserver(null);

var div = document.createElement("div");
Expand All @@ -127,6 +140,22 @@ asyncTest("basic insertion without mutation observer - removeChild", function(){
domMutate.removeChild.call(document.getElementById("qunit-fixture"), div);
});

asyncTest("disabled removal without mutation observer - removeChild", function(){
getMutationObserver(null);

var input = document.createElement("input");
input.disabled = true;

domEvents.addEventListener.call(input,"removed", function(){
ok(true, "called back");
getMutationObserver(_MutationObserver);
start();
});

domMutate.appendChild.call(document.getElementById("qunit-fixture"), input);
domMutate.removeChild.call(document.getElementById("qunit-fixture"), input);
});

asyncTest("basic insertion without mutation observer - replaceChild", function(){
getMutationObserver(null);

Expand Down

0 comments on commit d5f9c1c

Please sign in to comment.