Commit b62ad12e authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Commit Bot

[Autofill Assistant] SelectOption now gives the selected option as a call argument.

This avoids problems when replacing values that have special characters
(like ' and \) in the script.

Bug: 806868
Change-Id: I68ef1b3d549b0d17b6d6bb638d07cd086ae64400
Reviewed-on: https://chromium-review.googlesource.com/1244679
Commit-Queue: Jordan Demeulenaere <jdemeulenaere@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594631}
parent ad7e5862
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_util.h"
#include "components/autofill/content/browser/content_autofill_driver.h" #include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/autofill_manager.h" #include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/common/form_data.h" #include "components/autofill/core/common/form_data.h"
...@@ -30,16 +29,15 @@ const char* const kScrollIntoViewScript = ...@@ -30,16 +29,15 @@ const char* const kScrollIntoViewScript =
node.scrollIntoViewIfNeeded();\ node.scrollIntoViewIfNeeded();\
}"; }";
// Javascript where $1 will be replaced with the option value. Also fires a // Javascript to select a value from a select box. Also fires a "change" event
// "change" event to trigger any listeners. Changing the index directly does not // to trigger any listeners. Changing the index directly does not trigger this.
// trigger this.
const char* const kSelectOptionScript = const char* const kSelectOptionScript =
R"(function() { R"(function(value) {
const value = '$1'.toUpperCase(); const uppercaseValue = value.toUpperCase();
var found = false; var found = false;
for (var i = 0; i < this.options.length; ++i) { for (var i = 0; i < this.options.length; ++i) {
const label = this.options[i].label.toUpperCase(); const label = this.options[i].label.toUpperCase();
if (label.length > 0 && label.startsWith(value)) { if (label.length > 0 && label.startsWith(uppercaseValue)) {
this.options.selectedIndex = i; this.options.selectedIndex = i;
found = true; found = true;
break; break;
...@@ -58,8 +56,7 @@ const char* const kSelectOptionScript = ...@@ -58,8 +56,7 @@ const char* const kSelectOptionScript =
const char* const kGetValueAttributeScript = const char* const kGetValueAttributeScript =
"function () { return this.value; }"; "function () { return this.value; }";
// Javascript code to set the 'value' attribute of a node, where $1 will be // Javascript code to set the 'value' attribute of a node.
// replaced with the value.
const char* const kSetValueAttributeScript = const char* const kSetValueAttributeScript =
"function (value) { this.value = value; }"; "function (value) { this.value = value; }";
...@@ -548,18 +545,17 @@ void WebController::OnFindElementForSelectOption( ...@@ -548,18 +545,17 @@ void WebController::OnFindElementForSelectOption(
return; return;
} }
std::string select_option_script = base::ReplaceStringPlaceholders(
kSelectOptionScript, {selected_option}, nullptr);
std::vector<std::unique_ptr<runtime::CallArgument>> argument; std::vector<std::unique_ptr<runtime::CallArgument>> argument;
argument.emplace_back( argument.emplace_back(
runtime::CallArgument::Builder().SetObjectId(object_id).Build()); runtime::CallArgument::Builder()
.SetValue(base::Value::ToUniquePtrValue(base::Value(selected_option)))
.Build());
devtools_client_->GetRuntime()->Enable(); devtools_client_->GetRuntime()->Enable();
devtools_client_->GetRuntime()->CallFunctionOn( devtools_client_->GetRuntime()->CallFunctionOn(
runtime::CallFunctionOnParams::Builder() runtime::CallFunctionOnParams::Builder()
.SetObjectId(object_id) .SetObjectId(object_id)
.SetArguments(std::move(argument)) .SetArguments(std::move(argument))
.SetFunctionDeclaration(std::string(select_option_script)) .SetFunctionDeclaration(std::string(kSelectOptionScript))
.SetReturnByValue(true) .SetReturnByValue(true)
.Build(), .Build(),
base::BindOnce(&WebController::OnSelectOption, base::BindOnce(&WebController::OnSelectOption,
......
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