Commit 3a10ca5e authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Use a single function to set field value.

This single function will make it easier to support more scenario
(events, frameworks...).

Bug: 788099
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib76aadebbb3c6999b20b5261a8b0045689bd2785
Reviewed-on: https://chromium-review.googlesource.com/786239
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520954}
parent 364b6405
......@@ -634,13 +634,9 @@ __gCrWeb.autofill['fillForm'] = function(data, forceFillFieldName) {
continue;
if (__gCrWeb.autofill.isTextInput(element) ||
__gCrWeb.autofill.isTextAreaElement(element)) {
__gCrWeb.autofill.isTextAreaElement(element) ||
__gCrWeb.autofill.isSelectElement(element)) {
__gCrWeb.common.setInputElementValue(value, element, true);
} else if (__gCrWeb.autofill.isSelectElement(element)) {
if (element.value !== value) {
element.value = value;
__gCrWeb.common.notifyElementValueChanged(element);
}
}
// TODO(bondd): Handle __gCrWeb.autofill.isCheckableElement(element) ==
// true. |is_checked| is not currently passed in by the caller.
......@@ -690,10 +686,8 @@ __gCrWeb.autofill['clearAutofilledFields'] = function(formName) {
} else if (__gCrWeb.autofill.isSelectElement(element)) {
// Reset to the first index.
// TODO(bondd): Store initial values and reset to the correct one here.
if (element.selectedIndex != 0) {
element.selectedIndex = 0;
__gCrWeb.common.notifyElementValueChanged(element);
}
__gCrWeb.common.setInputElementValue(element.option[0].value,
element, true);
} else if (__gCrWeb.autofill.isCheckableElement(element)) {
// TODO(bondd): Handle checkable elements. They aren't properly supported
// by iOS Autofill yet.
......@@ -1848,16 +1842,11 @@ __gCrWeb.autofill.fillFormField = function(data, field) {
__gCrWeb.common.setInputElementValue(sanitizedValue, field, true);
field.isAutofilled = true;
} else if (__gCrWeb.autofill.isSelectElement(field)) {
if (field.value !== data['value']) {
field.value = data['value'];
__gCrWeb.common.notifyElementValueChanged(field);
} else if (__gCrWeb.autofill.isSelectElement(field)) {
__gCrWeb.common.setInputElementValue(data['value'], field, true);
} else if (__gCrWeb.autofill.isCheckableElement(field)) {
__gCrWeb.common.setInputElementValue(data['is_checked'], field, true);
}
} else {
if (__gCrWeb.autofill.isCheckableElement(field)) {
__gCrWeb.common.setInputElementChecked(data['is_checked'], field, true);
}
}
};
/**
......
......@@ -231,35 +231,7 @@ __gCrWeb['common'] = __gCrWeb.common;
};
/**
* Sets the checked value of an input and dispatches an change event if
* |shouldSendChangeEvent|.
*
* This is a simplified version of the implementation of
*
* void setChecked(bool nowChecked, TextFieldEventBehavior eventBehavior)
*
* in chromium/src/third_party/WebKit/Source/WebKit/chromium/src/
* WebInputElement.cpp, which calls
* void HTMLInputElement::setChecked(
* bool nowChecked, TextFieldEventBehavior eventBehavior)
* in chromium/src/third_party/WebKit/Source/core/html/HTMLInputElement.cpp.
*
* @param {boolean} nowChecked The new checked value of the input element.
* @param {Element} input The input element of which the value is set.
* @param {boolean} shouldSendChangeEvent Whether a change event should be
* dispatched.
*/
__gCrWeb.common.setInputElementChecked = function(
nowChecked, input, shouldSendChangeEvent) {
var checkedChanged = input.checked !== nowChecked;
input.checked = nowChecked;
if (checkedChanged) {
__gCrWeb.common.notifyElementValueChanged(input);
}
};
/**
* Sets the value of an input and dispatches an change event if
* Sets the value of an input and dispatches a change event if
* |shouldSendChangeEvent|.
*
* It is based on the logic in
......@@ -269,24 +241,41 @@ __gCrWeb['common'] = __gCrWeb.common;
* in chromium/src/third_party/WebKit/Source/WebKit/chromium/src/
* WebInputElement.cpp, which calls
* void setValue(const String& value, TextFieldEventBehavior eventBehavior)
* or
* void setChecked(bool nowChecked, TextFieldEventBehavior eventBehavior)
* in chromium/src/third_party/WebKit/Source/core/html/HTMLInputElement.cpp.
*
* @param {string} value The value the input element will be set.
* @param {(string|boolean)} value The value the input element will be set.
* For text input, it is the value to set in the field.
* For select, it is the value of the option to select.
* For checkable element, it is the checked value (true/false).
* @param {Element} input The input element of which the value is set.
* @param {boolean} shouldSendChangeEvent Whether a change event should be
* dispatched.
*/
__gCrWeb.common.setInputElementValue = function(
value, input, shouldSendChangeEvent) {
// In HTMLInputElement.cpp there is a check on canSetValue(value), which
// returns false only for file input. As file input is not relevant for
// autofill and this method is only used for autofill for now, there is no
// such check in this implementation.
var sanitizedValue = __gCrWeb.common.sanitizeValueForInputElement(
value, input);
var valueChanged = sanitizedValue !== input.value;
input.value = sanitizedValue;
if (valueChanged) {
if (!input) {
return;
}
var changed = false;
if (input.type === 'checkbox' || input.type === 'radio') {
changed = input.checked !== value;
input.checked = value;
} else if (input.type === 'select-one') {
changed = input.value !== value;
input.value = value;
} else {
// In HTMLInputElement.cpp there is a check on canSetValue(value), which
// returns false only for file input. As file input is not relevant for
// autofill and this method is only used for autofill for now, there is no
// such check in this implementation.
var sanitizedValue = __gCrWeb.common.sanitizeValueForInputElement(
/** @type {string} */ (value), input);
changed = sanitizedValue !== input.value;
input.value = sanitizedValue;
}
if (changed && shouldSendChangeEvent) {
__gCrWeb.common.notifyElementValueChanged(input);
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment