2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "AutofillElements.h"
29 #include "FocusController.h"
34 static inline bool isAutofillableElement(Element& node)
36 if (!is<HTMLInputElement>(node))
39 auto inputElement = &downcast<HTMLInputElement>(node);
40 return inputElement->isTextField() || inputElement->isEmailField();
43 static inline RefPtr<HTMLInputElement> nextAutofillableElement(Node* startNode, FocusController& focusController)
45 if (!is<Element>(startNode))
48 RefPtr<Element> nextElement = downcast<Element>(startNode);
50 nextElement = focusController.nextFocusableElement(*nextElement.get());
51 } while (nextElement && !isAutofillableElement(*nextElement.get()));
56 return &downcast<HTMLInputElement>(*nextElement);
59 static inline RefPtr<HTMLInputElement> previousAutofillableElement(Node* startNode, FocusController& focusController)
61 if (!is<Element>(startNode))
64 RefPtr<Element> previousElement = downcast<Element>(startNode);
66 previousElement = focusController.previousFocusableElement(*previousElement.get());
67 } while (previousElement && !isAutofillableElement(*previousElement.get()));
72 return &downcast<HTMLInputElement>(*previousElement);
75 AutofillElements::AutofillElements(RefPtr<HTMLInputElement>&& username, RefPtr<HTMLInputElement>&& password)
76 : m_username(username)
77 , m_password(password)
81 std::optional<AutofillElements> AutofillElements::computeAutofillElements(Ref<HTMLInputElement> start)
83 if (!start->document().page())
85 FocusController& focusController = start->document().page()->focusController();
86 if (start->isPasswordField()) {
87 RefPtr<HTMLInputElement> previousElement = previousAutofillableElement(start.ptr(), focusController);
88 RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController);
89 bool hasDuplicatePasswordElements = (nextElement && nextElement->isPasswordField()) || (previousElement && previousElement->isPasswordField());
90 if (hasDuplicatePasswordElements)
93 if (previousElement && is<HTMLInputElement>(*previousElement)) {
94 if (previousElement->isTextField())
95 return AutofillElements(WTFMove(previousElement), WTFMove(start));
98 RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController);
99 if (nextElement && is<HTMLInputElement>(*nextElement)) {
100 if (nextElement->isPasswordField()) {
101 RefPtr<HTMLInputElement> elementAfternextElement = nextAutofillableElement(nextElement.get(), focusController);
102 bool hasDuplicatePasswordElements = elementAfternextElement && elementAfternextElement->isPasswordField();
103 if (hasDuplicatePasswordElements)
106 return AutofillElements(WTFMove(start), WTFMove(nextElement));
111 if (start->isPasswordField()) {
112 RefPtr<HTMLInputElement> previousElement = previousAutofillableElement(start.ptr(), focusController);
113 RefPtr<HTMLInputElement> nextElement = nextAutofillableElement(start.ptr(), focusController);
114 if (!previousElement && !nextElement)
115 return AutofillElements(nullptr, start.ptr());
120 void AutofillElements::autofill(String username, String password)
123 m_username->setValueForUser(username);
125 m_password->setValueForUser(password);
128 } // namespace WebCore