Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / chromevox / common / focus_util.js
blob4c00595a954edf97d8ec63c40106925ccc283005
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.
5 /**
6 * @fileoverview A collection of JavaScript utilities used to manage focus
7 * within a document.
8 */
11 goog.provide('cvox.FocusUtil');
14 /**
15 * Utilities for managing focus.
16 * @constructor
18 cvox.FocusUtil = function() {
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
29 * TODO(dmazzoni): merge this with cvox.DomUtil.isInputTypeText
31 * @type {Object}
33 cvox.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
59 /**
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;
68 if (!activeElement) {
69 return false;
72 if (activeElement.isContentEditable) {
73 return true;
76 if (activeElement.getAttribute('role') == 'textbox') {
77 return true;
80 if (activeElement.getAttribute('readOnly') == 'true') {
81 return false;
84 if (activeElement.tagName === 'TEXTAREA' ||
85 activeElement.tagName === 'SELECT') {
86 return true;
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];
97 return false;