delegate.html revision cb4f4f79feb7498d1f3406b88fb62209e5f5a444
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <div id="mod-header" class="hd"><h3 class="title">H3 - Title</h3></div>
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov <p>simple paragraph with a link <a href="#"id="firstlink">simple link</a></p>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <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>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <li id="list-2-li-1"><em><em>Item One</em></em></li>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <li id="list-3-li-1"><em>Item Three - One</em></li>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <li id="list-3-li-1"><em>Item Three - Two</em></li>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <li id="list-3-li-1"><em>Item Three - Three</em></li>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe <div id="div-1-1-1" class="div-c"><em>Item One</em></div>
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov <div id="div-1-2-1-1" class="div-c"><em>Item Two - Two</em></div>
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov <!-- YUI3 Core //-->
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov <script type="text/javascript" src="/build/yui/yui.js"></script>
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov <!-- Initialization process //-->
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov logExclude: { Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }).use('console', 'test', 'event', 'event-simulate', function (Y) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // creating the console...
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (new Y.Console()).render();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov // starting the testing process
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov // add the test cases and suites
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name: "Event Delegate Tests",
e7df7762bfed06e996cc80f583cbee2d8ed81d69Cody Peter Mello test_simple_delegate_for_anchors: function(){
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe var foo = false, t, ct, boundEl;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe Y.delegate('click', function(e) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }, '#mod1', 'a');
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov Y.Event.simulate (document.getElementById('firstlink'), 'click');
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov // Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.get('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.get('#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.get('#mod-header'), "event delegate works but the matched element is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.get('#mod-body'), "event delegate works but the matched element is an incorrect node, should be the matching node");
Y.delegate('click', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
}, Y.get('#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.get('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.get('#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.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching node");
Y.Assert.areEqual(ct, Y.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
Y.Assert.areEqual(t, Y.get('#fakeimage'), "event delegate works but the target is an incorrect node, should be the actual click target");
Y.Assert.areEqual(boundEl, Y.get('#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.get('#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.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t1, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked node");
Y.Assert.areEqual(ct2, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked");
Y.delegate('click', function(e) {
t1 = e.target;
ct = e.currentTarget;
Y.on('click', function(e) {
t2 = e.target;
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.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
Y.Assert.areEqual(ct, Y.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
Y.Assert.areEqual(t1, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the actual target");
Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but it doesn't restore e.target to the original value.");
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.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.");