OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview A collection of JavaScript utilities used to manage focus |
| 7 * within a document. |
| 8 */ |
| 9 |
| 10 |
| 11 goog.provide('cvox.FocusUtil'); |
| 12 |
| 13 |
| 14 /** |
| 15 * Utilities for managing focus. |
| 16 * @constructor |
| 17 */ |
| 18 cvox.FocusUtil = function() { |
| 19 }; |
| 20 |
| 21 /** |
| 22 * Maps whether an input element of specified type accepts text selection or |
| 23 * not. True if the element does accept text selection, false if it does not. |
| 24 * This can be used to determine whether a visitor to that element should |
| 25 * provide interactive text editing to the user. |
| 26 * From the W3C table of possible type keywords: |
| 27 * http://www.w3.org/TR/html5/the-input-element.html#attr-input-type |
| 28 * @type {Object} |
| 29 */ |
| 30 cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE = { |
| 31 'hidden' : false, |
| 32 'text' : true, |
| 33 'search' : true, |
| 34 'tel' : true, |
| 35 'url' : true, |
| 36 'email' : true, |
| 37 'password' : true, |
| 38 'datetime' : false, |
| 39 'date' : false, |
| 40 'month' : false, |
| 41 'week' : false, |
| 42 'time' : false, |
| 43 'datetime-local' : false, |
| 44 'number' : false, |
| 45 'range' : false, |
| 46 'color' : false, |
| 47 'checkbox' : false, |
| 48 'radio' : false, |
| 49 'file' : false, |
| 50 'submit' : false, |
| 51 'image' : false, |
| 52 'reset' : false, |
| 53 'button' : false |
| 54 }; |
| 55 |
| 56 /** |
| 57 * Checks if the currently focused element is a field that accepts text input |
| 58 * (This can include text fields and selectors) |
| 59 * |
| 60 * @return {boolean} True if the currently focused element accepts text input. |
| 61 */ |
| 62 cvox.FocusUtil.isFocusInTextInputField = function() { |
| 63 // TODO(rshearer): Check all editable text fields here, including |
| 64 // aria role=textbox. |
| 65 |
| 66 var activeElement = document.activeElement; |
| 67 |
| 68 if (activeElement.isContentEditable) { |
| 69 return true; |
| 70 } |
| 71 |
| 72 if ((activeElement.tagName === 'INPUT') || |
| 73 (activeElement.tagName === 'TEXTAREA') || |
| 74 (activeElement.tagName === 'SELECT')) { |
| 75 |
| 76 if (activeElement.getAttribute('readOnly') == 'true') { |
| 77 return false; |
| 78 } else if (! activeElement.hasAttribute('type')) { |
| 79 return true; |
| 80 } else { |
| 81 var activeType = activeElement.getAttribute('type').toLowerCase(); |
| 82 |
| 83 return cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE[activeType]; |
| 84 } |
| 85 } |
| 86 return false; |
| 87 }; |
OLD | NEW |