Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Joris-van-der-Wel/ebff816a2b65d87c7212de7ebac2a511 to your computer and use it in GitHub Desktop.
Save Joris-van-der-Wel/ebff816a2b65d87c7212de7ebac2a511 to your computer and use it in GitHub Desktop.
<!doctype html>
<meta charset="utf-8">
<title>Handling of inline event handlers without a browsing context</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
var fired;
test(function() {
// This document has no browsing context:
var doc = document.implementation.createHTMLDocument('');
var e = doc.createElement("div");
e.setAttribute("onclick", "fired = true");
assert_equals(e.onclick, null, "event handler should return null");
assert_equals(e.onclick, null, "event handler should still return null");
fired = false;
e.dispatchEvent(new Event("click"));
assert_equals(fired, false, "the event handler should have no effect");
});
test(function() {
var doc = document.implementation.createHTMLDocument('');
var e = doc.createElement("div");
e.setAttribute("onclick", "fired = true");
assert_equals(e.onclick, null, "event handler should be null");
assert_equals(e.onclick, null, "event handler should should remain null");
document.adoptNode(e);
assert_equals(typeof e.onclick, 'function', "the event handler should be compiled properly after adopting into a document with a window");
doc.adoptNode(e);
assert_equals(typeof e.onclick, 'function', "the compiled event handler should not be unset during adoption");
});
test(function() {
var doc = document.implementation.createHTMLDocument('');
var e = doc.createElement("div");
var f = function() { fired = true; };
e.onclick = f;
assert_equals(e.onclick, f, "event handler should be equal to the function just set");
e.setAttribute("onclick", "fired = true");
assert_equals(e.onclick, null, "the previous event handler should have been overwritten"); // (but getting it returns null because we have no browsing context)
assert_equals(e.onclick, null, "the event handler should remain null");
fired = false;
e.dispatchEvent(new Event("click"));
assert_equals(fired, false, "the event handler should have no effect");
});
test(function() {
// the <body> has special behaviour for event handlers (the handlers are also set on the window)
var doc = document.implementation.createHTMLDocument('');
var e = doc.body;
var f = function() { fired = true; };
e.onscroll = f;
assert_equals(e.onscroll, f, "event handler should be equal to the function just set");
e.setAttribute("onscroll", "fired = true");
assert_equals(e.onscroll, null, "the previous event handler should have been overwritten by null");
assert_equals(e.onscroll, null, "the event handler should remain null");
fired = false;
e.dispatchEvent(new Event("scroll"));
assert_equals(fired, false, "the event handler should have no effect");
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment