Chromium Code Reviews| Index: ios/web/web_state/js/resources/form.js |
| diff --git a/ios/web/web_state/js/resources/form.js b/ios/web/web_state/js/resources/form.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2d76a8225e323a64484e6624e41a7b2b3f350c3b |
| --- /dev/null |
| +++ b/ios/web/web_state/js/resources/form.js |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview Adds listeners that are used to handle forms, enabling autofill |
| + * and the replacement method to dismiss the keyboard needed because of the |
| + * Autofill keyboard accessory. |
| + */ |
| + |
| +goog.provide('__crWeb.form'); |
| + |
| +goog.require('__crWeb.message'); |
| + |
| +/** Beginning of anonymous object */ |
| +(function() { |
| + |
| + /** |
| + * Focus and input events for form elements are messaged to the main |
| + * application for broadcast to CRWWebControllerObservers. |
| + * This is done with a single event handler for each type being added to the |
| + * main document element which checks the source element of the event; this |
| + * is much easier to manage than adding handlers to individual elements. |
| + * @private |
| + */ |
| + var formActivity_ = function(evt) { |
| + var srcElement = evt.srcElement; |
| + var fieldName = srcElement.name || ''; |
| + var value = srcElement.value || ''; |
| + |
| + var msg = { |
| + 'command': 'form.activity', |
| + 'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement.form), |
| + 'fieldName': fieldName, |
| + 'type': evt.type, |
| + 'value': value |
| + }; |
| + __gCrWeb.message.invokeOnHost(msg); |
| + }; |
| + |
| + /** |
| + * Focus events performed on the 'capture' phase otherwise they are often |
| + * not received. |
| + */ |
| + document.addEventListener('focus', formActivity_, true); |
| + document.addEventListener('blur', formActivity_, true); |
| + document.addEventListener('change', formActivity_, true); |
| + |
| + /** |
| + * Text input is watched at the bubbling phase as this seems adequate in |
| + * practice and it is less obtrusive to page scripts than capture phase. |
| + */ |
| + document.addEventListener('input', formActivity_, false); |
| + document.addEventListener('keyup', formActivity_, false); |
| + |
| + /** |
| + * Capture form submit actions. |
| + */ |
| + document.addEventListener('submit', function(evt) { |
| + var action; |
| + if (evt['defaultPrevented']) |
| + return; |
| + action = evt.target.getAttribute('action'); |
| + // Default action is to re-submit to same page. |
| + if (!action) { |
| + action = document.location.href; |
| + } |
| + __gCrWeb.message.invokeOnHost({ |
| + 'command': 'document.submit', |
| + 'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement), |
| + 'href': getFullyQualifiedURL_(action) |
| + }); |
| + }, false); |
| + |
| + /** @private */ |
| + function getFullyQualifiedUrl_(originalURL) { |
|
Eugene But (OOO till 7-30)
2017/04/10 21:42:07
nit: How about |var getFullyQualifiedUrl_ = functi
danyao
2017/04/10 22:12:59
Done.
|
| + // A dummy anchor (never added to the document) is used to obtain the |
| + // fully-qualified URL of |originalURL|. |
| + var anchor = document.createElement('a'); |
| + anchor.href = originalURL; |
| + return anchor.href; |
| + }; |
| + |
| + /** Flush the message queue. */ |
| + if (__gCrWeb.message) { |
| + __gCrWeb.message.invokeQueues(); |
| + } |
| + |
| +}()); // End of anonymous object |