<p>another paragraph with a complex link <a href="#" id="secondlink"><strong>strong within link</strong><img alt="fake image" id="fakeimage" /> - complex <span id="spanwithinlink">link</span></a></p>
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
logExclude: { Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
var suite = new Y.Test.Suite("Delegate");
(new Y.Console()).render();
// Y.delegate('foo|click', function(e) {
Y.delegate('click', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
// Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.one('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.one('#mod1'), "event delegate works but the container property should be the bound element");
Y.delegate('click', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
Y.Assert.areEqual(ct, Y.one('#mod-header'), "event delegate works but the matched element is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.one('#mod-body'), "event delegate works but the matched element is an incorrect node, should be the matching node");
handle = Y.delegate('click', function (e) {
return target.get('tagName').toLowerCase() === 'a';
Y.Assert.areSame(1, count);
if (Y.isWindowInFocus()) {
handle = Y.delegate('focus', function (e) {
return target.get('tagName').toLowerCase() === 'a';
Y.one('#spanwithinlink').focus();
Y.one('#fakeimage').focus();
Y.one('#secondlink').focus();
Y.Assert.areSame(1, count);
Y.log("Window is not focused. Can't properly test.",
Y.delegate('click', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
}, Y.one('#mod1').get('ownerDocument'), 'a');
Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
Y.Assert.areEqual(ct, Y.one('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.one('#mod1').get('ownerDocument'), "event delegate works but the container property should be the bound element");
Y.delegate('click', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
Y.Assert.isTrue(foo, "delegation fails for an image within an anchor, mod1 should pickup the event and test target [secondlink]");
// Y.Assert.areEqual(t, Y.one('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.one('#fakeimage'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.one('#mod1'), "event delegate works but the container property should be the bound element");
Y.delegate('click', function(e) {
t = e.target;
Y.Assert.isFalse(foo, "delegation fails, the container (specified in the on) can not be part of the selectors");
Y.delegate('click', function(e) {
t = e.target;
Y.Assert.isFalse(foo, "delegation fails, delegation without at least one selector should never trigger an event");
Y.delegate('click', function(e) {
t = e.target;
Y.Assert.isTrue(foo, "multiple selectors fails, delegate should be able to match different selectors");
Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
Y.delegate('click', function(e) {
t1 = e.target;
ct1 = e.currentTarget;
Y.delegate('click', function(e) {
t2 = e.target;
ct2 = e.currentTarget;
Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
Y.Assert.isTrue(foo2, "second match fail, delegate should be able to match [a span]");
Y.Assert.areEqual(ct1, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t1, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked node");
Y.Assert.areEqual(ct2, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t2, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked");
Y.delegate('click', function(e) {
t1 = e.target;
ct1 = e.currentTarget;
container1 = e.container;
Y.on('click', function(e) {
t2 = e.target;
ct2 = e.currentTarget;
container2 = e.container;
Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
Y.Assert.isTrue(foo2, "second match fail, the event doesn't bubble up after the delegate routine");
// Y.Assert.areEqual(t1, Y.one('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
Y.Assert.areEqual(ct1, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t1, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the actual target");
Y.Assert.areEqual(t2, Y.one('#spanwithinlink'), "event delegate works but it doesn't restore e.target to the original value.");
Y.Assert.isUndefined(container2);
Y.delegate('click', function(e) {
e.halt();
Y.on('click', function(e) {
Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
Y.Assert.isFalse(foo2, "the listener for 'doc' got executed, which means that e.halt fails during the delegate routine");
Y.one("#list-2").delegate("click", function (e) {
matchID = this.get("id");
Y.Assert.areEqual('list-2-li-3', matchID, "The currentTarget is an incorrect node, should be the matching node.");
Y.one("#div-1").delegate("click", function (e) {
matchID = this.get("id");
Y.Assert.areEqual('div-1-1', matchID, "The currentTarget is an incorrect node, should be the matching node.");
Y.Assert.isNull(matchID, "The currentTarget is an incorrect node, should be the matching node.");
Y.delegate('click', function (e) {
Y.Assert.areSame(4, ids.length, "Delegate handler should have fired three times, once for each div in #div-1");
Y.Assert.areSame('div-1-2-1-1', ids[0], "First delegate callback should be from the deepest div in the subtree");
Y.Assert.areSame('div-1-2-1', ids[1]);
Y.Assert.areSame('div-1-2', ids[2]);
Y.Assert.areSame('div-1', ids[3]);
Y.one('#a').delegate('click', function (e) {
this.get('parentNode').remove();
Y.Assert.areSame(2, count);
Y.delegate('click', function(e) {
Y.Event.purgeElement('#mod1');
// calling Y.Event.purgeElement
Y.Assert.isFalse(bHandler1Called, "No delegated listeners should be called after a call to Y.Event.purgeElement");
// calling Y.Event.purgeElement
Y.delegate('click', function(e) {
Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
// calling Y.Event.purgeElement and passing in a type
Y.Event.purgeElement('#mod1', false, 'click');
Y.Assert.isFalse(bHandler2Called, "The second listener should not be be called.");
Y.delegate("click", function () {
Y.Event.purgeElement('#list-1');
Y.one("#container").setContent('<ul id="list-1"><li id="item-1"><em>Item Type One</em></li><li id="item-2"><em>Item Type Two</em></li><li id="item-3"><em>Item Type Three</em></li></ul>');
Y.delegate("click", function () {
Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
var handle = Y.delegate('click', function(e) {
Y.Assert.isFalse(bHandler1Called, "Listener should not be called after being detached.");
Y.delegate('click', function(e) {
Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
handle = Y.delegate("click", function () {
Y.one("#container").setContent('<ul id="list-1"><li id="item-1"><em>Item Type One</em></li><li id="item-2"><em>Item Type Two</em></li><li id="item-3"><em>Item Type Three</em></li></ul>');
Y.delegate("click", function () {
Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
"Y.delegate('cat|type') should subscribe event": function () {
cat = Y.guid(),
handle = Y.delegate(cat + "|click", function () {
Y.Assert.areSame(1, count);
Y.Assert.areSame(1, count, "category sub did not detach with handle");
"node.delegate('cat|type') should subscribe event": function () {
cat = Y.guid(),
handle = Y.one('#container').delegate(cat + "|click", function () {
Y.Assert.areSame(1, count);
Y.Assert.areSame(1, count, "category sub did not detach with handle");
"node.detach('cat|type', fn) should detach delegate": function () {
target = Y.one("#container"),
cat = Y.guid(),
handle = target.delegate(cat + "|click", increment, 'li');
Y.Assert.areSame(1, count);
target.detach(cat + '|click', increment);
Y.Assert.areSame(1, count);
"node.detach('cat|type') should detach delegate": function () {
target = Y.one("#container"),
cat = Y.guid(),
handle = target.delegate(cat + "|click", increment, 'li');
Y.Assert.areSame(1, count);
target.detach(cat + '|click');
Y.Assert.areSame(1, count);
"node.detach('cat|*') should detach delegate": function () {
target = Y.one("#container"),
cat = Y.guid(),
handle = target.delegate(cat + "|click", increment, 'li');
Y.Assert.areSame(1, count);
target.detach(cat + '|*');
Y.Assert.areSame(1, count);
Y.one('#div-1').delegate({
Y.Assert.isTrue(clickOk);
Y.Assert.isTrue(mousedownOk);
Y.one('#div-1').delegate({
Y.Assert.isObject(thisObj);
Y.one('#div-1').delegate({
Y.Assert.areSame("extra arg", arg);
handle = Y.one('#div-1').delegate({
Y.Assert.areSame(1, count);
Y.Assert.areSame(1, count);
Y.one('#div-1').delegate(['click', 'mousedown'], function () {
Y.Assert.areSame(2, count);
name: "Y.delegate.compileFilter",
"Y.delegate.compileFilter should return a function": function () {
var filter = Y.delegate.compileFilter('em'),
container = Y.one('#container'),
em = Y.one('#container em');
Y.Assert.isTrue(filter(em, {
var filter = Y.delegate.compileFilter('em'),
container = Y.one('#list-2'),
em = Y.one('#container em');
Y.Assert.isFalse(filter(em, {
var filter = Y.delegate.compileFilter('div'),
container = Y.one('#container');
Y.Assert.isTrue(filter(container, {
if (Y.isWindowInFocus()) {
handle = Y.one('body').delegate('focus', function (e) {
Y.one('#firstlink').focus();
Y.Assert.areSame(1, count);
Y.log("Window is not focused. Can't properly test.",
handle = Y.delegate('click', function (e) {
e.halt();
Y.Assert.isTrue((count > 1));
Y.Assert.areSame(1, count);
Y.Assert.areSame(1, count);
Y.Test.Runner.setName("Delegate");
Y.Test.Runner.add(suite);