| // Copyright 2013 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // This file provides common methods that can be shared by other JavaScripts. |
| |
| // Requires functions from base.js. |
| |
| /** @typedef {HTMLInputElement|HTMLTextAreaElement|HTMLSelectElement} */ |
| let FormControlElement; |
| |
| /** |
| * Namespace for this file. It depends on |__gCrWeb| having already been |
| * injected. String 'common' is used in |__gCrWeb['common']| as it needs to be |
| * accessed in Objective-C code. |
| */ |
| __gCrWeb.common = {}; |
| |
| // Store common namespace object in a global __gCrWeb object referenced by a |
| // string, so it does not get renamed by closure compiler during the |
| // minification. |
| __gCrWeb['common'] = __gCrWeb.common; |
| |
| /** |
| * JSON safe object to protect against custom implementation of Object.toJSON |
| * in host pages. |
| * @constructor |
| */ |
| __gCrWeb.common.JSONSafeObject = function JSONSafeObject() {}; |
| |
| /** |
| * Protect against custom implementation of Object.toJSON in host pages. |
| */ |
| __gCrWeb.common.JSONSafeObject.prototype['toJSON'] = null; |
| |
| /** |
| * Retain the original JSON.stringify method where possible to reduce the |
| * impact of sites overriding it |
| */ |
| __gCrWeb.common.JSONStringify = JSON.stringify; |
| |
| /** |
| * Returns a string that is formatted according to the JSON syntax rules. |
| * This is equivalent to the built-in JSON.stringify() function, but is |
| * less likely to be overridden by the website itself. Prefer the private |
| * {@code __gcrWeb.common.JSONStringify} whenever possible instead of using |
| * this public function. The |__gCrWeb| object itself does not use it; it uses |
| * its private counterpart instead. |
| * @param {*} value The value to convert to JSON. |
| * @return {string} The JSON representation of value. |
| */ |
| __gCrWeb.stringify = function(value) { |
| if (value === null) return 'null'; |
| if (value === undefined) return 'undefined'; |
| if (typeof (value.toJSON) === 'function') { |
| // Prevents websites from changing stringify's behavior by adding the |
| // method toJSON() by temporarily removing it. |
| const originalToJSON = value.toJSON; |
| value.toJSON = undefined; |
| const stringifiedValue = __gCrWeb.common.JSONStringify(value); |
| value.toJSON = originalToJSON; |
| return stringifiedValue; |
| } |
| return __gCrWeb.common.JSONStringify(value); |
| }; |
| |
| /** |
| * Returns if an element is a text field. |
| * This returns true for all of textfield-looking types such as text, |
| * password, search, email, url, and number. |
| * |
| * This method aims to provide the same logic as method |
| * bool WebInputElement::isTextField() const |
| * in chromium/src/third_party/WebKit/Source/WebKit/chromium/src/ |
| * WebInputElement.cpp, where this information is from |
| * bool HTMLInputElement::isTextField() const |
| * { |
| * return m_inputType->isTextField(); |
| * } |
| * (chromium/src/third_party/WebKit/Source/WebCore/html/HTMLInputElement.cpp) |
| * |
| * The implementation here is based on the following: |
| * |
| * - Method bool InputType::isTextField() defaults to be false and it is |
| * override to return true only in HTMLInputElement's subclass |
| * TextFieldInputType (chromium/src/third_party/WebKit/Source/WebCore/html/ |
| * TextFieldInputType.h). |
| * |
| * - The implementation here considers all the subclasses of |
| * TextFieldInputType: NumberInputType and BaseTextInputType, which has |
| * subclasses EmailInputType, PasswordInputType, SearchInputType, |
| * TelephoneInputType, TextInputType, URLInputType. (All these classes are |
| * defined in chromium/src/third_party/WebKit/Source/WebCore/html/) |
| * |
| * @param {Element} element An element to examine if it is a text field. |
| * @return {boolean} true if element has type=text. |
| */ |
| __gCrWeb.common.isTextField = function(element) { |
| if (!element) { |
| return false; |
| } |
| if (element.type === 'hidden') { |
| return false; |
| } |
| return element.type === 'text' || element.type === 'email' || |
| element.type === 'password' || element.type === 'search' || |
| element.type === 'tel' || element.type === 'url' || |
| element.type === 'number'; |
| }; |