delegate.html revision 5923a4c5f9d5f357cd461ff95cb29475eecf8e3d
0N/A <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>
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);
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.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(3, ids.length, "Delegate handler should have fired three times, once for each div in #div-1");
Y.Assert.areSame('div-1-2', ids[0], "First delegate callback should be from the highest div in the subtree");
Y.Assert.areSame('div-1-2-1', ids[1]);
Y.Assert.areSame('div-1-2-1-1', ids[2]);
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.");
handle = Y.one('body').delegate('focus', function (e) {
Y.one('#firstlink').focus();
Y.Assert.areSame(1, count);
Y.Test.Runner.add(suite);