Mirror of GNU Guix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.6 KiB

  1. From 0922145c255bf2503d3b2dd5f8f1e813338ba990 Mon Sep 17 00:00:00 2001
  2. From: Mats Palmgren <mats@mozilla.com>
  3. Date: Sat, 24 Jan 2015 12:37:47 -0500
  4. Subject: [PATCH] Bug 1110557. r=mak, r=gavin, a=bkerensa
  5. ---
  6. .../components/satchel/nsFormFillController.cpp | 67 +++++++++++++++-------
  7. toolkit/components/satchel/nsFormFillController.h | 5 ++
  8. 2 files changed, 52 insertions(+), 20 deletions(-)
  9. diff --git a/toolkit/components/satchel/nsFormFillController.cpp b/toolkit/components/satchel/nsFormFillController.cpp
  10. index 315fc68..676ad84 100644
  11. --- a/toolkit/components/satchel/nsFormFillController.cpp
  12. +++ b/toolkit/components/satchel/nsFormFillController.cpp
  13. @@ -61,6 +61,7 @@ nsFormFillController::nsFormFillController() :
  14. mSuppressOnInput(false)
  15. {
  16. mController = do_GetService("@mozilla.org/autocomplete/controller;1");
  17. + MOZ_ASSERT(mController);
  18. }
  19. struct PwmgrInputsEnumData
  20. @@ -104,6 +105,21 @@ nsFormFillController::AttributeChanged(nsIDocument* aDocument,
  21. int32_t aNameSpaceID,
  22. nsIAtom* aAttribute, int32_t aModType)
  23. {
  24. + if ((aAttribute == nsGkAtoms::type || aAttribute == nsGkAtoms::readonly ||
  25. + aAttribute == nsGkAtoms::autocomplete) &&
  26. + aNameSpaceID == kNameSpaceID_None) {
  27. + nsCOMPtr<nsIDOMHTMLInputElement> focusedInput(mFocusedInput);
  28. + // Reset the current state of the controller, unconditionally.
  29. + StopControllingInput();
  30. + // Then restart based on the new values. We have to delay this
  31. + // to avoid ending up in an endless loop due to re-registering our
  32. + // mutation observer (which would notify us again for *this* event).
  33. + nsCOMPtr<nsIRunnable> event =
  34. + NS_NewRunnableMethodWithArg<nsCOMPtr<nsIDOMHTMLInputElement>>
  35. + (this, &nsFormFillController::MaybeStartControllingInput, focusedInput);
  36. + NS_DispatchToCurrentThread(event);
  37. + }
  38. +
  39. if (mListNode && mListNode->Contains(aElement)) {
  40. RevalidateDataList();
  41. }
  42. @@ -841,28 +857,26 @@ nsFormFillController::RemoveForDocumentEnumerator(const nsINode* aKey,
  43. return PL_DHASH_NEXT;
  44. }
  45. -nsresult
  46. -nsFormFillController::Focus(nsIDOMEvent* aEvent)
  47. +void
  48. +nsFormFillController::MaybeStartControllingInput(nsIDOMHTMLInputElement* aInput)
  49. {
  50. - nsCOMPtr<nsIDOMHTMLInputElement> input = do_QueryInterface(
  51. - aEvent->InternalDOMEvent()->GetTarget());
  52. - nsCOMPtr<nsINode> inputNode = do_QueryInterface(input);
  53. + nsCOMPtr<nsINode> inputNode = do_QueryInterface(aInput);
  54. if (!inputNode)
  55. - return NS_OK;
  56. + return;
  57. - nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(input);
  58. + nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(aInput);
  59. if (!formControl || !formControl->IsSingleLineTextControl(true))
  60. - return NS_OK;
  61. + return;
  62. bool isReadOnly = false;
  63. - input->GetReadOnly(&isReadOnly);
  64. + aInput->GetReadOnly(&isReadOnly);
  65. if (isReadOnly)
  66. - return NS_OK;
  67. + return;
  68. - bool autocomplete = nsContentUtils::IsAutocompleteEnabled(input);
  69. + bool autocomplete = nsContentUtils::IsAutocompleteEnabled(aInput);
  70. nsCOMPtr<nsIDOMHTMLElement> datalist;
  71. - input->GetList(getter_AddRefs(datalist));
  72. + aInput->GetList(getter_AddRefs(datalist));
  73. bool hasList = datalist != nullptr;
  74. bool dummy;
  75. @@ -871,9 +885,16 @@ nsFormFillController::Focus(nsIDOMEvent* aEvent)
  76. isPwmgrInput = true;
  77. if (isPwmgrInput || hasList || autocomplete) {
  78. - StartControllingInput(input);
  79. + StartControllingInput(aInput);
  80. }
  81. +}
  82. +nsresult
  83. +nsFormFillController::Focus(nsIDOMEvent* aEvent)
  84. +{
  85. + nsCOMPtr<nsIDOMHTMLInputElement> input = do_QueryInterface(
  86. + aEvent->InternalDOMEvent()->GetTarget());
  87. + MaybeStartControllingInput(input);
  88. return NS_OK;
  89. }
  90. @@ -1087,6 +1108,10 @@ nsFormFillController::StartControllingInput(nsIDOMHTMLInputElement *aInput)
  91. // Make sure we're not still attached to an input
  92. StopControllingInput();
  93. + if (!mController) {
  94. + return;
  95. + }
  96. +
  97. // Find the currently focused docShell
  98. nsCOMPtr<nsIDocShell> docShell = GetDocShellForInput(aInput);
  99. int32_t index = GetIndexOfDocShell(docShell);
  100. @@ -1129,13 +1154,15 @@ nsFormFillController::StopControllingInput()
  101. mListNode = nullptr;
  102. }
  103. - // Reset the controller's input, but not if it has been switched
  104. - // to another input already, which might happen if the user switches
  105. - // focus by clicking another autocomplete textbox
  106. - nsCOMPtr<nsIAutoCompleteInput> input;
  107. - mController->GetInput(getter_AddRefs(input));
  108. - if (input == this)
  109. - mController->SetInput(nullptr);
  110. + if (mController) {
  111. + // Reset the controller's input, but not if it has been switched
  112. + // to another input already, which might happen if the user switches
  113. + // focus by clicking another autocomplete textbox
  114. + nsCOMPtr<nsIAutoCompleteInput> input;
  115. + mController->GetInput(getter_AddRefs(input));
  116. + if (input == this)
  117. + mController->SetInput(nullptr);
  118. + }
  119. if (mFocusedInputNode) {
  120. MaybeRemoveMutationObserver(mFocusedInputNode);
  121. diff --git a/toolkit/components/satchel/nsFormFillController.h b/toolkit/components/satchel/nsFormFillController.h
  122. index b60d28d..8c3ba26 100644
  123. --- a/toolkit/components/satchel/nsFormFillController.h
  124. +++ b/toolkit/components/satchel/nsFormFillController.h
  125. @@ -62,6 +62,11 @@ protected:
  126. void StartControllingInput(nsIDOMHTMLInputElement *aInput);
  127. void StopControllingInput();
  128. + /**
  129. + * Checks that aElement is a type of element we want to fill, then calls
  130. + * StartControllingInput on it.
  131. + */
  132. + void MaybeStartControllingInput(nsIDOMHTMLInputElement* aElement);
  133. nsresult PerformInputListAutoComplete(nsIAutoCompleteResult* aPreviousResult);
  134. --
  135. 2.2.1