1 // Copyright 2014 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.
6 * @fileoverview A collection of JavaScript utilities used to manage focus
11 goog.provide('cvox.FocusUtil');
15 * Utilities for managing focus.
18 cvox.FocusUtil = function() {
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
29 * TODO(dmazzoni): merge this with cvox.DomUtil.isInputTypeText
33 cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE = {
46 'datetime-local' : false,
60 * Checks if the currently focused element is a field that accepts text input
61 * (This can include text fields and selectors)
63 * @return {boolean} True if the currently focused element accepts text input.
65 cvox.FocusUtil.isFocusInTextInputField = function() {
66 var activeElement = document.activeElement;
72 if (activeElement.isContentEditable) {
76 if (activeElement.getAttribute('role') == 'textbox') {
80 if (activeElement.getAttribute('readOnly') == 'true') {
84 if (activeElement.tagName === 'TEXTAREA' ||
85 activeElement.tagName === 'SELECT') {
89 if (activeElement.tagName === 'INPUT') {
90 if (!activeElement.hasAttribute('type')) {
93 var activeType = activeElement.getAttribute('type').toLowerCase();
94 return cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE[activeType];