| 1 | define( [ |
| 2 | "qunit", |
| 3 | "jquery", |
| 4 | "lib/helper", |
| 5 | "ui/data", |
| 6 | "ui/focusable", |
| 7 | "ui/tabbable" |
| 8 | ], function( QUnit, $, helper ) { |
| 9 | "use strict"; |
| 10 | |
| 11 | QUnit.module( "core - selectors", { afterEach: helper.moduleAfterEach } ); |
| 12 | |
| 13 | QUnit.assert.isFocusable = function( selector, msg ) { |
| 14 | this.pushResult( { |
| 15 | result: $( selector ).is( ":focusable" ), |
| 16 | actual: null, |
| 17 | expected: null, |
| 18 | message: msg + " - selector " + selector + " is focusable" |
| 19 | } ); |
| 20 | }; |
| 21 | |
| 22 | QUnit.assert.isNotFocusable = function( selector, msg ) { |
| 23 | this.pushResult( { |
| 24 | result: $( selector ).length && !$( selector ).is( ":focusable" ), |
| 25 | actual: null, |
| 26 | expected: null, |
| 27 | message: msg + " - selector " + selector + " is not focusable" |
| 28 | } ); |
| 29 | }; |
| 30 | |
| 31 | QUnit.assert.isTabbable = function( selector, msg ) { |
| 32 | this.pushResult( { |
| 33 | result: $( selector ).is( ":tabbable" ), |
| 34 | actual: null, |
| 35 | expected: null, |
| 36 | message: msg + " - selector " + selector + " is tabbable" |
| 37 | } ); |
| 38 | }; |
| 39 | |
| 40 | QUnit.assert.isNotTabbable = function( selector, msg ) { |
| 41 | this.pushResult( { |
| 42 | result: $( selector ).length && !$( selector ).is( ":tabbable" ), |
| 43 | actual: null, |
| 44 | expected: null, |
| 45 | message: msg + " - selector " + selector + " is not tabbable" |
| 46 | } ); |
| 47 | }; |
| 48 | |
| 49 | QUnit.test( "data", function( assert ) { |
| 50 | assert.expect( 15 ); |
| 51 | |
| 52 | var element; |
| 53 | |
| 54 | function shouldHaveData( msg ) { |
| 55 | assert.ok( element.is( ":data(test)" ), msg ); |
| 56 | } |
| 57 | |
| 58 | function shouldNotHaveData( msg ) { |
| 59 | assert.ok( !element.is( ":data(test)" ), msg ); |
| 60 | } |
| 61 | |
| 62 | element = $( "<div>" ); |
| 63 | shouldNotHaveData( "data never set" ); |
| 64 | |
| 65 | element = $( "<div>" ).data( "test", null ); |
| 66 | shouldNotHaveData( "data is null" ); |
| 67 | |
| 68 | element = $( "<div>" ).data( "test", true ); |
| 69 | shouldHaveData( "data set to true" ); |
| 70 | |
| 71 | element = $( "<div>" ).data( "test", false ); |
| 72 | shouldNotHaveData( "data set to false" ); |
| 73 | |
| 74 | element = $( "<div>" ).data( "test", 0 ); |
| 75 | shouldNotHaveData( "data set to 0" ); |
| 76 | |
| 77 | element = $( "<div>" ).data( "test", 1 ); |
| 78 | shouldHaveData( "data set to 1" ); |
| 79 | |
| 80 | element = $( "<div>" ).data( "test", "" ); |
| 81 | shouldNotHaveData( "data set to empty string" ); |
| 82 | |
| 83 | element = $( "<div>" ).data( "test", "foo" ); |
| 84 | shouldHaveData( "data set to string" ); |
| 85 | |
| 86 | element = $( "<div>" ).data( "test", [] ); |
| 87 | shouldHaveData( "data set to empty array" ); |
| 88 | |
| 89 | element = $( "<div>" ).data( "test", [ 1 ] ); |
| 90 | shouldHaveData( "data set to array" ); |
| 91 | |
| 92 | element = $( "<div>" ).data( "test", {} ); |
| 93 | shouldHaveData( "data set to empty object" ); |
| 94 | |
| 95 | element = $( "<div>" ).data( "test", { foo: "bar" } ); |
| 96 | shouldHaveData( "data set to object" ); |
| 97 | |
| 98 | element = $( "<div>" ).data( "test", new Date() ); |
| 99 | shouldHaveData( "data set to date" ); |
| 100 | |
| 101 | element = $( "<div>" ).data( "test", /test/ ); |
| 102 | shouldHaveData( "data set to regexp" ); |
| 103 | |
| 104 | element = $( "<div>" ).data( "test", function() {} ); |
| 105 | shouldHaveData( "data set to function" ); |
| 106 | } ); |
| 107 | |
| 108 | QUnit.test( "focusable - visible, enabled elements", function( assert ) { |
| 109 | assert.expect( 22 ); |
| 110 | |
| 111 | assert.isNotFocusable( "#formNoTabindex", "form" ); |
| 112 | assert.isFocusable( "#formTabindex", "form with tabindex" ); |
| 113 | assert.isFocusable( "#enabledFieldset input", "input in enabled fieldset" ); |
| 114 | assert.isNotFocusable( "#disabledFieldset input", "input in disabled fieldset" ); |
| 115 | assert.isFocusable( "#visibleAncestor-inputTypeNone", "input, no type" ); |
| 116 | assert.isFocusable( "#visibleAncestor-inputTypeText", "input, type text" ); |
| 117 | assert.isFocusable( "#visibleAncestor-inputTypeCheckbox", "input, type checkbox" ); |
| 118 | assert.isFocusable( "#visibleAncestor-inputTypeRadio", "input, type radio" ); |
| 119 | assert.isFocusable( "#visibleAncestor-inputTypeButton", "input, type button" ); |
| 120 | assert.isNotFocusable( "#visibleAncestor-inputTypeHidden", "input, type hidden" ); |
| 121 | assert.isFocusable( "#visibleAncestor-button", "button" ); |
| 122 | assert.isFocusable( "#visibleAncestor-select", "select" ); |
| 123 | assert.isFocusable( "#visibleAncestor-textarea", "textarea" ); |
| 124 | assert.isFocusable( "#visibleAncestor-object", "object" ); |
| 125 | assert.isFocusable( "#visibleAncestor-anchorWithHref", "anchor with href" ); |
| 126 | assert.isNotFocusable( "#visibleAncestor-anchorWithoutHref", "anchor without href" ); |
| 127 | assert.isNotFocusable( "#visibleAncestor-span", "span" ); |
| 128 | assert.isNotFocusable( "#visibleAncestor-div", "div" ); |
| 129 | assert.isFocusable( "#visibleAncestor-spanWithTabindex", "span with tabindex" ); |
| 130 | assert.isFocusable( "#visibleAncestor-divWithNegativeTabindex", "div with tabindex" ); |
| 131 | assert.isFocusable( "#nestedVisibilityInheritWithVisibleAncestor", |
| 132 | "span, visibility: inherit inside visibility: visible parent" ); |
| 133 | assert.isFocusable( "#nestedVisibilityInheritWithVisibleAncestor-input", |
| 134 | "input, visibility: inherit inside visibility: visible parent" ); |
| 135 | } ); |
| 136 | |
| 137 | QUnit.test( "focusable - disabled elements", function( assert ) { |
| 138 | assert.expect( 9 ); |
| 139 | |
| 140 | assert.isNotFocusable( "#disabledElement-inputTypeNone", "input, no type" ); |
| 141 | assert.isNotFocusable( "#disabledElement-inputTypeText", "input, type text" ); |
| 142 | assert.isNotFocusable( "#disabledElement-inputTypeCheckbox", "input, type checkbox" ); |
| 143 | assert.isNotFocusable( "#disabledElement-inputTypeRadio", "input, type radio" ); |
| 144 | assert.isNotFocusable( "#disabledElement-inputTypeButton", "input, type button" ); |
| 145 | assert.isNotFocusable( "#disabledElement-inputTypeHidden", "input, type hidden" ); |
| 146 | assert.isNotFocusable( "#disabledElement-button", "button" ); |
| 147 | assert.isNotFocusable( "#disabledElement-select", "select" ); |
| 148 | assert.isNotFocusable( "#disabledElement-textarea", "textarea" ); |
| 149 | } ); |
| 150 | |
| 151 | QUnit.test( "focusable - hidden styles", function( assert ) { |
| 152 | assert.expect( 15 ); |
| 153 | |
| 154 | assert.isNotFocusable( "#displayNoneAncestor-input", "input, display: none parent" ); |
| 155 | assert.isNotFocusable( "#displayNoneAncestor-span", "span with tabindex, display: none parent" ); |
| 156 | |
| 157 | assert.isNotFocusable( "#visibilityHiddenAncestor-input", "input, visibility: hidden parent" ); |
| 158 | assert.isNotFocusable( "#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent" ); |
| 159 | |
| 160 | assert.isFocusable( "#nestedVisibilityOverrideAncestor-input", "input, visibility: visible parent but visibility: hidden grandparent" ); |
| 161 | assert.isFocusable( "#nestedVisibilityOverrideAncestor-span", "span with tabindex, visibility: visible parent but visibility: hidden grandparent " ); |
| 162 | |
| 163 | assert.isNotFocusable( "#nestedVisibilityInheritWithHiddenAncestor", "span, visibility: inherit inside visibility: hidden parent" ); |
| 164 | assert.isNotFocusable( "#nestedVisibilityInheritWithHiddenAncestor-input", "input, visibility: inherit inside visibility: hidden parent" ); |
| 165 | |
| 166 | assert.isNotFocusable( "#displayNone-input", "input, display: none" ); |
| 167 | assert.isNotFocusable( "#visibilityHidden-input", "input, visibility: hidden" ); |
| 168 | assert.isNotFocusable( "#visibilityCollapse-input", "input, visibility: collapse" ); |
| 169 | |
| 170 | assert.isNotFocusable( "#displayNone-span", "span with tabindex, display: none" ); |
| 171 | assert.isNotFocusable( "#visibilityHidden-span", "span with tabindex, visibility: hidden" ); |
| 172 | assert.isNotFocusable( "#visibilityCollapse-span", "span with tabindex, visibility: collapse" ); |
| 173 | |
| 174 | assert.isNotFocusable( "#visibilityCollapse-td", "td with tabindex, visibility: collapse" ); |
| 175 | } ); |
| 176 | |
| 177 | QUnit.test( "focusable - natively focusable with various tabindex", function( assert ) { |
| 178 | assert.expect( 4 ); |
| 179 | |
| 180 | assert.isFocusable( "#inputTabindex0", "input, tabindex 0" ); |
| 181 | assert.isFocusable( "#inputTabindex10", "input, tabindex 10" ); |
| 182 | assert.isFocusable( "#inputTabindex-1", "input, tabindex -1" ); |
| 183 | assert.isFocusable( "#inputTabindex-50", "input, tabindex -50" ); |
| 184 | } ); |
| 185 | |
| 186 | QUnit.test( "focusable - not natively focusable with various tabindex", function( assert ) { |
| 187 | assert.expect( 4 ); |
| 188 | |
| 189 | assert.isFocusable( "#spanTabindex0", "span, tabindex 0" ); |
| 190 | assert.isFocusable( "#spanTabindex10", "span, tabindex 10" ); |
| 191 | assert.isFocusable( "#spanTabindex-1", "span, tabindex -1" ); |
| 192 | assert.isFocusable( "#spanTabindex-50", "span, tabindex -50" ); |
| 193 | } ); |
| 194 | |
| 195 | QUnit.test( "focusable - area elements", function( assert ) { |
| 196 | assert.expect( 3 ); |
| 197 | |
| 198 | assert.isFocusable( "#areaCoordsHref", "coords and href" ); |
| 199 | assert.isFocusable( "#areaNoCoordsHref", "href but no coords" ); |
| 200 | assert.isNotFocusable( "#areaNoImg", "not associated with an image" ); |
| 201 | } ); |
| 202 | |
| 203 | QUnit.test( "focusable - dimensionless parent with overflow", function( assert ) { |
| 204 | assert.expect( 1 ); |
| 205 | |
| 206 | assert.isFocusable( "#dimensionlessParent", "input" ); |
| 207 | } ); |
| 208 | |
| 209 | QUnit.test( "tabbable - visible, enabled elements", function( assert ) { |
| 210 | assert.expect( 20 ); |
| 211 | |
| 212 | assert.isNotTabbable( "#formNoTabindex", "form" ); |
| 213 | assert.isTabbable( "#formTabindex", "form with tabindex" ); |
| 214 | assert.isTabbable( "#enabledFieldset input", "input in enabled fieldset" ); |
| 215 | assert.isNotTabbable( "#disabledFieldset input", "input in disabled fieldset" ); |
| 216 | assert.isTabbable( "#visibleAncestor-inputTypeNone", "input, no type" ); |
| 217 | assert.isTabbable( "#visibleAncestor-inputTypeText", "input, type text" ); |
| 218 | assert.isTabbable( "#visibleAncestor-inputTypeCheckbox", "input, type checkbox" ); |
| 219 | assert.isTabbable( "#visibleAncestor-inputTypeRadio", "input, type radio" ); |
| 220 | assert.isTabbable( "#visibleAncestor-inputTypeButton", "input, type button" ); |
| 221 | assert.isNotTabbable( "#visibleAncestor-inputTypeHidden", "input, type hidden" ); |
| 222 | assert.isTabbable( "#visibleAncestor-button", "button" ); |
| 223 | assert.isTabbable( "#visibleAncestor-select", "select" ); |
| 224 | assert.isTabbable( "#visibleAncestor-textarea", "textarea" ); |
| 225 | assert.isTabbable( "#visibleAncestor-object", "object" ); |
| 226 | assert.isTabbable( "#visibleAncestor-anchorWithHref", "anchor with href" ); |
| 227 | assert.isNotTabbable( "#visibleAncestor-anchorWithoutHref", "anchor without href" ); |
| 228 | assert.isNotTabbable( "#visibleAncestor-span", "span" ); |
| 229 | assert.isNotTabbable( "#visibleAncestor-div", "div" ); |
| 230 | assert.isTabbable( "#visibleAncestor-spanWithTabindex", "span with tabindex" ); |
| 231 | assert.isNotTabbable( "#visibleAncestor-divWithNegativeTabindex", "div with tabindex" ); |
| 232 | } ); |
| 233 | |
| 234 | QUnit.test( "tabbable - disabled elements", function( assert ) { |
| 235 | assert.expect( 9 ); |
| 236 | |
| 237 | assert.isNotTabbable( "#disabledElement-inputTypeNone", "input, no type" ); |
| 238 | assert.isNotTabbable( "#disabledElement-inputTypeText", "input, type text" ); |
| 239 | assert.isNotTabbable( "#disabledElement-inputTypeCheckbox", "input, type checkbox" ); |
| 240 | assert.isNotTabbable( "#disabledElement-inputTypeRadio", "input, type radio" ); |
| 241 | assert.isNotTabbable( "#disabledElement-inputTypeButton", "input, type button" ); |
| 242 | assert.isNotTabbable( "#disabledElement-inputTypeHidden", "input, type hidden" ); |
| 243 | assert.isNotTabbable( "#disabledElement-button", "button" ); |
| 244 | assert.isNotTabbable( "#disabledElement-select", "select" ); |
| 245 | assert.isNotTabbable( "#disabledElement-textarea", "textarea" ); |
| 246 | } ); |
| 247 | |
| 248 | QUnit.test( "tabbable - hidden styles", function( assert ) { |
| 249 | assert.expect( 10 ); |
| 250 | |
| 251 | assert.isNotTabbable( "#displayNoneAncestor-input", "input, display: none parent" ); |
| 252 | assert.isNotTabbable( "#displayNoneAncestor-span", "span with tabindex, display: none parent" ); |
| 253 | |
| 254 | assert.isNotTabbable( "#visibilityHiddenAncestor-input", "input, visibility: hidden parent" ); |
| 255 | assert.isNotTabbable( "#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent" ); |
| 256 | |
| 257 | assert.isTabbable( "#nestedVisibilityOverrideAncestor-input", "input, visibility: visible parent but visibility: hidden grandparent" ); |
| 258 | assert.isTabbable( "#nestedVisibilityOverrideAncestor-span", "span with tabindex, visibility: visible parent but visibility: hidden grandparent " ); |
| 259 | |
| 260 | assert.isNotTabbable( "#displayNone-input", "input, display: none" ); |
| 261 | assert.isNotTabbable( "#visibilityHidden-input", "input, visibility: hidden" ); |
| 262 | |
| 263 | assert.isNotTabbable( "#displayNone-span", "span with tabindex, display: none" ); |
| 264 | assert.isNotTabbable( "#visibilityHidden-span", "span with tabindex, visibility: hidden" ); |
| 265 | } ); |
| 266 | |
| 267 | QUnit.test( "tabbable - natively tabbable with various tabindex", function( assert ) { |
| 268 | assert.expect( 4 ); |
| 269 | |
| 270 | assert.isTabbable( "#inputTabindex0", "input, tabindex 0" ); |
| 271 | assert.isTabbable( "#inputTabindex10", "input, tabindex 10" ); |
| 272 | assert.isNotTabbable( "#inputTabindex-1", "input, tabindex -1" ); |
| 273 | assert.isNotTabbable( "#inputTabindex-50", "input, tabindex -50" ); |
| 274 | } ); |
| 275 | |
| 276 | QUnit.test( "tabbable - not natively tabbable with various tabindex", function( assert ) { |
| 277 | assert.expect( 4 ); |
| 278 | |
| 279 | assert.isTabbable( "#spanTabindex0", "span, tabindex 0" ); |
| 280 | assert.isTabbable( "#spanTabindex10", "span, tabindex 10" ); |
| 281 | assert.isNotTabbable( "#spanTabindex-1", "span, tabindex -1" ); |
| 282 | assert.isNotTabbable( "#spanTabindex-50", "span, tabindex -50" ); |
| 283 | } ); |
| 284 | |
| 285 | QUnit.test( "tabbable - area elements", function( assert ) { |
| 286 | assert.expect( 3 ); |
| 287 | |
| 288 | assert.isTabbable( "#areaCoordsHref", "coords and href" ); |
| 289 | assert.isTabbable( "#areaNoCoordsHref", "href but no coords" ); |
| 290 | assert.isNotTabbable( "#areaNoImg", "not associated with an image" ); |
| 291 | } ); |
| 292 | |
| 293 | QUnit.test( "tabbable - dimensionless parent with overflow", function( assert ) { |
| 294 | assert.expect( 1 ); |
| 295 | |
| 296 | assert.isTabbable( "#dimensionlessParent", "input" ); |
| 297 | } ); |
| 298 | |
| 299 | } ); |