Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // Installs Autofill management functions on the |__gCrWeb| object. | 5 // Installs Autofill management functions on the |__gCrWeb| object. |
| 6 // | 6 // |
| 7 // It scans the DOM, extracting and storing forms and returns a JSON string | 7 // It scans the DOM, extracting and storing forms and returns a JSON string |
| 8 // representing an array of objects, each of which represents an Autofill form | 8 // representing an array of objects, each of which represents an Autofill form |
| 9 // with information about a form to be filled and/or submitted and it can be | 9 // with information about a form to be filled and/or submitted and it can be |
| 10 // translated to struct FormData | 10 // translated to struct FormData |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 return; | 494 return; |
| 495 } | 495 } |
| 496 __gCrWeb.autofill.lastAutoFilledElement = activeElement; | 496 __gCrWeb.autofill.lastAutoFilledElement = activeElement; |
| 497 __gCrWeb.autofill.fillFormField(data, activeElement); | 497 __gCrWeb.autofill.fillFormField(data, activeElement); |
| 498 }; | 498 }; |
| 499 | 499 |
| 500 /** | 500 /** |
| 501 * Fills a number of fields in the same named form. | 501 * Fills a number of fields in the same named form. |
| 502 * | 502 * |
| 503 * @param {Object<AutofillFormData>} data The data to fill in. | 503 * @param {Object<AutofillFormData>} data The data to fill in. |
| 504 * @param {boolean} onlyFillEmpty Only fill empty fields. Otherwise fill all | |
| 505 * fields. | |
| 506 * @param {string} forceFillFieldName Named field will always be filled, | |
|
Justin Donnelly
2015/07/20 17:21:41
Is it possible to use document.activeElement rathe
bondd
2015/07/20 23:55:29
fillActiveFormField() (the function immediately ab
| |
| 507 * regardless of value of |onlyFillEmpty|. May be null. | |
| 504 * @param {boolean} styleElements Apply Autofill CSS style to filled elements. | 508 * @param {boolean} styleElements Apply Autofill CSS style to filled elements. |
| 505 */ | 509 */ |
| 506 __gCrWeb.autofill['fillForm'] = function(data, styleElements) { | 510 __gCrWeb.autofill['fillForm'] = function(data, onlyFillEmpty, |
| 511 forceFillFieldName, styleElements) { | |
| 507 // Inject CSS to style the autofilled elements with a yellow background. | 512 // Inject CSS to style the autofilled elements with a yellow background. |
| 508 if (styleElements && !__gCrWeb.autofill.styleInjected) { | 513 if (styleElements && !__gCrWeb.autofill.styleInjected) { |
| 509 var style = document.createElement('style'); | 514 var style = document.createElement('style'); |
| 510 style.textContent = '[chrome-autofilled] {' + | 515 style.textContent = '[chrome-autofilled] {' + |
| 511 'background-color:#FAFFBD !important;' + | 516 'background-color:#FAFFBD !important;' + |
| 512 'background-image:none !important;' + | 517 'background-image:none !important;' + |
| 513 'color:#000000 !important;' + | 518 'color:#000000 !important;' + |
| 514 '}'; | 519 '}'; |
| 515 document.head.appendChild(style); | 520 document.head.appendChild(style); |
| 516 __gCrWeb.autofill.styleInjected = true; | 521 __gCrWeb.autofill.styleInjected = true; |
| 517 } | 522 } |
| 518 | 523 |
| 519 // Remove Autofill styling when control element is edited. | 524 // Remove Autofill styling when control element is edited. |
| 520 var controlElementInputListener = function(evt) { | 525 var controlElementInputListener = function(evt) { |
| 521 evt.target.removeAttribute('chrome-autofilled'); | 526 evt.target.removeAttribute('chrome-autofilled'); |
| 527 evt.target.isAutofilled = false; | |
| 522 evt.target.removeEventListener('input', controlElementInputListener); | 528 evt.target.removeEventListener('input', controlElementInputListener); |
| 523 }; | 529 }; |
| 524 | 530 |
| 525 var form = __gCrWeb.common.getFormElementFromIdentifier(data.formName); | 531 var form = __gCrWeb.common.getFormElementFromIdentifier(data.formName); |
| 526 var controlElements = __gCrWeb.common.getFormControlElements(form); | 532 var controlElements = __gCrWeb.common.getFormControlElements(form); |
| 527 for (var i = 0; i < controlElements.length; ++i) { | 533 for (var i = 0; i < controlElements.length; ++i) { |
| 528 var element = controlElements[i]; | 534 var element = controlElements[i]; |
| 529 if (!__gCrWeb.autofill.isAutofillableElement(element)) { | 535 if (!__gCrWeb.autofill.isAutofillableElement(element)) { |
| 530 continue; | 536 continue; |
| 531 } | 537 } |
| 532 var value = data.fields[__gCrWeb['common'].nameForAutofill(element)]; | 538 var fieldName = __gCrWeb['common'].nameForAutofill(element); |
| 539 | |
| 540 // 'select-one' elements are autofilled even if non-empty. See | |
|
Justin Donnelly
2015/07/20 17:21:41
This comment looks like it's meant to describe the
bondd
2015/07/20 23:55:29
Done.
| |
| 541 // AutofillManager::FillOrPreviewDataModelForm(). | |
| 542 if (onlyFillEmpty && element.value && element.value.length > 0 && | |
| 543 !__gCrWeb.autofill.isSelectElement(element) && | |
| 544 fieldName !== forceFillFieldName) { | |
| 545 continue; | |
| 546 } | |
| 547 var value = data.fields[fieldName]; | |
| 533 if (value) { | 548 if (value) { |
| 534 element.value = value; | 549 element.value = value; |
| 535 if (styleElements) { | 550 if (styleElements) { |
| 536 element.setAttribute('chrome-autofilled'); | 551 element.setAttribute('chrome-autofilled'); |
| 552 element.isAutofilled = true; | |
| 537 element.addEventListener('input', controlElementInputListener); | 553 element.addEventListener('input', controlElementInputListener); |
| 538 } | 554 } |
| 539 } | 555 } |
| 540 } | 556 } |
| 541 | 557 |
| 542 // Remove Autofill styling when form receives 'reset' event. | 558 // Remove Autofill styling when form receives 'reset' event. |
| 543 // Individual control elements may be left with 'input' event listeners but | 559 // Individual control elements may be left with 'input' event listeners but |
| 544 // they are harmless. | 560 // they are harmless. |
| 545 if (styleElements) { | 561 if (styleElements) { |
| 546 var formResetListener = function(evt) { | 562 var formResetListener = function(evt) { |
| 547 var controlElements = __gCrWeb.common.getFormControlElements(evt.target); | 563 var controlElements = __gCrWeb.common.getFormControlElements(evt.target); |
| 548 for (var i = 0; i < controlElements.length; ++i) { | 564 for (var i = 0; i < controlElements.length; ++i) { |
| 549 controlElements[i].removeAttribute('chrome-autofilled'); | 565 controlElements[i].removeAttribute('chrome-autofilled'); |
| 566 controlElements[i].isAutofilled = false; | |
| 550 } | 567 } |
| 551 evt.target.removeEventListener('reset', formResetListener); | 568 evt.target.removeEventListener('reset', formResetListener); |
| 552 }; | 569 }; |
| 553 | 570 |
| 554 form.addEventListener('reset', formResetListener); | 571 form.addEventListener('reset', formResetListener); |
| 555 } | 572 } |
| 556 }; | 573 }; |
| 557 | 574 |
| 558 /** | 575 /** |
| 559 * Dispatch an autocomplete event to the named form. | 576 * Dispatch an autocomplete event to the named form. |
| (...skipping 1186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1746 continue; | 1763 continue; |
| 1747 } | 1764 } |
| 1748 var elementName = __gCrWeb['common'].nameForAutofill(element); | 1765 var elementName = __gCrWeb['common'].nameForAutofill(element); |
| 1749 var value = formData[elementName]; | 1766 var value = formData[elementName]; |
| 1750 if (value) { | 1767 if (value) { |
| 1751 element.placeholder = value; | 1768 element.placeholder = value; |
| 1752 } | 1769 } |
| 1753 } | 1770 } |
| 1754 } | 1771 } |
| 1755 }; | 1772 }; |
| OLD | NEW |