Commit 5912ae5c authored by thestig's avatar thestig Committed by Commit bot

Autofill: Modify various utility function to deal with unowned form fields.

BUG=428919

Review URL: https://codereview.chromium.org/804443002

Cr-Commit-Position: refs/heads/master@{#308224}
parent 957dee7b
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebElementCollection.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
class GURL; class GURL;
...@@ -114,18 +115,31 @@ bool WebFormElementToFormData( ...@@ -114,18 +115,31 @@ bool WebFormElementToFormData(
FormData* form, FormData* form,
FormFieldData* field); FormFieldData* field);
// Get all form control elements from |elements| that are not part of a form.
// If |fieldsets| is not NULL, also append the fieldsets encountered that are
// not part of a form.
std::vector<blink::WebFormControlElement>
GetUnownedAutofillableFormFieldElements(
const blink::WebElementCollection& elements,
std::vector<blink::WebElement>* fieldsets);
// Fills |form| with the form data derived from |fieldsets|, |control_elements| // Fills |form| with the form data derived from |fieldsets|, |control_elements|
// and |origin|. |extract_mask| usage and the return value are the same as // and |origin|. If |field| is non-NULL, fill it with the FormField
// representation for |element|.
// |extract_mask| usage and the return value are the same as
// WebFormElementToFormData() above. // WebFormElementToFormData() above.
bool UnownedFormElementsAndFieldSetsToFormData( bool UnownedFormElementsAndFieldSetsToFormData(
const std::vector<blink::WebElement>& fieldsets, const std::vector<blink::WebElement>& fieldsets,
const std::vector<blink::WebFormControlElement>& control_elements, const std::vector<blink::WebFormControlElement>& control_elements,
const blink::WebFormControlElement* element,
const GURL& origin, const GURL& origin,
RequirementsMask requirements,
ExtractMask extract_mask, ExtractMask extract_mask,
FormData* form); FormData* form,
FormFieldData* field);
// Finds the form that contains |element| and returns it in |form|. Fills // Finds the form that contains |element| and returns it in |form|. If |field|
// |field| with the |FormField| representation for element. // is non-NULL, fill it with the FormField representation for |element|.
// Returns false if the form is not found or cannot be serialized. // Returns false if the form is not found or cannot be serialized.
bool FindFormAndFieldForFormControlElement( bool FindFormAndFieldForFormControlElement(
const blink::WebFormControlElement& element, const blink::WebFormControlElement& element,
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h" #include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElementCollection.h"
#include "third_party/WebKit/public/web/WebFormControlElement.h" #include "third_party/WebKit/public/web/WebFormControlElement.h"
#include "third_party/WebKit/public/web/WebFormElement.h" #include "third_party/WebKit/public/web/WebFormElement.h"
#include "third_party/WebKit/public/web/WebInputElement.h" #include "third_party/WebKit/public/web/WebInputElement.h"
...@@ -29,7 +28,6 @@ ...@@ -29,7 +28,6 @@
using blink::WebConsoleMessage; using blink::WebConsoleMessage;
using blink::WebDocument; using blink::WebDocument;
using blink::WebElement; using blink::WebElement;
using blink::WebElementCollection;
using blink::WebFormControlElement; using blink::WebFormControlElement;
using blink::WebFormElement; using blink::WebFormElement;
using blink::WebFrame; using blink::WebFrame;
...@@ -87,22 +85,6 @@ void LogDeprecationMessages(const WebFormControlElement& element) { ...@@ -87,22 +85,6 @@ void LogDeprecationMessages(const WebFormControlElement& element) {
} }
} }
bool IsElementInsideFormOrFieldSet(const WebElement& element) {
for (WebNode parent_node = element.parentNode();
!parent_node.isNull();
parent_node = parent_node.parentNode()) {
if (!parent_node.isElementNode())
continue;
WebElement cur_element = parent_node.to<WebElement>();
if (cur_element.hasHTMLTagName("form") ||
cur_element.hasHTMLTagName("fieldset")) {
return true;
}
}
return false;
}
// To avoid overly expensive computation, we impose a minimum number of // To avoid overly expensive computation, we impose a minimum number of
// allowable fields. The corresponding maximum number of allowable fields // allowable fields. The corresponding maximum number of allowable fields
// is imposed by WebFormElementToFormData(). // is imposed by WebFormElementToFormData().
...@@ -120,30 +102,6 @@ FormCache::FormCache() { ...@@ -120,30 +102,6 @@ FormCache::FormCache() {
FormCache::~FormCache() { FormCache::~FormCache() {
} }
// static
std::vector<WebFormControlElement>
FormCache::GetUnownedAutofillableFormFieldElements(
const WebElementCollection& elements,
std::vector<WebElement>* fieldsets) {
std::vector<WebFormControlElement> unowned_fieldset_children;
for (WebElement element = elements.firstItem();
!element.isNull();
element = elements.nextItem()) {
if (element.isFormControlElement()) {
WebFormControlElement control = element.to<WebFormControlElement>();
if (control.form().isNull())
unowned_fieldset_children.push_back(control);
}
if (fieldsets && element.hasHTMLTagName("fieldset") &&
!IsElementInsideFormOrFieldSet(element)) {
fieldsets->push_back(element);
}
}
return ExtractAutofillableElementsFromSet(unowned_fieldset_children,
REQUIRE_NONE);
}
std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) { std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) {
std::vector<FormData> forms; std::vector<FormData> forms;
WebDocument document = frame.document(); WebDocument document = frame.document();
...@@ -177,7 +135,7 @@ std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) { ...@@ -177,7 +135,7 @@ std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) {
FormData form; FormData form;
if (!WebFormElementToFormData(form_element, WebFormControlElement(), if (!WebFormElementToFormData(form_element, WebFormControlElement(),
REQUIRE_NONE, extract_mask, &form, NULL)) { REQUIRE_NONE, extract_mask, &form, nullptr)) {
continue; continue;
} }
...@@ -205,8 +163,9 @@ std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) { ...@@ -205,8 +163,9 @@ std::vector<FormData> FormCache::ExtractNewForms(const WebFrame& frame) {
FormData form; FormData form;
if (!UnownedFormElementsAndFieldSetsToFormData(fieldsets, control_elements, if (!UnownedFormElementsAndFieldSetsToFormData(fieldsets, control_elements,
document.url(), extract_mask, nullptr, document.url(),
&form)) { REQUIRE_NONE, extract_mask,
&form, nullptr)) {
return forms; return forms;
} }
...@@ -241,11 +200,14 @@ void FormCache::ResetFrame(const WebFrame& frame) { ...@@ -241,11 +200,14 @@ void FormCache::ResetFrame(const WebFrame& frame) {
bool FormCache::ClearFormWithElement(const WebFormControlElement& element) { bool FormCache::ClearFormWithElement(const WebFormControlElement& element) {
WebFormElement form_element = element.form(); WebFormElement form_element = element.form();
if (form_element.isNull()) std::vector<WebFormControlElement> control_elements;
return false; if (form_element.isNull()) {
control_elements = GetUnownedAutofillableFormFieldElements(
std::vector<WebFormControlElement> control_elements = element.document().all(), nullptr);
ExtractAutofillableElementsInForm(form_element, REQUIRE_NONE); } else {
control_elements = ExtractAutofillableElementsInForm(
form_element, REQUIRE_NONE);
}
for (size_t i = 0; i < control_elements.size(); ++i) { for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement control_element = control_elements[i]; WebFormControlElement control_element = control_elements[i];
// Don't modify the value of disabled fields. // Don't modify the value of disabled fields.
......
...@@ -32,15 +32,6 @@ class FormCache { ...@@ -32,15 +32,6 @@ class FormCache {
FormCache(); FormCache();
~FormCache(); ~FormCache();
// Get all form control elements from |elements| that are not part of a form.
// If |fieldsets| is not NULL, also append the fieldsets encountered that are
// not part of a form.
// Exposed for sharing with tests.
static std::vector<blink::WebFormControlElement>
GetUnownedAutofillableFormFieldElements(
const blink::WebElementCollection& elements,
std::vector<blink::WebElement>* fieldsets);
// Scans the DOM in |frame| extracting and storing forms that have not been // Scans the DOM in |frame| extracting and storing forms that have not been
// seen before. Returns the extracted forms. // seen before. Returns the extracted forms.
std::vector<FormData> ExtractNewForms(const blink::WebFrame& frame); std::vector<FormData> ExtractNewForms(const blink::WebFrame& frame);
......
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