• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5/**
6 * @fileoverview A collection of JavaScript utilities used to manage focus
7 * within a document.
8 */
9
10
11goog.provide('cvox.FocusUtil');
12
13
14/**
15 * Utilities for managing focus.
16 * @constructor
17 */
18cvox.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 *
29 * TODO(dmazzoni): merge this with cvox.DomUtil.isInputTypeText
30 *
31 * @type {Object}
32 */
33cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE = {
34  'hidden' : false,
35  'text' : true,
36  'search' : true,
37  'tel' : true,
38  'url' : true,
39  'email' : true,
40  'password' : true,
41  'datetime' : false,
42  'date' : false,
43  'month' : false,
44  'week' : false,
45  'time' : false,
46  'datetime-local' : false,
47  'number' : false,
48  'range' : false,
49  'color' : false,
50  'checkbox' : false,
51  'radio' : false,
52  'file' : false,
53  'submit' : false,
54  'image' : false,
55  'reset' : false,
56  'button' : false
57};
58
59/**
60 * Checks if the currently focused element is a field that accepts text input
61 * (This can include text fields and selectors)
62 *
63 * @return {boolean} True if the currently focused element accepts text input.
64 */
65cvox.FocusUtil.isFocusInTextInputField = function() {
66  var activeElement = document.activeElement;
67
68  if (!activeElement) {
69    return false;
70  }
71
72  if (activeElement.isContentEditable) {
73    return true;
74  }
75
76  if (activeElement.getAttribute('role') == 'textbox') {
77    return true;
78  }
79
80  if (activeElement.getAttribute('readOnly') == 'true') {
81    return false;
82  }
83
84  if (activeElement.tagName === 'TEXTAREA' ||
85      activeElement.tagName === 'SELECT') {
86    return true;
87  }
88
89  if (activeElement.tagName === 'INPUT') {
90    if (!activeElement.hasAttribute('type')) {
91      return true;
92    } else {
93      var activeType = activeElement.getAttribute('type').toLowerCase();
94      return cvox.FocusUtil.INPUT_TYPE_ACCEPTS_SELECTION_TABLE[activeType];
95    }
96  }
97  return false;
98};
99