delegate.html revision f647f9c7909e84a0b19f09de42ce82a715e2d52a
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <div id="mod-header" class="hd"><h3 class="title">H3 - Title</h3></div>
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <p>simple paragraph with a link <a href="#"id="firstlink">simple link</a></p>
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <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>
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <!-- YUI3 Core //-->
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <script type="text/javascript" src="/build/yui/yui-debug.js"></script>
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber <!-- Initialization process //-->
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber filter: 'debug',
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber combine: false,
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber useConsole: true,
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber logExclude: {deprecated: 1,
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber }).use('console', 'test', 'event', 'event-simulate', function (Y) {
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber // creating the console...
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber // (new Y.Console()).render();
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber // starting the testing process
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber // add the test cases and suites
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber name: "Event Delegate Tests",
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber test_simple_delegate_for_anchors: function(){
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber var foo = false, t, ct, boundEl;
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.on('delegate', function(e) {
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber }, '#mod1', 'click', 'a');
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.Event.simulate (document.getElementById('firstlink'), 'click');
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber // Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.Assert.areEqual(ct, Y.get('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
1ba0013f21e1953ae4a59cdf61562fbe843077e9Stéphane Graber Y.Assert.areEqual(boundEl, Y.get('#mod1'), "event delegate works but the container property should be the bound element");
Y.on('delegate', 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.on('delegate', function(e) {
t = e.target;
ct = e.currentTarget;
boundEl = e.container;
}, Y.get('#mod1').get('ownerDocument'), 'click', '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.on('delegate', 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.on('delegate', 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.on('delegate', function(e) {
t = e.target;
Y.Assert.isFalse(foo, "delegation fails, delegation without at least one selector should never trigger an event");
Y.on('delegate', 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.on('delegate', function(e) {
t1 = e.target;
ct1 = e.currentTarget;
Y.on('delegate', 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.on('delegate', 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.on('delegate', 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");