OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/autofill/form_autofill_util.h" | 5 #include "chrome/renderer/autofill/form_autofill_util.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 for (size_t i = 0; i < list_items.size(); ++i) { | 406 for (size_t i = 0; i < list_items.size(); ++i) { |
407 if (IsOptionElement(list_items[i])) { | 407 if (IsOptionElement(list_items[i])) { |
408 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); | 408 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); |
409 option_values->push_back(option.value()); | 409 option_values->push_back(option.value()); |
410 option_contents->push_back(option.text()); | 410 option_contents->push_back(option.text()); |
411 } | 411 } |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 // The callback type used by |ForEachMatchingFormField()|. | 415 // The callback type used by |ForEachMatchingFormField()|. |
416 typedef void (*Callback)(WebKit::WebFormControlElement*, | 416 typedef void (*Callback)(const FormFieldData&, |
417 const FormFieldData*, | 417 bool, |
418 bool); | 418 WebKit::WebFormControlElement*); |
419 | 419 |
420 // For each autofillable field in |data| that matches a field in the |form|, | 420 // For each autofillable field in |data| that matches a field in the |form|, |
421 // the |callback| is invoked with the corresponding |form| field data. | 421 // the |callback| is invoked with the corresponding |form| field data. |
422 void ForEachMatchingFormField(const WebFormElement& form_element, | 422 void ForEachMatchingFormField(const WebFormElement& form_element, |
423 const WebElement& initiating_element, | 423 const WebElement& initiating_element, |
424 const FormData& data, | 424 const FormData& data, |
425 Callback callback) { | 425 Callback callback) { |
426 std::vector<WebFormControlElement> control_elements; | 426 std::vector<WebFormControlElement> control_elements; |
427 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, | 427 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, |
428 &control_elements); | 428 &control_elements); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 // Only autofill empty fields and the field that initiated the filling, | 462 // Only autofill empty fields and the field that initiated the filling, |
463 // i.e. the field the user is currently editing and interacting with. | 463 // i.e. the field the user is currently editing and interacting with. |
464 if (!is_initiating_element && !input_element->value().isEmpty()) | 464 if (!is_initiating_element && !input_element->value().isEmpty()) |
465 continue; | 465 continue; |
466 } | 466 } |
467 | 467 |
468 if (!element->isEnabled() || element->isReadOnly() || | 468 if (!element->isEnabled() || element->isReadOnly() || |
469 !element->isFocusable()) | 469 !element->isFocusable()) |
470 continue; | 470 continue; |
471 | 471 |
472 callback(element, &data.fields[i], is_initiating_element); | 472 callback(data.fields[i], is_initiating_element, element); |
473 } | |
474 } | |
475 | |
476 // Sets the |field|'s value to the value in |data|. | |
477 // Also sets the "autofilled" attribute, causing the background to be yellow. | |
478 void FillFormField(WebKit::WebFormControlElement* field, | |
479 const FormFieldData* data, | |
480 bool is_initiating_node) { | |
481 // Nothing to fill. | |
482 if (data->value.empty()) | |
483 return; | |
484 | |
485 WebInputElement* input_element = toWebInputElement(field); | |
486 if (IsTextInput(input_element)) { | |
487 // If the maxlength attribute contains a negative value, maxLength() | |
488 // returns the default maxlength value. | |
489 input_element->setValue( | |
490 data->value.substr(0, input_element->maxLength()), true); | |
491 input_element->setAutofilled(true); | |
492 if (is_initiating_node) { | |
493 int length = input_element->value().length(); | |
494 input_element->setSelectionRange(length, length); | |
495 // Clear the current IME composition (the underline), if there is one. | |
496 input_element->document().frame()->unmarkText(); | |
497 } | |
498 } else { | |
499 DCHECK(IsSelectElement(*field)); | |
500 WebSelectElement select_element = field->to<WebSelectElement>(); | |
501 if (select_element.value() != data->value) { | |
502 select_element.setValue(data->value); | |
503 select_element.dispatchFormControlChangeEvent(); | |
504 } | |
505 } | 473 } |
506 } | 474 } |
507 | 475 |
508 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | 476 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. |
509 // Also sets the "autofilled" attribute, causing the background to be yellow. | 477 // Also sets the "autofilled" attribute, causing the background to be yellow. |
510 void PreviewFormField(WebKit::WebFormControlElement* field, | 478 void PreviewFormField(const FormFieldData& data, |
511 const FormFieldData* data, | 479 bool is_initiating_node, |
512 bool is_initiating_node) { | 480 WebKit::WebFormControlElement* field) { |
513 // Nothing to preview. | 481 // Nothing to preview. |
514 if (data->value.empty()) | 482 if (data.value.empty()) |
515 return; | 483 return; |
516 | 484 |
517 // Only preview input fields. | 485 // Only preview input fields. |
518 WebInputElement* input_element = toWebInputElement(field); | 486 WebInputElement* input_element = toWebInputElement(field); |
519 if (!IsTextInput(input_element)) | 487 if (!IsTextInput(input_element)) |
520 return; | 488 return; |
521 | 489 |
522 // If the maxlength attribute contains a negative value, maxLength() | 490 // If the maxlength attribute contains a negative value, maxLength() |
523 // returns the default maxlength value. | 491 // returns the default maxlength value. |
524 input_element->setSuggestedValue( | 492 input_element->setSuggestedValue( |
525 data->value.substr(0, input_element->maxLength())); | 493 data.value.substr(0, input_element->maxLength())); |
526 input_element->setAutofilled(true); | 494 input_element->setAutofilled(true); |
527 if (is_initiating_node) { | 495 if (is_initiating_node) { |
528 // Select the part of the text that the user didn't type. | 496 // Select the part of the text that the user didn't type. |
529 input_element->setSelectionRange(input_element->value().length(), | 497 input_element->setSelectionRange(input_element->value().length(), |
530 input_element->suggestedValue().length()); | 498 input_element->suggestedValue().length()); |
531 } | 499 } |
532 } | 500 } |
533 | 501 |
534 } // namespace | 502 } // namespace |
535 | 503 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 WebFormElement form_element = element.form(); | 784 WebFormElement form_element = element.form(); |
817 if (form_element.isNull()) | 785 if (form_element.isNull()) |
818 return; | 786 return; |
819 | 787 |
820 ForEachMatchingFormField(form_element, | 788 ForEachMatchingFormField(form_element, |
821 element, | 789 element, |
822 form, | 790 form, |
823 &FillFormField); | 791 &FillFormField); |
824 } | 792 } |
825 | 793 |
| 794 // Sets the |field|'s value to the value in |data|. |
| 795 // Also sets the "autofilled" attribute, causing the background to be yellow. |
| 796 void FillFormField(const FormFieldData& data, |
| 797 bool is_initiating_node, |
| 798 WebKit::WebFormControlElement* field) { |
| 799 // Nothing to fill. |
| 800 if (data.value.empty()) |
| 801 return; |
| 802 |
| 803 WebInputElement* input_element = toWebInputElement(field); |
| 804 if (IsTextInput(input_element)) { |
| 805 // If the maxlength attribute contains a negative value, maxLength() |
| 806 // returns the default maxlength value. |
| 807 input_element->setValue( |
| 808 data.value.substr(0, input_element->maxLength()), true); |
| 809 input_element->setAutofilled(true); |
| 810 if (is_initiating_node) { |
| 811 int length = input_element->value().length(); |
| 812 input_element->setSelectionRange(length, length); |
| 813 // Clear the current IME composition (the underline), if there is one. |
| 814 input_element->document().frame()->unmarkText(); |
| 815 } |
| 816 } else { |
| 817 DCHECK(IsSelectElement(*field)); |
| 818 WebSelectElement select_element = field->to<WebSelectElement>(); |
| 819 if (select_element.value() != data.value) { |
| 820 select_element.setValue(data.value); |
| 821 select_element.dispatchFormControlChangeEvent(); |
| 822 } |
| 823 } |
| 824 } |
| 825 |
826 void PreviewForm(const FormData& form, const WebInputElement& element) { | 826 void PreviewForm(const FormData& form, const WebInputElement& element) { |
827 WebFormElement form_element = element.form(); | 827 WebFormElement form_element = element.form(); |
828 if (form_element.isNull()) | 828 if (form_element.isNull()) |
829 return; | 829 return; |
830 | 830 |
831 ForEachMatchingFormField(form_element, | 831 ForEachMatchingFormField(form_element, |
832 element, | 832 element, |
833 form, | 833 form, |
834 &PreviewFormField); | 834 &PreviewFormField); |
835 } | 835 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 continue; | 896 continue; |
897 | 897 |
898 if (input_element->isAutofilled()) | 898 if (input_element->isAutofilled()) |
899 return true; | 899 return true; |
900 } | 900 } |
901 | 901 |
902 return false; | 902 return false; |
903 } | 903 } |
904 | 904 |
905 } // namespace autofill | 905 } // namespace autofill |
OLD | NEW |