r1512 - in branches/experimental/tabbable: . tests

r1512 - in branches/experimental/tabbable: . tests


Author: scott.gonzalez
Date: Sun Jan 4 17:17:47 2009
New Revision: 1512
Modified:
branches/experimental/tabbable/tests/core.html
branches/experimental/tabbable/tests/core.js
branches/experimental/tabbable/ui.core.js
Log:
Tabbable: Added tests from Fluid project.
Modified: branches/experimental/tabbable/tests/core.html
==============================================================================
--- branches/experimental/tabbable/tests/core.html    (original)
+++ branches/experimental/tabbable/tests/core.html    Sun Jan 4 17:17:47 2009
@@ -24,6 +24,21 @@
<ol id="tests"></ol>
<div id="main" style="position: absolute; top: -10000px; border: 1px solid
black; padding: 10px; margin: 10px;">
+    <!-- BEGIN TABINDEX -->
+    <h1>Food</h1>
+    <ol id="listWithTabIndex" tabindex="0">
+        <li id="foodWithNegativeTabIndex" tabindex="-1">Rice</li>
+        <li id="foodNoTabIndex">Beans</li>
+        <li>Blinis</li>
+        <li>Tofu</li>
+    </ol>
+
+    <h2 id="headingWithNoTabIndex">I'm hungry. I should...</h2>
+    <a href="#" id="linkWithNoTabIndex">Eat lots of food</a> |
+    <a href="#" id="linkWithTabIndex" tabindex="2">Eat a little food</a> |
+    <a href="#" id="linkWithNegativeTabIndex" tabindex="-1">Eat no food</a>
+    <!-- END TABINDEX -->
+
    <div id="wrap1">
        <input id="input1-1" />
        <input type="text" id="input1-2" />
Modified: branches/experimental/tabbable/tests/core.js
==============================================================================
--- branches/experimental/tabbable/tests/core.js    (original)
+++ branches/experimental/tabbable/tests/core.js    Sun Jan 4 17:17:47 2009
@@ -3,6 +3,69 @@
*/
(function($) {
+module("tabindex");
+
+test("tabindex - get", function() {
+    expect(5);
+
+    // tabindex 0
+    equals($('#listWithTabIndex').attr('tabindex'), 0, 'tabindex of 0');
+
+    // positive tabindex
+    equals($('#linkWithTabIndex').attr('tabindex'), 2, 'tabindex of 2');
+
+    // negative tabindex
+    equals($('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'negative
tabindex');
+
+    // regular element without a tabindex
+    equals($('#headingWithNoTabIndex').attr('tabindex'), undefined, 'no
tabindex, not tabbable by default');
+
+ // link without a tabindex
+    equals($('#linkWithNoTabIndex').attr('tabindex'), undefined, 'no
tabindex, tabbable by default');
+});
+
+test('tabindex - set on element not tabbable by default', function() {
+    expect(7);
+
+    var element = $('#headingWithNoTabIndex');
+    equals(element.attr('tabindex'), undefined, 'start with no tabindex');
+
+    // set a positive string
+    element.attr('tabindex', '1');
+    equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
+
+    // set a zero string
+    element.attr('tabindex', '0');
+    equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
+
+    // set a negative string
+    element.attr('tabindex', '-1');
+    equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
+    
+    // set a positive number
+    element.attr('tabindex', 1);
+    equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
+
+    // set a zero number
+    element.attr('tabindex', 0);
+    equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
+
+    // set a negative number
+    element.attr('tabindex', -1);
+    equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
+});
+
+test("tabindex - set on element with existing tabindex", function() {
+    expect(2);
+
+    var element = jQuery('#linkWithTabIndex');
+    equals(element.attr('tabindex'), 2, 'start with tabindex 2');
+
+    element.attr('tabindex', -1);
+    equals(element.attr('tabindex'), -1, 'set negative tabindex');
+});
+
+
module("selectors");
test("tabbable - enabled elements", function() {
Modified: branches/experimental/tabbable/ui.core.js
==============================================================================
--- branches/experimental/tabbable/ui.core.js    (original)
+++ branches/experimental/tabbable/ui.core.js    Sun Jan 4 17:17:47 2009
@@ -1,6 +1,7 @@
(function($) {
var attr = $.attr,
+    // TODO: determine which case to use for IE8
    normalizedTabindex = ($.browser.msie && parseInt($.browser.version, 10) <
8
        ? 'tabIndex'
        : 'tabindex');
@@ -8,7 +9,7 @@
$.attr = function(elem, key, value) {
    if (/tabindex/i.test(key)) {
        if (value !== undefined) {
-            return attr.apply(elem, normalizedTabindex, value);
+            return attr(elem, normalizedTabindex, value);
        } else {
            var attribute = elem.getAttributeNode(normalizedTabindex);
            return attribute && attribute.specified && attribute.value || undefined;
@@ -17,6 +18,8 @@
    
    return attr.apply(this, arguments);
};
+
+// TODO: proxy $.removeAttr for tabindex
$.extend($.expr[':'], {
    focusable: function(element) {